/*================================================== GLOBAL JAVASCRIPT */
/*
    Javascript Document for Savills
    Build: Pete Robelou on behalf of SAS - 06/10/2009
*/

//To avoid conflict with other libraries (Prototype)
jQuery.noConflict();

jQuery('html').addClass('js');

//Shadowbox setup
Shadowbox.loadSkin('classic', '/_js/shadowbox/skin'); // use the "classic" skin
Shadowbox.loadLanguage('en', '/_js/shadowbox/lang'); // use the English language
Shadowbox.loadPlayer(['img', 'iframe'], '/_js/shadowbox/player'); // use iframe player

//Global variables
var header_supportnav_timer;
var header_supportnav_timer_duration = 300;
var expand_speed = 250;

jQuery(document).ready(function() {
    /*////////////////////////////////////// INIT */
    //------------------------ FIX IE6 BACKGROUND IMAGE CACHING PROBLEM
    if (jQuery.browser.msie) {
        try {
            document.execCommand("BackgroundImageCache", false, true);
        } catch (err) { }
    }

    // MAIN MENU SELECTOR
    // var location_sub = /\/([^\/]+?)\/(([^\/]+?)\.aspx)?/;
    // var address = location_sub.exec(window.location.href.split('/').slice(3).join('/'));
    var address = (window.location.href.split(/[\#\?]/)[0].split('/').slice(3));

    jQuery.each(jQuery('#header_navigation_container ul a, .vx_menu a'), function() {
        try {
            var location = this.href.split('/').slice(3);
            // if the location and address are the same
            // if the beginning folder is the same and there is no second page
            if (address[0] !== '' && (location.join('/') == address.join('/') || (location[0] == address[0] && !location[1]))) {
                var active_elements = jQuery(this).parents().filter('li').addClass('current').addClass('sub_level');
            }
        } catch (address_error) { }
    });


    // INITIALISE EXPANDABLE MODULES
    InitExpandableModules();
    if (document.body.className == 'home_page') {
        jQuery("#accordion").accordion({ active: false, collapsible: true, autoHeight: false, header: '.vx_block > .expand_link' });
        if (jQuery.cookie('home_accordion') !== null) {
            try {
                jQuery("#accordion").accordion("activate", jQuery('div.' + jQuery.cookie('home_accordion')));
                bar_header = jQuery('div.' + jQuery.cookie('home_accordion'));
                bar_header.find('span').hide();
                bar_header.append('<span class="close">Close</span>');
            }
            catch(err) {}
        }
        else
            jQuery("#accordion").accordion({ active: false, collapsible: true, autoHeight: false, header: '.vx_block > .expand_link' });
    } else {
        jQuery("#accordion").accordion({ active: false, collapsible: true, autoHeight: false });
    }
    jQuery('div.expand_link').click(function() {
        jQuery('.select_dropdown:visible').sSelect();
        return false;
    });

    // INITIALISE DROPDOWN NAVIGATION 
    jQuery('.dropdown_navigation > li').hover(
	function() {
	    window.clearTimeout(header_supportnav_timer);
	    hideSupportNav();

	    jQuery('ul', this).css('display', 'block');
	    jQuery(this).addClass('current');
	},
    function() {
        if (jQuery(this).hasClass('headlink')) {
            header_supportnav_timer = window.setTimeout('hideSupportNav("li.headlink")', header_supportnav_timer_duration);
        } else {
            hideSupportNav();
        }
    });

    // PAGE NAVIGATION CENTER POSITIONING
    var page_container_width = jQuery('.page_navigation_center').parent('.page_navigation_center_container').width();
    var pager_width = jQuery('.page_navigation_center').width();
    var left_position = (page_container_width - pager_width) / 2;
    jQuery('.page_navigation_center').css('left', left_position + 'px');


    // INITIALISE SELECT BOX REPLACEMENT
    jQuery('.select_dropdown:visible').sSelect();


    // INITIALISE SHADOWBOX
    Shadowbox.init();


    // ANCHOR SMOOTH SCROLL
    //jQuery('#master_container').localScroll();


    //------------------------ FORMS         
    InitInputDefaultValue(".searchterms", ".searchbutton", "Search");


    jQuery('.showcase_highlight').hover(
		function() {
		    jQuery(this).addClass('showcase_highlight_hover');
		},
		function() {
		    jQuery(this).removeClass('showcase_highlight_hover');
		}
	);

    jQuery('.archive_highlight').hover(
		function() {
		    jQuery(this).addClass('archive_highlight_hover');
		},
		function() {
		    jQuery(this).removeClass('archive_highlight_hover');
		}
	);

});



//--------------------------------------------- HIDE DROPDOWN NAVIGATION
function hideSupportNav(current_id) { 
    if(arguments.length > 0){
        jQuery(current_id).find('ul').hide();           
        jQuery(current_id).removeClass('current');
    }else{
        jQuery('.dropdown_navigation li ul').hide();        
        jQuery('.dropdown_navigation > li').removeClass('current');
    } 
};


//--------------------------------------------- EXPANDABLE MODULES
function InitExpandableModules() {

    jQuery(".expandable a.expand_link").click(function() {
        var link = jQuery(this);
        var org_link_classes = link.attr('class').split('opened')[0].split('closed')[0];

        var module_holder = jQuery(this).parents('div.expand_container').find("div.expand_content");
        if (jQuery(this).find('+ div.expand_content').length > 0) {
            module_holder = jQuery(this).find("+ div.expand_content");
        }

        //console.log(org_link_classes);

        module_holder.slideToggle(expand_speed, function() {

            if (link.hasClass('closed')) {
                //if the module was CLOSE and is OPENING
                link.attr('class', org_link_classes + ' opened');
            } else {
                //if the module was OPEN and is CLOSING
                link.attr('class', org_link_classes + ' closed');
            }

            if (link.find('span:not(.vx_text)').length > 0) {
                if (link.hasClass('opened')) {
                    link.find('span:not(.vx_text)').text('View');
                } else {
                    link.find('span:not(.vx_text)').text('Hide');
                }
            }

        });
        jQuery(this).toggleClass('close');
        jQuery(this).toggleClass('open');        
        return false;
    });

    jQuery(".home_page #accordion div.expand_link").click(function() {
        collapsable_module = jQuery(this).find('+ div.expand_content');
        //Open module
        if (collapsable_module.css('display') == 'none') {
            //Close any other opened modules
            jQuery('.home_page #accordion div.expand_link').each(function() {
                close_module(jQuery(this).find('+ div.expand_content'));
            });

            //Open the selected module            
            jQuery.cookie('home_accordion', jQuery(this).attr('class').split(' ')[0]);
            jQuery(this).find('span').hide();
            jQuery(this).append('<span class="close">Close</span>');

            //Close module
        } else {
            jQuery.cookie('home_accordion', null);
            close_module(collapsable_module);
        }
    });
}

function close_module(collapsable_module) {    
    bar_header = collapsable_module.prev('div.expand_link');
    bar_header.find('span.close').remove();
    bar_header.find('span').show();
}


//--------------------------------------------- FORMS
/* initialize the inputs which requires a 'default value' system */
function InitInputDefaultValue(input_class, submit_class, default_value) {
    var input_text = jQuery(input_class);
    
    //Init the input field
    if (input_text.val() == "") {
        input_text.val(default_value);
    }
    
    //Add input field events
    input_text.click(function() {
        if (jQuery(this).val() == default_value)
            jQuery(this).val("");
    }).blur(function() {
        if (jQuery(this).val() == "")
            jQuery(this).val(default_value);
    });
    
    //Add form submit event
    jQuery(submit_class).click(function() {
        if (input_text.val() == default_value) {
            input_text.val("");
        }
    });
}

function getURLParam(strParamName){
    var strReturn = "";
    var strHref = window.location.href;

    if ( strHref.indexOf("?") > -1 ){        
        var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
        var aQueryString = strQueryString.split("&");
        for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
            if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
                var aParam = aQueryString[iParam].split("=");
                strReturn = aParam[1];
                break;
            }
        }
    }
    return strReturn;
}
