﻿$(function () {
    $(".imagesBar").each(function () {
        new ImagesBar(this);
    }
    )

    $("tr.body > td").height($(window).height() - 284)

    $(".leftNavigation li.firstLevel div.title").hover(
        function () {
            $(this).parents("li.firstLevel").addClass("hoverFirstLevel");
        },
        function () {
            $(this).parents("li.firstLevel").removeClass("hoverFirstLevel");
        }
    );

    $(".leftNavigation ul.secondLevel li div.titleLi").hover(
        function () {
            $($(this).parents("li")[0]).addClass("hoverSecondLevel")

        },
        function () {
            $($(this).parents("li")[0]).removeClass("hoverSecondLevel");
        }
    );

    $(".leftNavigation ul.thirdLevel li").hover(
        function () {
            $($(this).parents("ul.secondLevel").find("div.titleLi")).addClass("inactive");
            $(this).addClass("hoverThirdLevel");
        },
        function () {
            $(this).removeClass("hoverThirdLevel");
        }
    );



    $("tr.topNavigation table td").hover(
        function () {
            if ($(this).hasClass("news")) {
                $(this).addClass("newsHover");
            }
            else {
                $(this).addClass("hoverButton");
            }
        },
        function () {
            if ($(this).hasClass("news")) {
                $(this).removeClass("newsHover");
            }
            else {
                $(this).removeClass("hoverButton");
            }
        }
    );


    $("tr.topNavigation table td").click(
    function () {
        document.location.href = $(this).find("input").val();
    });

    $("img[showPopup='on']")
        .click(
            function () {
                //debugger;alert(12);
                var bigFotoUrl = this.src.slice(0, this.src.lastIndexOf(".")) + "Big.jpg";

                window.open(bigFotoUrl, '', 'status=0, resizable=0, scrollbars=0, menubar=0, toolbar=0, width=699, height=500');
            }
        )
        .css(
            { cursor: 'pointer' }
        );

    $(".link").click(
        function () {
            if ($(this).find("input").val() != "http://www.sexualeducation.org.ua") {
                document.location.href = $(this).find("input").val();
            }
            else {
                window.open($(this).find("input").val(), "_blank");
            }

            if (this.tagName == "LI") {
                saveState(this);
            }
            else {
                saveState($(this).parents("li")[0]);
            }
            return true;
        });




    $("li:has(ul)").click(function (event) {
        if ($($(event.target).parents("li")[0]).hasClass("link")) {
            return true;
        }
        if ($(this).find(".title").html() == $(event.target).html() || $(this).find(".titleLi").html() == $(event.target).html()) {
            if ($(this).children('ul').is(':hidden')) {
                $(this).children('ul').slideDown();
                if ($(this).hasClass("firstLevel")) {
                    $(this).addClass("openedFirstLevel");
                }
                else {
                    $(this).addClass("openedSecondLevel");
                }
            }
            else {
                $(this).children('ul').slideUp();
                $(this).removeClass("openedFirstLevel");
                $(this).removeClass("openedSecondLevel");
            }
        }
        return false;
    })

            .css({ cursor: 'pointer' })
            .children('ul').hide();

    $(window).resize(function () {
        $("tr.body > td").height($(window).height() - 285);
    });
    changeFoto(2);
    restoreState();
})

var t;
var counter = 2
function changeFoto(fotoNumber)
{
    var fotoNumber;
    
    $(".fotoConteiner").fadeOut(1000, function()
    {
        var url = $(".fotoConteiner img").attr("src");
        
        $(".fotoConteiner img").attr("src", "images/Foto" + fotoNumber + ".jpg")
        
        $(".fotoConteiner").fadeIn(5000);
    })
    counter++
    fotoNumber = counter%15;
    t=setTimeout("changeFoto("+fotoNumber+")", 12000);  
}

function saveState(button)
{    
    if(button.id!=null)
    {
        $.cookie("activeButton", button.id,{expires: null, path: "/womanhealth"});
                                            }
    var openedItems = ""
    $("ul.mainMenu li").each(function()
    {
        if($(this).hasClass("openedFirstLevel")||$(this).hasClass("openedSecondLevel"))
        {
            openedItems += this.id + ",";
        }
    });
    $.cookie("openedItems", openedItems.slice(0, openedItems.length-1));
    return true
}

function restoreState()
{        
    setActive($.cookie("activeButton"));
    setOpened($.cookie("openedItems"));
}

function setActive(id)
{
    if(id!=null)
    {
        if(id.substr(0,4)=="lnTl")
        {
            $("#" + id).addClass("active");
            return
        }
        if(id.substr(0,4)=="lnSl")
        {
            $("#" + id).addClass("active");
            return
        }
        if(id.substr(0,4)=="lnFl")
        {
            $("#" + id).addClass("active");
            return
        }
    }
}

function setOpened(openedItemsId)
{
    if(openedItemsId!=null)
    {
        var openedItems = openedItemsId.split(',');
        $.each(openedItems, function()
        {
            if(this.toString().substr(0,4)=="lnFl")
            {
                $("#" + this).addClass("openedFirstLevel");
            }
            if(this.toString().substr(0,4)=="lnSl")
            {
                $("#" + this).addClass("openedSecondLevel");
            }
            if ($("#" + this).children('ul').is(':hidden'))
            { 
               $("#" + this).children('ul').show();
            }
        })
    }
}

function ImagesBar(table)
{
    var images = new Array();
    $(table).find("img[showpopup='on']").each(function()
    {
        images.push($(this).attr("src"));
    })
    		
	
    var render = function()
	{
		var i  = 0;
		$(table).find("img[showpopup='on']").each(function()
		{
			$(this).attr("src", images[i])
			i++
		})
	}
	
	var moveArrayLeft = function()
	{
		var temp = images[0];
		for(var i = 0; i < images.length-1; i++)
		{
			images[i]=images[i+1];
		}
		images[images.length-1] = temp;
	}
	var moveArrayRight = function()
	{
		var temp = images[images.length-1];
		for(var i = images.length-1; i > 0; i--)
		{
			images[i]=images[i-1];
		}
		images[0] = temp;
	}
	
	$(table).find(".leftButtonImageBar").click(function(){moveArrayLeft(); render()})
	$(table).find(".rightButtonImageBar").click(function(){moveArrayRight(); render()})
}




