﻿/*--- tabs ---*/
function initTabs() {
    $('ul.tabset').each(function() {
        var btn_h = $(this);
        var _btn = $(this).find('a.tab');
        var _a = _btn.index(_btn.filter('.active:eq(0)'));
        if (_a == -1) _a = 0;
        _btn.removeClass('active').eq(_a).addClass('active');
        _btn.each(function(_i) {
            this._box = this.href.substr(this.href.indexOf("#") + 1);
            if (this._box) {
                this._box = $('#' + this._box);
                if (_i == _a) this._box.show();
                else this._box.hide();
            }
            $(this).click(function(e) {
                changeTab(_i);
                e.preventDefault();
                return false;
            });
        });
        function changeTab(_ind) {
            if (_ind != _a) {
                if (_btn.get(_a)._box) _btn.get(_a)._box.hide();
                if (_btn.get(_ind)._box) _btn.get(_ind)._box.fadeIn(1000);
                _btn.eq(_a).removeClass('active');
                _btn.eq(_ind).addClass('active');
                _a = _ind;
            }
        }
    });
}

function contactFormStatus() {
    $(".ask_us .employment_status select").change(function() {
        value = $(this).val()
        if (value == "zaposlen") {
            $(".institution").show();
        } else {
            $(".institution").hide();
        }
    });
}

$.fn.slideToggle = function() {
    return this.each(function() {
        var el = $(this);
        el.find('h4 a').click(function(e) {
            e.preventDefault();
        });
        el.children('li').click(function() {
            if ($(this).find('.w').is(':hidden')) {
                $(this).find('.w').slideDown().parent().addClass('active');
            } else {
                $(this).find('.w').slideUp().parent().removeClass('active');
            }
        });
    });
}
var toggleVisuals = function(){
    el = $('.promo .player');
    selected = el.find('img:not(.hidden)');
    next = selected.next();
    if(next.length == 0)next = el.find('img:first');
    next.fadeIn().removeClass('hidden');
    selected.fadeOut().addClass('hidden');
}

var toggleSocial = function(show,e){
    if(show){
        e.find('.w').fadeIn();
        e.children('a').fadeOut();
    } else {
        e.find('.w').fadeOut();
        e.children('a').fadeIn();
    }
}

var sendFriend = function(e){

    if ($('.send-friend form input[name=name]').val() == '') {
        alert('Pozabili ste vpisati vaše ime');
        $('.send-friend form input[name=name]').focus();
        e.preventDefault();
        return;
    }

    if ($('.send-friend form input[name=email]').val() == '') {
        alert('Pozabili ste vpisati naslovnikov email');
        $('.send-friend form input[name=email]').focus();
        e.preventDefault();
        return;
    }
	
    $.get("/send-friend",
            $('#sendFriendBox form').serialize(),
            function(data) {
                $('#sendFriendBox').jqmHide();
            });
}

$(document).ready(function() {
    initTabs();
    contactFormStatus();
    $('.winners').slideToggle();
    $('select#year').change(function() {
        $(this).parents('form').submit();
    });
    yoxviewPath = "/yoxview/";
    $("ol.images").yoxview({lang: 'sl'});
    $('.link-list li.socials').hover(function(){toggleSocial(true,$(this))},function(){toggleSocial(false,$(this))});
    $('.link-list li.send-friend').click(function(){$('#sendFriendBox').jqm().jqmShow();});
    $('#sendFriendBox .submit').click(sendFriend);
    $('.twitter a').click(function(){shareOnTwitter(window.location,'Krkine nagrade','Krkine nagrade');return false;});
    $('.facebook a').click(function(){shareOnFacebook(window.location,'Krkine nagrade','Krkine nagrade');return false;});
    $('.myspace a').click(function(){shareOnMyspace(window.location,'Krkine nagrade','Krkine nagrade');return false;});
    $('.send-friend #friend-email').focus(function(){$(this).val('');$(this).css({'color' : '#000', 'font-style' : 'normal'})});
    setInterval('toggleVisuals()',2500);
});

/* share */
function shareOnFacebook(url, title, comment) {
    if(url == "" || url == null || url == undefined) {
        url = encodeURIComponent(document.location);
    } else {
        url = encodeURIComponent(url);
    }

    if(title == "" || title == null || title == undefined) {
        title = encodeURIComponent(document.title);
    } else {
        title = encodeURIComponent(title);
    }

//    comment = encodeURIComponent(comment.replace("[URL]", document.location));

    window.open('http://www.facebook.com/sharer.php?u=' + url + '&t=' + title, 'sharer', 'toolbar=0,status=0,width=626,height=436');
}

function shareOnMyspace(url, title, comment) {
    if(url == "" || url == null || url == undefined) {
        url = encodeURIComponent(document.location);
    } else {
        url = encodeURIComponent(url);
    }

    if(title == "" || title == null || title == undefined) {
        title = encodeURIComponent(document.title);
    } else {
        title = encodeURIComponent(title);
    }

    comment = encodeURIComponent(comment.replace("[URL]", document.location));

    window.open('http://www.myspace.com/Modules/PostTo/Pages/?u=' + url + '&t=' + title + '&c=' + comment, 'sharer', 'toolbar=0,status=0,width=626,height=436');
}

function shareOnTwitter(url, title, comment) {
    if(url == "" || url == null || url == undefined) {
        url = encodeURIComponent(document.location);
    } else {
        url = encodeURIComponent(url);
    }

    if(title == "" || title == null || title == undefined) {
        title = encodeURIComponent(document.title);
    } else {
        title = encodeURIComponent(title);
    }

    comment = comment.replace("[URL]", url);

    window.open('http://twitter.com/timeline/home?status=' + comment, 'sharer', 'toolbar=0,status=0,width=600,height=500');
}