/*
	Site:		Grimsby: Food Town: Website
	File:		/assets/scripts/global.js
	Version:	2009-04-13
	Author: 	
----------------------------------------------- */

/*	Global Configuration
----------------------------------------------- */
jQuery.noConflict();
var $j = jQuery;
var isIE6 = false;
var isSafari = false;
if (typeof sIFR) {
	var cSIFRFont = { src: '/assets/scripts/lib/sifr/3-akkurat-common.swf' };
	var cSIFRFont2 = { src: '/assets/scripts/lib/sifr/3-akkurat-bold-common.swf' };
	sIFR.useDomLoaded = true;
//	sIFR.useStyleCheck = true;
	sIFR.activate(cSIFRFont, cSIFRFont2);
}

/* Enable caching of JS loaded via getScript */
$j.ajaxSetup({ cache: true });

/*	Global Literal Object - Site-wide functions
----------------------------------------------- */
var Global = {

	/* Configuration */

	/* jQuery Object References to the Elements this speeds up the DOM */

	/* Utility: Browser Tests / Specific Fixes/Hooks */
	utilBrowserTests : function() {

		/* Checks for IE6 using object detection, then applies the fix for background flicker bug */
		if (typeof document.body.style.maxHeight == 'undefined') { isIE6 = true; try { document.execCommand('BackgroundImageCache', false, true); } catch(e) { } }

		/* Checks for Safari and adds class of .safari to div#Container */
		if($j.browser.safari) { isSafari = true; $j('div#Container').addClass('safari'); }

	},

	/* Utility: Form Legends */
	utilFromLegends : function() {
		$j('legend.accessibility').hide();
		$j('legend.replace').each(function(){
			var jObj = $j(this);
			var cssClassesStr = jObj.attr('class').toString();
			var cssClasses = cssClassesStr.split('replace replace-');
			jObj.after('<' + cssClasses[1] + ' class="legend">' + jObj.html() + '</' + cssClasses[1] + '>').remove();
		});
	},

	/* Utility: External Links */
	utilExternalLinks : function() {
		$j('a.external').each(function(){
			$j(this).click(function(){return !window.open($j(this).attr('href'));});
		});
	},

	/* Utility: External Links */
	uiSIFR : function() {

		/* For each .sifr get current hight and apply to style to avoid .sifr render jump */

		$j('.sifr').each(function() {
			var jCHight = $j(this).height();
			var jCPaddingBottom = parseInt($j(this).css('padding-bottom'));
			var jCFontSize = parseInt($j(this).css('font-size'));
//			jCHight = (jCHight + jCPaddingBottom);
			jCHight = (jCHight/jCFontSize) + 'em';
			$j(this).css('height', jCHight);
		});

		sIFR.replace(cSIFRFont, {
			selector: 'h1.sifr'
			,css: '.sIFR-root { background-color: #FFFFFF; color: #009AB9; leading: -5; }'
			,selectable: true
			,wmode: 'transparent'
			,transparent: true
		});

		sIFR.replace(cSIFRFont2, {
			selector: '.c-factbox strong.sifr'
			,css: '.sIFR-root { color: #FFFFFF; display: block; font-size: 50px; line-height: 1; }'
			,selectable: true
			,wmode: 'transparent'
			,transparent: true
		});

	},

	/* Initialise */ 
	init : function() {

		/* Class Context */
		var cc = this;

		/* Configuration */

		/* jQuery Object References to the Elements this speeds up the DOM */

		cc.utilBrowserTests();
		cc.uiSIFR();

	}

};


/*	Listings Literal Object - functions for Component: div.c-whylocatehere
----------------------------------------------- */
var ComponentWhyLocateHere = {

	/* Configuration */

	/* jQuery Object References to the Elements this speeds up the DOM */
	jOEListingMeetings: null,

	/* Accordion */
	componentAccordion : function() {

		/* Class Context */
		var cc = this;

		function callbackChange(event, ui) {
			/* Get ID of header and substring to get current index then use to move background image */
			var headerID = ui.newHeader.attr('id').substring(2, 3);
			headerID = (headerID * 250);
			$j('div.c-b', cc.jOEWhyLocateHere).css({ backgroundPosition: '230px -' + headerID + 'px' });
		}

		$j('div.c-b ul', cc.jOEWhyLocateHere).accordion({
			event: 'mouseover',
			header: 'h3',
			fillSpace: true,
			change: callbackChange
		});

	},

	/* Initialise */ 
	init : function() {

		/* Class Context */
		var cc = this;

		/* Configuration */

		/* jQuery Object References to the Elements this speeds up the DOM */
		cc.jOEWhyLocateHere = $j('div.c-whylocatehere');

		cc.componentAccordion();

	}

};


/*	DOM Ready events
----------------------------------------------- */
$j(document).ready(function() {

	/* Fire global functions */
	Global.init();

	/* If component div.c-whylocatehere exisits initialize functions */
	if ($j('div.c-whylocatehere').length) {
		ComponentWhyLocateHere.init();
	}

	/* Fire local page initialize methods */
	if (typeof initPage == 'function'){ initPage(); }

	/* Fire Global.utilExternalLinks() last so any external links added via DOM during local page initialization will be activated */
	Global.utilExternalLinks();

});
