var beeState = false;

$(document).ready(
    function(){
        addHoverEvent(".main-menu-element");
        addHoverEvent("#login-button");
        addHoverEvent("#register-button");
        addHoverEvent(".read-more");
        resetTitle();
        addHoverGoUp("#facebook-button");
        addHoverGoUp("#twitter-button");
        addHoverEvent("#get-more-information");
        addHoverEvent(".add-item-form.el04");
        addHoverEvent(".payments-container-bottom-footer-button");
        addHoverEvent(".content-tab-arrow");
        addHoverEvent(".inner-tab-element");
        addHoverMenuElement();
        setActiveMenuElement();
        setClickEventInnerTabMenu();
        addActionsToArrows();
        addCookieEvent();
        //aha();
    }
    
);
function addCookieEvent() {
	$('input.el04').click(function() {
		
		$.cookie('firstElementItem', $('input.el01').val(), {expires: 7, path: '/'});
		$.cookie('firstElementQuantity', $('input.el02').val(), {expires: 7, path: '/'});
		$.cookie('firstElementCost', $('input.el03').val(), {expires: 7, path: '/'});
	});
}	

function getRandomComment(lang) {
    $.ajax({
        type: "POST",
        data: "lang="+lang,
        url: 'apps/drobeeCms/comments.php',
        success: function(data) {
            $("#comments").html(data);
        }
    });
}

function setTabsCapacities() {
    $(".content-tab").each(
        function(e){
           a = $(".content-tab:nth-child(" + e + ") .inner-tab-content-element").html();
           alert(a);
            
        }
    );
}

function aha(){
    $(document).mousemove(function(e){
        if(beeState){
            $("#cage").attr("title", "kliknij, niech pszczoła odpocznie");
            $('#bee').animate(
                {
                    left: (e.pageX+20),
                    top: (e.pageY+5)
                }, 5
            );
        }else{
            $("#cage").attr("title", "wyprowadź pszczołę na spacer");
        }
    });

//    $(document).click(function(e){
//        if(beeState){
//            $("#cage").attr("title", "kliknij, niech pszczoła odpocznie");
//            $('#bee').animate(
//                {
//                    left: (e.pageX+20),
//                    top: (e.pageY+5)
//                }, 2000
//            );
//        }else{
//            $("#cage").attr("title", "wyprowadź pszczołę na spacer");
//        }
//    });

    $("#cage").click(
        function(){
            if(beeState){
               beeState = false;
               $('#bee').animate(
                    {
                        left: "35",
                        top: "40"
                    }, 500
                );
            }else{
                beeState = true;
            }
        }
    );
}

function setClickEventInnerTabMenu() {
    $(".content-tab:visible .inner-tab-element").live('click', function() {
        triggerActionForInnerTab(this);
    });
}

function triggerActionForInnerTab(selector){
    var arrowLeft = $(".content-tab-arrow.left");
    var arrowRight = $(".content-tab-arrow.right");
    $(".content-tab:visible .inner-tab-element.active").removeClass("active");
    $(selector).addClass("active");
    var activeIndex = $(".content-tab:visible .inner-tab-element").index(selector) + 1;
    var max =  $(".content-tab:visible .inner-tab-element").length;
    
//    addClass("disabled");
//    removeClass("disabled");

    arrowLeft.removeClass("disabled");
    arrowRight.removeClass("disabled");

    if (activeIndex == max) {
        arrowRight.addClass("disabled");
    }
    if(activeIndex == 1) {
        arrowLeft.addClass("disabled");
    }

    if(max == 0) {
        arrowLeft.addClass("disabled");
        arrowRight.addClass("disabled");
    }

    $(".content-tab:visible .inner-tab-content .inner-tab-content-element:visible").fadeOut();
    $(".content-tab:visible .inner-tab-content .inner-tab-content-element:nth-child(" + activeIndex + ")").fadeIn();


}

function setActiveInnerTab(no) {
    var max = $(".content-tab:visible .inner-tab-element").length;
    if (no > 0 && no <= max){
        triggerActionForInnerTab($(".content-tab:visible .inner-tab-element:nth-child(" + (no*2-1) + ")"));
    }
}

function leftArrowAction() {
    var activeTab = $(".content-tab:visible .inner-tab-element.active");
    var index = $(".content-tab:visible .inner-tab-element").index(activeTab) + 1;
    setActiveInnerTab(index-1);
}

function rightArrowAction() {
    var activeTab = $(".content-tab:visible .inner-tab-element.active");
    var index = $(".content-tab:visible .inner-tab-element").index(activeTab) + 1;
    setActiveInnerTab(index+1);
}

function addActionsToArrows() {
    $(".content-tab-arrow.left").click(
        function() {
            leftArrowAction();
        }
    );
    $(".content-tab-arrow.right").click(
        function() {
            rightArrowAction();
        }
    );
}

function setActiveMenuElement() {
    moveToActiveElement(".left-menu li");
    $(".left-menu li:first-child").trigger("click");
}

function moveToActiveElement(selector) {
    $(selector).click(
        function(){
            var index = $(selector).index(this) + 3;
            var pos = $(this).position();
            var text = $(this).html();
            $("#left-menu-active-element span").fadeOut();
            $("#left-menu-active-element").animate(
                {
                    left: pos.left,
                    top: pos.top
                }, 500, function(){
                    $("#left-menu-active-element span").html(text);
                    $("#left-menu-active-element span").fadeIn("fast");
                }
            );

            //$("#left-menu-active-element").css("left", pos.left);
            //$("#left-menu-active-element").css("top", pos.top);
            //$("#left-menu-active-element").html(text);

            $(".content-tab:visible").fadeOut("slow");
            $(".content-tab:nth-child(" + index + ")").fadeIn("slow");
            $(".content-tab-arrow.right").show();

            //wyswietlanie odpowiedniej ilosci zakladek
            var tabsLength = $(".content-tab:nth-child(" + index + ") .inner-tab-content-element").length;
            if (tabsLength > 0) {
                tabsString = '<div class="inner-tab-element ite1"></div>';
                for(i = 2; i <= tabsLength; i++){
                    tabsString += '</div><div class="inner-tab-arrow"></div><div class="inner-tab-element ite' + i + '">';
                }
                $(".content-tab:nth-child(" + index + ") .inner-tab-menu").html(tabsString);
            }
            
            setActiveInnerTab(1);
        }
    );
}

function addHoverGoUp(selector) {
    $(selector).mouseover(
        function() {
            $(selector + "-shadow").fadeOut("500");
            $(selector + "-shadow2").fadeIn("500");
            $(selector).animate(
                {
                    top: '-40px',
                    height: '103px'
                }, {
                    duration: 500,
                    specialEasing: {
                      width: 'linear',
                      height: 'easeOutBounce'
                    }
                  }
            );
        }
    );
    $(selector).mouseout(
        function() {
            $(selector + "-shadow").fadeIn("500");
            $(selector + "-shadow2").fadeOut("500");
            $(selector).animate(
                {
                    top: '-22px',
                    height: '100px'
                }, {
                    duration: 500,
                    specialEasing: {
                      width: 'linear',
                      height: 'easeOutBounce'
                    }
                  }
            );
        }
    );
}

function resetTitle(){
    $(".add-item-form").each(
        function() {
            if ($(this).attr("resetText")) {
                var txt = $(this).attr("resetText");
                $(this).focus(
                    function() {
                        ($(this).val() == txt) ? $(this).val("") : $(this).val();
                    }
                );
                $(this).blur(
                    function() {
                        ($(this).val() == "") ? $(this).val(txt) : $(this).val();
                    }
                );
            }
        }
    );
}

function addHoverEvent(selector){
    $(selector).mouseover(
        function(){
            $(this).addClass("hover");
        }
    );

    $(selector).mouseout(
        function(){
            $(this).removeClass("hover");
        }
    );
}

function addHoverMenuElement() {
    $(".left-menu li").mouseover(
        function(){
            $(this).addClass("hover");
            if($(this).html() == $(".left-menu li:last-child").html()) {
                $(".left-menu-bottom-line").addClass("hover");
            }else{
                $(".left-menu-bottom-line").removeClass("hover");
            }
        }
    );

    $(".left-menu li").mouseout(
        function(){
            $(this).removeClass("hover");
            $(".left-menu-bottom-line").removeClass("hover");
        }
    );
}


