/*
 * използва
 */
/* FB.init("1973b227a2c9dd60bc5c29e778a7114e"); */

$(document).ready(function(){
    /*
     * Ако мониторчето е с по малко от 600 пиксела на височина
     * горната лента я спираме от движение :)
     */
    function resizedN(){
        if ($(window).height() < 600) {
            $('#header').css('position', 'relative');
            $('.content_wrapper').css('padding-top', '0');
        }
    }
    /*
     * Добавяме class ggodini_yubiley
     * ако широчината е по голяма от 1100 пиксела
     *
     */
    function yubiley(){
        if ($(document).width() > 1100) {
            $('body').addClass('godini_yubiley');
        }
        else {
            $('body').removeClass('godini_yubiley');
        }
    }
    
    resizedN();
    yubiley();
    
    $(window).bind('resize', function(){
        yubiley();
    });
    var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
    
    if (is_chrome == true) {
        $('#signin_menu').css('margin-left', '757px');
    }
    
    function delayTimer(delay){
        var timer;
        return function(fn){
            timer = clearTimeout(timer);
            if (fn) 
                timer = setTimeout(function(){
                    fn();
                }, delay);
            return timer;
        }
    }
    
    
    function slideToggle(el, bShow){
        var $el = $(el), height = $el.data("originalHeight"), visible = $el.is(":visible");
        
        // if the bShow isn't present, get the current visibility and reverse it
        if (arguments.length == 1) 
            bShow = !visible;
        
        // if the current visiblilty is the same as the requested state, cancel
        if (bShow == visible) 
            return false;
        
        // get the original height
        if (!height) {
            // get original height
            height = $el.show().height();
            // update the height
            $el.data("originalHeight", height);
            // if the element was hidden, hide it again
            if (!visible) 
                $el.hide().css({
                    height: 0
                });
        }
        
        // expand the knowledge (instead of slideDown/Up, use custom animation which applies fix)
        if (bShow) {
            $el.show().animate({
                height: height
            }, {
                duration: 20
            });
        }
        else {
            $el.animate({
                height: 0
            }, {
                duration: 0,
                complete: function(){
                    $el.hide();
                }
            });
        }
    }
    
    /*
     * Scrool top
     */
    function scrollWin(){
        $('html, body').animate({
            scrollTop: $("body").offset().top
        }, 2000);
    }
    $(".scroolNow").click(function(){
        scrollWin();
        return false;
    });
    /*
     * Навигация
     */
    /*
     $(".navi_").hover(function(){
     //$("#submenu").slideDown(500);
     slideToggle('.'+$(this).attr('rel'), true);
     //alert($(this).attr('rel'));
     
     }, function(){
     // slideToggle('.'+$(this).attr('rel'), false);
     });
     */
    $("#secondpane p.menu_head").mouseover(function(){
    
        $(this).next("div.menu_body").slideDown(500).siblings("div.menu_body").slideUp("slow");
        $(this).siblings();
    });
    
    // eksp.
    
    $('#s_menu > li').mouseover(function(){
        $(this).find('ul').css('display', 'block');
    });
    $('#s_menu > li').mouseout(function(){
        $(this).find('.submenu').css('display', 'none');
    });
    
    // !eksp		
    
    $('.languages img').fadeTo("slow", 0.33);
    function lang_fadeOut_current(){
        $('.languages img').each(function(){
        
            if ($(this).is('.curl_lang')) {
                $(this).fadeTo("slow", 1);
            };
            $(this).css('border', 'none');
            $(this).css('margin-left', '2px');
            
        });
    };
    lang_fadeOut_current();
    
    $('.languages img').mouseover(function(){
        $(this).fadeTo("slow", 1);
        lang_fadeOut_current();
    });
    $('.languages img').mouseout(function(){
        $(this).fadeTo("slow", 0.33);
        lang_fadeOut_current();
    });
    /*
     * Слайдшоу
     */
    $('.slideshow_top_today').cycle({
        fx: 'turnDown', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
        timeout: 5000
    });
    $('#twitter_messages').cycle({
        fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
        timeout: 50000
    });
    
    var inputDelayer = delayTimer(500);
    
    
    /*
     * Сподели!
     */
    //grab all the anchor tag with rel set to shareit
    $('a[rel=shareit], #shareit-box').mouseenter(function(){
    
        //get the height, top and calculate the left value for the sharebox
        var height = $(this).height();
        var top = $(this).offset().top;
        
        //get the left and find the center value
        var left = $(this).offset().left + ($(this).width() / 2) - ($('#shareit-box').width() / 2);
        
        //grab the href value and explode the bar symbol to grab the url and title
        //the content should be in this format url|title
        var value = $(this).attr('href').split('|');
        
        //assign the value to variables and encode it to url friendly
        var field = value[0];
        var url = encodeURIComponent(value[0]);
        var title = encodeURIComponent(value[1]);
        
        //assign the height for the header, so that the link is cover
        $('#shareit-header').height(height);
        
        //display the box
        $('#shareit-box').show();
        
        //set the position, the box should appear under the link and centered
        $('#shareit-box').css({
            'top': top,
            'left': left
        });
        
        //assign the url to the textfield
        $('#shareit-field').val(field);
        
        //make the bookmark media open in new tab/window
        $('a.shareit-sm').attr('target', '_blank');
        
        //Setup the bookmark media url and title
        $('a[rel=shareit-mail]').attr('href', 'http://mailto:?subject=' + title);
        $('a[rel=shareit-delicious]').attr('href', 'http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=' + url + '&title=' + title);
        $('a[rel=shareit-designfloat]').attr('href', 'http://www.designfloat.com/submit.php?url=' + url + '&amp;title=' + title);
        $('a[rel=shareit-digg]').attr('href', 'http://digg.com/submit?phase=2&amp;url=' + url + '&amp;title=' + title);
        $('a[rel=shareit-stumbleupon]').attr('href', 'http://www.stumbleupon.com/submit?url=' + url + '&title=' + title);
        $('a[rel=shareit-twitter]').attr('href', 'http://twitter.com/home?status=' + title + '%20-%20' + title);
        
    });
    
    //onmouse out hide the shareit box
    $('#shareit-box').mouseleave(function(){
        $('#shareit-field').val('');
        $(this).hide();
    });
    
    //hightlight the textfield on click event
    $('#shareit-field').click(function(){
        $(this).select();
    });
    
    // партньори
    $(".partners_logo").fadeTo('fast', '0.45').mouseover(function(){
        $(this).fadeTo('slow', 1);
    }).mouseout(function(){
        $(this).fadeTo('slow', '0.45');
    });
    
    /*
     * Login
     */
    $(".signin").click(function(e){
        e.preventDefault();
        $("fieldset#signin_menu").toggle();
        $(".signin").toggleClass("menu-open");
    });
    
    $("fieldset#signin_menu").mouseup(function(){
        return false
    });
    $(document).mouseup(function(e){
        if ($(e.target).parent("a.signin").length == 0) {
            $(".signin").removeClass("menu-open");
            $("fieldset#signin_menu").hide();
        }
    });
    
    $('.akcent').css('height', '288px');
    /*
     * Анкети
     * Гласуваме и показваме резултата
     */
    $('#submit_anketi_').click(function(){
        var cheked = 0;
        $('.select_pool_opt').each(function(i){
            if ($(this).attr('checked')) {
                $('.anketi').find('.content').hide();
                $('.anketi').find('.buttons').hide();
                $('.anketi').find('.rezult').show();
                
                $.post("index.php/bg/ajax/pools/vote", {
                    voted: $(this).val()
                }, function(data){
                    jQuery.each(data, function(i, app){
                        html = '<div class="rezult_opt">' + app['name'] + ' - ' + app['rezult'] + ' ' + app['text_'] + ' <br /><div class="rezult_opt_graf" style="background:' + app['color'] + ';width:' + app['procent'] + '%"></div></div>';
                        $('.anketi').find('.rezult').append(html);
                    })
                }, "json");
            }
            
        });
        return false;
    });
    /*
     * Some css fixes
     */
    $('.facebook_home').css('text-align', 'left');
    /**
     * IE7 Fixes
     */
    if ($.browser.msie == true && $.browser.version == '7.0') {
    
        // DD_roundies.addRule('#main_content', '4px');
        $('.widget_spectacles_top').css('margin', '0px');
        $('.widget_spectacles_top').css('margin-left', '2px');
        $('.widget_spectacles_top').css('margin-top', '4px');
        
        $('#main_content').css('margin-top', '-230px');
        $('#navigation_current').css('width', '170px');
        
        //header
        $('#header').css('margin-left', '-138px');
        $('#header').css('width', ($('#header').width() + 10));
        
        $(".content_wrapper > .right").css('margin', '0px');
        $('.curl_lang').css('border', '0px');
        $('.languages').css('margin-top', '-40px');
        $('.anketi').css('border', '1px solid black').css('background', 'white');
        
        $('.leftcurtain').remove();
        $('.rightcurtain').remove();
    }
    
    if ($.browser.msie == true && $.browser.version == '6.0') {
        // DD_roundies.addRule('#main_content', '4px');
        $('.widget_spectacles_top').css('margin', '0px');
        $('.widget_spectacles_top').css('margin-left', '0px');
        $('.widget_spectacles_top').css('margin-top', '4px');
        
        $('#main_content').css('margin-top', '-610px');
        $('#navigation_current').css('width', '170px');
        $('#breadcumbs').css('margin-top', '-100px');
        //header
        //		$('#header').css('margin-left','-138px');
        //		$('#header').css('width', ($('#header').width() + 10 ) );
        
        //		$(".content_wrapper > .right").css('margin','0px');
        //		$('.curl_lang').css('border','0px');
        //		$('.languages').css('margin-top','-40px');
        $('.anketi').css('border', '1px solid black').css('background', 'white');
        
        //		$('.leftcurtain').remove();
        //		$('.rightcurtain').remove();
    }
    
    /*
     * Opera fixes
     */
    if ($.browser.opera == true) {
        $('.leftcurtain').remove();
        $('.rightcurtain').remove();
    }
    
    /* animaci
     *
     */
    $curtainopen = false;
    
    function ope_n(){
        if ($curtainopen == false) {
            $(".leftcurtain").stop().animate({
                width: '10px'
            }, 2000);
            $(".rightcurtain").stop().animate({
                width: '10px'
            }, 2000);
            $curtainopen = true;
        }
        else {
            $(".leftcurtain").stop().animate({
                width: '50%'
            }, 2000);
            $(".rightcurtain").stop().animate({
                width: '51%'
            }, 2000);
            $curtainopen = false;
        }
    };
    $('.akcent').click(function(){
        ope_n();
    });
    
    setTimeout(function(){
        ope_n();
    }, 5000);

//Cufon.replace('#main_content > .title',{fontSize:'18'});
});