// Inicia��o do modelo

var intervalId = 0;
var arrIndex = 0;

var viewer = new Object();
viewer.init = function() {


    viewer.CloseRecomendar = function() {

        $("label.error").remove();
        $("#recomende_popup").animate({ height: '0px' }, 500);
    }


    viewer.OpenRecomendar = function() {
        //$(".recomende-popup").css("display", "block");
        $(".submit > label").html(ressources["fillallfields."]);
        $("#recomende_popup").animate({ height: '220px' }, 500);
    }


    viewer.UpdateFormRecomendationAfterSend = function(message) {

        $(".submit > label").css("width", "110px");
        $(".submit > label").html(message);
        $("#emailFrom").val("");
        $("#emailTo").val("");

    }


    viewer.FormRecomendationShowLoader = function(message) {
        $(".submit > label").text(ressources["Sending"]+"...");
        $(".submit > label").css("display", "block");
    }


    // ///////////////////////////////////////// Dark Area //////////////////////////////////////////////


    viewer.LoadHtmlForDarkContent = function(html, handler) {
        $("#contentLeftMenu #imgBGA").remove();
        $("#contentLeftMenu").html(html);
        $("#contentLeftMenu").children(".darkBg").next().css("marginLeft", "40px");
        // viewer.LoadSideImage("#contentLeftMenu");
        handler();
    }


    viewer.AnimateForDarkContent = function(handler) {


        $("#contentLeftMenu").children(".darkBg").next().css("marginLeft", "-440px");

        // if ($("#contentLeftMenu").find(".jScrollPaneContainer.jScrollPaneScrollable").html() == null)
        $("#contentLeftMenu").children(".darkBg").next().animate({ marginLeft: '40px' }, 500);
        // else
        //  $("#contentLeftMenu").children(".darkBg").next().animate({ marginLeft: '0px' }, 500);

        $("#contentLeftMenu").animate({ width: '440px', marginLeft: '10px' }, 500, handler);
    }

    viewer.HideDarkContent = function(handler) {
        // alert("entrou");
        $("#contentLeftMenu").animate({ width: '0px', marginLeft: '0px' }, 500, handler);
    }


    // ///////////////////////////////////////// Dark Area //////////////////////////////////////////////



    // ///////////////////////////////////////// White Area //////////////////////////////////////////////





    // ///////////////////////////////////////// White Area //////////////////////////////////////////////


    viewer.LoadHtmlForWhiteContent = function(html, handler) {

        $("#conteMain #imgBGA").remove();
        $("#conteMain").html(html);
        //viewer.LoadSideImage("#conteMain");
        handler();
        //  if ($(".video-js-box").size() == 0) {
        $("#conteMain").children(".whiteBg").next().css("marginLeft", '40px');
        //}
        //else { $('#conteMain').removeClass('scrollNoPadd'); }
    }

    // Alberto -> Agora faz fade entre as várias imagens de fundo 
    viewer.LoadMultipleSideImage = function(arrImg, style) {
		
        //Reset the counter and the interval
        clearInterval(intervalId);
        arrIndex = 0;

        //If more than one image, preload all images and start animation
        if (arrImg.length > 1) {
            preloadImages(arrImg);
            viewer.LoadSideImage(arrImg[0], style);
            arrIndex++
			
            intervalId = setInterval(function() {LoadInterval(arrImg, style)}, 5000);
        } else {
            //If only one image, just load the image without animation
            if (arrImg.length > 0) {
                viewer.LoadSideImage(arrImg[0], style);
            }
            //If no image, just let the last backgound loaded.
        }


    }
    //Preload of background for smooth animation
    function preloadImages(arrImg) {
        var cache = [];


        var args_len = arrImg.length;
        for (var i = args_len; i--; ) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arrImg[i];
            cache.push(cacheImage);
        }


    }

    //Function that is called in the setInterval, loops through the array and calls the original function that fades the background (LoadSideImage)
    function LoadInterval(arrImg, style) {
		
        viewer.LoadSideImage(arrImg[arrIndex], style);
        if (arrIndex >= arrImg.length - 1) {
            arrIndex = 0;
        } else {
            arrIndex++;
        }
    }



    viewer.LoadSideImage = function(newImg, style) {

	
        var aux = "url('" + newImg + "')";
        var x = $("<div></div>");

        $(x).css("backgroundImage", aux);

        var auxx = $("#avilez").css("backgroundImage");
        aux = $(x).css("backgroundImage");

        var auxComp = aux.substring(aux.lastIndexOf("/"), aux.length).replace(")", "");
        var auxxComp = auxx.substring(auxx.lastIndexOf("/"), auxx.length).replace(")", "");

        // alert(auxComp + "::" + auxxComp);

        if (auxComp != undefined && auxComp != "url(undefined" && auxComp != "" && auxComp != auxxComp) {
            $("#avilez").fadeOut(500, function() {
                $("#avilez").css("backgroundImage", aux);
                $("#avilez").fadeIn(500);
            });
        }
        //alert($("#avilez").css("backgroundPosition"));
        if (style != undefined && !$("#avilez").hasClass(style)) {
            $("#avilez").fadeOut(500, function() {
                // $("#avilez").css("backgroundPosition", style);
                $("#avilez").removeClass().addClass(style);
                $("#avilez").fadeIn(500);
            });
        }
    }


    viewer.AnimateForWhiteContent = function(handler) {

        $("#conteMain").children(".whiteBg").next().css("marginLeft", "-400px");

        jQuery.each(jQuery.browser, function(i) {
            if ($.browser.msie) {

                //if (($(".jScrollPaneContainer").size() + $(".scroll-pane.scrollerRight.scrollValRight").size() < 2) && $("#conteMain").find(".jScrollPaneContainer.jScrollPaneScrollable").html() == null && model.page.GetRelativeUrl() != "/jose-avillez/inspiracoes") {
                //  if ($(".video-js-box").size() == 0)
                $("#conteMain").children(".whiteBg").next().css("marginLeft", '40px');
                // else
                //    $("#conteMain").children(".whiteBg").next().css("marginLeft", '0px');
                //}
                //else {
                //$("#conteMain").children(".whiteBg").next().css("marginLeft", '0px');
                //}
            }
            else {
                //if (($(".jScrollPaneContainer").size() + $(".scroll-pane.scrollerRight.scrollValRight").size() < 2) && $("#conteMain").find(".jScrollPaneContainer.jScrollPaneScrollable").html() == null && model.page.GetRelativeUrl() != "/jose-avillez/inspiracoes") {
                //if ($(".video-js-box").size() == 0)
                $("#conteMain").children(".whiteBg").next().animate({ marginLeft: '40px' }, 500);
                //else
                //   $("#conteMain").children(".whiteBg").next().animate({ marginLeft: '0px' }, 500);
                //}
                //else {
                //  $("#conteMain").children(".whiteBg").next().animate({ marginLeft: '0px' }, 500);
                //}
            }

        });


        $("#conteMain").animate({ width: '440px', marginLeft: '10px' }, 500, handler);
    }

    viewer.HideWhiteContent = function(handler) {

        $("#conteMain").animate({ width: '0px', marginLeft: '0px' }, 500, handler);
    }

    ///////////////////////////// Mudar Menu selecionado //////////////////////////////////////////////



    viewer.SetParentOn = function(parent) {
	$(".menu.preto.small > li").each(
    		function(index)
    			{
       			 $(this).removeClass("on")
    			});
        $(parent).addClass("on");
    }

    viewer.ChangeOn = function(hashUrl) {
       
        //hashUrl += ".aspx";
        var aux = $('a[link|=' + hashUrl + ']').parent();
       
        viewer.OffMenu();
        viewer.OnMenu(aux);
    }


    viewer.OnMenu = function(elem) {
        $(elem).addClass("on");
    }


    viewer.OffMenu = function() {

        $(".menu.cinza > li").removeClass("on");
        $(".menu.preto > li").removeClass("on");
    }

    ///////////////////////////// End Mudar Menu selecionado //////////////////////////////////////////////


    viewer.HideLeftMenu = function(handler) {
		$("#homeBtn").css('display','block');
		$("#homeBtn").animate({ left: '0px' }, 500);		
        $("#leftMenu").animate({ width: '0px', marginLeft: '0px' }, 500, handler);
		
    }

    viewer.ShowLeftMenu = function(handler) {
		$("#homeBtn").animate({ left: '-30px' }, 500, function(){$("#homeBtn").css('display','none')});	
        $("#leftMenu").animate({ width: '200px', marginLeft: '10px' }, 500, handler);
    }

    viewer.HideSubmenu = function(handler) {
        $("#submenu").animate({ width: '0px', marginLeft: '0px' }, 500, handler);
    }

    viewer.ShowSubmenu = function(html, handler) {

        $("#submenu").html(html);
        $("#submenu").animate({ width: '200px', marginLeft: '10px' }, 500, handler);
    }



    ////////////////////////////////////////////////////////////////////////////    

	
	 viewer.destqHome = function(show) {
		 if (show && $("#destq > img").attr("src")!="")
		 {
			$("#destq").delay(500).fadeIn();
		 } else {
			$("#destq").fadeOut();
		 }
	}

}
