/*
 * homepage-specific javascripts
 * 
 * @author Kirk Bushell
 * @date 29/08/2008
 */
var cas, acc, fs, cat, store, inquiry, categoryDropdown, topCategory;
var teamsiteDropDownWidgetSelects = [];
var widgetSelectObjects = [];

Event.observe( window, 'load', function() {
	cat = new WFStyleSelect( 'cat', 'category', 'updateCat' );
	
	for (var i = 0; i < teamsiteDropDownWidgetSelects.length; i++) {
		widgetSelectObjects.push(
			new WFStyleSelect('widgetSelectObjects[' + i + ']', teamsiteDropDownWidgetSelects[i], 'itemSelected')
		);
	}
	
	$$('img.active').each(function(img) {
		img.setStyle({ display: 'none' });
	});

//	$$('#carousel-list li div.cont').each(function(field) {
//		divChildren = field.childElements();
//
//		// Retrieve the link and apply to the div.cont
//		field.observe('click', function(event) {
//			link = this.childElements()[3].firstDescendant().readAttribute('href');
//			window.location = link;
//		});
//	});
	
	body = Element.extend(document.body);
	body.observe('click', function() {
		// hide open 
		if (openCont) $(openCont).hide();
	});

    // Add event tracking to UK sign up banners
    if (typeof(UkSignUpEvents) !== "undefined" && typeof(pageTrackerUK) !== "undefined") {
        UkSignUpEvents.init();
    }
} );

/*
 * updateCat
 * Callback function for when a user selects an item in the stylized select drop downs
 */
function updateCat( args )
{
	var centre = getCentreFromAddressBar();
	if (args.optionValue != "") {
		url = "/" + centre + "/find/search/store?category=" + args.optionValue;
		window.location.href = (url);
	}
}

/*
 * itemSelected
 * Callback function for a drop down on the right hand side of the home page
 * 
 */

function itemSelected(args) {
	var divs_to_hide = $$('.' + args.select + '-content');
	var div_with_content = $(args.optionValue.replace('opt-', args.select + '-cont-'));
	
	for (var i = 0; i < divs_to_hide.length; i++) {
		divs_to_hide[i].style.display = 'none';
	}
	
	if (div_with_content != null) {
		div_with_content.style.display = 'block';
	}
	
	// Add parameter to the link, incase we need it when it is clicked
	link = $(args.select + '-link');
	
	link.search = '?service_name=' + $('SSC' + args.select + '-styleIt').firstDescendant().innerHTML;
}

function categorySelected(args) {
	var centre = getCentreFromAddressBar();
	if (args.optionValue != "") {
		window.location.href = "/" + centre + "/find/search/" + topCategory + "?category=" + args.optionValue;
	}
	return false;
}


function getCentreFromAddressBar() {
	var centre;
	// split by /
	centre = String(window.location).split('/')[3];
	
	// split by #
	centre = String(centre).split('#')[0];
	
	return centre;
}

var UkSignUpEvents = (function() {
    return {
        init: function() {
            jQuery(".static-content").find("img").click(function() {
                pageTrackerUK._trackEvent("links", "centres", "Homepage Sign Up banner clicked");
            });

            jQuery("#newsletter-sign-up").click(function() {
                pageTrackerUK._trackEvent("links", "centres", "Homepage Sign Up text link clicked");
            });
        }
    };
}());

