/* 
 
Filename: init.js
Author: 33Delivered 
Website: Http://www.33delivered.com
Last Modified: 1.21.2011

Description: This file initializes all of the carousels found in the Stockland Martel templates. On page load, the MAIN navigation is loaded via an AJAX call, so that it can be kept as an include--Doesn't have to be 
updated on every page each time a change takes place. After the navigation is loaded, a callback is then called, which appeds a "tab" div to the sm_content div (this is the white overlay over the content area when hovering 
over a top-level navigation item when the image flys out).

*/


//1.21.2011 - force preload unmask after this amount of time
SM_PRELOAD_TIMEOUT = 20000;

//1.21.2011 - global flags
SM_MASK_T_FLG = false;
SM_MASK_T_OBJ = null;
SM_MASK_STATE = false;


$(function () {


    //sequential loading - hide all main parts of the page until they are done loading
    //(note: #subNavigation, #sm_content img  are set in css sheets because their content is already part of the DOM)

    //these are here cause they are ajax'd in
    $("#header").css("visibility", "hidden");
    $("#mainNavigation").css("visibility", "hidden");


    // preload the 'loading' image

    //1.21.2011 - removing callback to force mask now
    $.preloadImgPath(['/images/ajax-loader-2.gif', '/images/loading.png']);
    force_preload_mask();

    /*
    $.preloadImgPath(['/images/ajax-loader-2.gif','/images/loading.png'],function(){
			
    //1.21.2011 
    force_preload_mask();
			
    //$("#siteWrap").mask("<img src='/images/loading.png' />");
					
    });	*/


    //preload background images
    $.preloadSMImgs();

    //load header first				
    $.ajax({
        async: false, //so we can load sequentially 
        url: '/home/GetHeader',
        success: function (data) {

            //preload all images in sm_mainnavigation BEFORE its added to the DOM				
            $('img', data).preloadImg(function () {

                $('#header').html(data);

                //NOTE: must include modal script here cause elements are in header
                sm_inc_script('modal.js');
            });
        }
    });

    //load navigation
    $.ajax({
        async: false,
        url: '/home/GetMenu',
        success: function (data) {

            $('#mainNavigation').html(data);

            $('<div id="tab"><img src="/images/tab.png"></div>').insertAfter('#mainNavigation');
            $("#mainNavigation div ul li").addNavImages("<img src=''></img>");
            $("#tab").css('height', $("#contentWrap").css("height"));
            $("#tab").css('width', $("#contentWrap").css("width"));


            var menuConfig = {
                over: showmenu,
                timeout: 0,
                out: hidemenu
            };
            $("#mainNavigation ul li").hoverIntent(menuConfig);

            //support for sticky navs
            $("#mainNavigation ul li").click(function () {


                //case where there are stickies and the current one is sticky
                if ($.areAnySticky() && $(this).getStickyState()) {
                    $("#tab").hide();
                    $(this).setStickyOff();
                }

                //case where there are stickies and the current one is not sticky
                else if ($.areAnySticky() && !$(this).getStickyState()) {
                    $.getStickyElement().setStickyOff();
                    $(this).setStickyOn();

                }
                //case where there are no stickies - set current to sticky
                else if (!$.areAnySticky()) {
                    $("#tab").show();
                    $(this).setStickyOn();

                    //support for click-out 
                    var nav_elm = $(this);
                    $("#tab").click(function () {
                        $("#tab").hide();
                        nav_elm.setStickyOff();
                    });


                }

            });

            var config = {
                over: showpic, // function = onMouseOver callback (REQUIRED)  
                //1.22.2011 - dropshadow fix (was 1200)
                timeout: 0, // number = milliseconds delay before onMouseOut    
                out: hidepic // function = onMouseOut callback (REQUIRED)    
            };
            $("#mainNavigation ul li ul li").hoverIntent(config);

            //1.22.2011 - dropshadow fix
            /*
            $("#mainNavigation ul li ul li").mouseout(function() { 
            $(this).find("img").css("display","none");											   
            });
            */

            $('#mainNavigation div').hover(function () { if (!$.areAnySticky()) { $("#tab").show(); } }, function () { if (!$.areAnySticky()) { $("#tab").hide(); } });


        }
    });

    //1.21.2011	
    start_preload_timer();

    //preload all images that are in the current page
    $('img').preloadImg(function () {
        init_carousels();

        //1.21.2011
        force_preload_unmask();

        //unmask and set visible 
        /*
        $("#siteWrap").unmask();					
						
        $("#header").css('visibility','visible');		    
        $("#mainNavigation").css("visibility","visible");		    
        $("#subNavigation").css("visibility","visible");
        $("#sm_content img").css("display","block");
        */

    });

});



//1.21.2011 - function to mask the preloader
function force_preload_mask() {
    $("#siteWrap").mask("<img src='/images/loading.png' />");
    SM_MASK_STATE = true;
}

//1.21.2011 - function to start countdown 
function start_preload_timer() {
    SM_MASK_T_OBJ = setTimeout("handle_preload_timeout()", SM_PRELOAD_TIMEOUT);
    SM_MASK_T_FLG = true;
}

//1.21.2011 - function to execute after timeout has been triggered
function handle_preload_timeout() {
    SM_MASK_T_FLG = false;
    force_preload_unmask();
}

//1.21.2011 - function to be called to unmask the preloader
function force_preload_unmask() {
    if (SM_MASK_STATE === true) {
        //be sure we only unmask once
        SM_MASK_STATE = false;

        //unmask and set visible 
        $("#siteWrap").unmask();

        $("#header").css('visibility', 'visible');
        $("#mainNavigation").css("visibility", "visible");
        $("#subNavigation").css("visibility", "visible");
        $("#sm_content img").css("display", "block");
    }

    //this will execute if the timeout hasnt occured yet
    if (SM_MASK_T_FLG === true) {
        clearTimeout(SM_MASK_T_OBJ);
        SM_MASK_T_FLG = false;
    }

    return;

}


//this is used to set links within arrays to their correct gallery
$.applyArrayImageMapping = function () {

    $(".scrollableArea a").each(function () {


        var img_name = baseName($(this).find("img").attr("src"));
        var org_href = $(this).attr("href");

        $(this).attr("href", org_href + "?p=" + img_name);
    });


};


function init_carousels() {

    //initialize drop shadows
    $(".scrollableArea a").each(function (index) {
        var img_elm = $(this).children('img');
        $(this).wrap(function () {
            return '<div class="ds_outer1_topright"><div class="ds_outer2_bottomleft"><div class="ds_inner1_shadow"><div class="ds_inner2_image" style="height: ' + img_elm.height() + 'px; width: ' + img_elm.width() + 'px;">';

        });
    });

    $("div#makeMeScrollable").smoothDivScroll({ autoScroll: "always", autoScrollDirection: "endlessloopright", autoScrollStep: 1, autoScrollInterval: 15, visibleHotSpots: "" });
    $("div#makeMeScrollable a").addImageCaptions("<span></span>");
    $("div#makeMeScrollable2").smoothDivScroll({ autoScroll: "always", autoScrollDirection: "endlessloopright", autoScrollStep: 1, autoScrollInterval: 15, visibleHotSpots: "" });
    $("div#makeMeScrollable2 a").addImageCaptions("<span></span>");

    $('.hoverport').jScrollPane({ showArrows: true });
    $(".hovercat a").hover(function () {
        $(".hovercat p").removeClass("active");
        active_tabs = $(this).attr("rel");
        tab_array = active_tabs.split(",");
        for (i = 0; i < tab_array.length; i++) {
            port = tab_array[i];
            $(".hoverport").find("." + port).addClass("active");
        }
    }, function () {
        $(".hoverport p").removeClass("active");
    });



    //disable for IE < v9
    jQuery.each(jQuery.browser, function (i) {
        if ($.browser.msie && parseInt($.browser.version) < 9) {
            //do nothing
        } else {
            $("div#sm_content").setMouseListener();
        }
    });

}
