/*
	Site:		FDI & Wrigley Photographic Award 2009: Website
	File:		/assets/scripts/global.js
	Version:	2009-03-12
	Author: 	
----------------------------------------------- */

/*	Global Configuration
----------------------------------------------- */
jQuery.noConflict();
var $j = jQuery;
var sitePath = '';
var isIE6 = false;
var isSafari = false;

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

/* jQuery.preloadImages */
jQuery.preloadImages = function() {
	for(var i = 0; i<arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	}
};

/*	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: External Links */
	utilExternalLinks : function() {
		$j("a.external").each(function(){
			$j(this).click(function(){return !window.open($j(this).attr("href"));});
		});
	},

	/* Initialise */ 
	init : function() {

		/* Class Context */
		var cc = this;

		/* Configuration */

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

		cc.utilBrowserTests();

	}

};

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

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

	if ($j('body.submit').length) {

		var additionalPhotographCount = 1;
		var additionalPhotographAction = $j('<span id="additional"><a href="#photograph-2"><img alt="Enter an additional photograph" height="22" src="' + sitePath + 'assets/images/content/submit/additional.png" width="221" /></a></span>');
		var additionalPhotographNavigation = $j('<ul class="photograph-navigation"><li class="inactive" id="photograph-1-nav"><a class="inactive" href="#photograph-1">Photograph 1</a></li><li class="inactive" id="photograph-2-nav"><a href="#photograph-2">Photograph 2</a></li><li class="inactive" id="photograph-3-nav"><a class="inactive" href="#photograph-3">Photograph 3</a></li><li class="inactive" id="photograph-4-nav"><a href="#photograph-4">Photograph 4</a></li><li class="inactive" id="photograph-5-nav"><a href="#photograph-5">Photograph 5</a></li></ul>');

		/* Hide div.photograph fields */
		$j('div#photograph-2, div#photograph-3, div#photograph-4, div#photograph-5').hide();

		/* Prepends additionalPhotographAction to div.form-row-action */
		$j('div.form-row-action').prepend(additionalPhotographAction);

		$j('span#additional a').click(function() {

			/* Updated additionalPhotographCount */
			additionalPhotographCount++;

			/* Create var href attribute of new photograph */
			var cThisHrefNew = '#photograph-' + additionalPhotographCount;

			/* Create var href attribute + 1 for next photograph */
			var cThisHrefNext = '#photograph-' + (additionalPhotographCount + 1);

			/* If additionalPhotographCount is 2 > show additionalPhotographNavigation and set current state */
			if (additionalPhotographCount >= 2) {
				additionalPhotographNavigation.attr('id', 'photograph-navigation-' + additionalPhotographCount);
				$j('ul.photograph-navigation li.active').removeClass('active').addClass('inactive');
				$j('li#photograph-' + additionalPhotographCount + '-nav').removeClass('inactive').addClass('active');
				additionalPhotographNavigation.show();
			}

			/* Show new photograph */
			/* Hide all div.photograph fields */
			$j('div.photograph').hide();
			$j(cThisHrefNew).show();

			/* Update href attribute of additionalPhotographAction for accessibility */
			$j(this).attr('href', cThisHrefNext);

			/* If additionalPhotographCount is 5 remove the button */
			if (additionalPhotographCount == 5) {
				additionalPhotographAction.remove();
			}

			/* Set Location URL Hash to cThisHrefNew */
			window.location.hash = cThisHrefNew;

			return false;
		});

		/* Hide additionalPhotographNavigation */
		additionalPhotographNavigation.hide();
		
		/* Prepends additionalPhotographNavigation to div.form-col-2 */
		$j('div.form-col-2').prepend(additionalPhotographNavigation);

		$j('a', additionalPhotographNavigation).click(function() {
			var jOEThis = $j(this);
			var jOEThisID = jOEThis.attr('href');
			jOEThisID = jOEThisID.substring(jOEThisID.indexOf("#"));

			var jOEThisParrent = $j(this).parent();

			if (jOEThisParrent.hasClass('inactive')) {
				$j('li.active').removeClass('active').addClass('inactive');
				jOEThisParrent.removeClass('inactive').addClass('active');

				/* Show new photograph */
				/* Hide all div.photograph fields */
				$j('div.photograph').hide();
				$j(jOEThisID).show();

			}
			return false;
		});
	}

	if ($j('body.home').length) {
		/* Preload images for use on all pages */
		$j.preloadImages(
			sitePath + 'assets/images/content/about-fdi/Content.jpg',
			sitePath + 'assets/images/content/about-fdi/h1.png',
			sitePath + 'assets/images/content/about-fdi/li.png',
			sitePath + 'assets/images/content/about-fdi/s-ui-i-1.png',
			sitePath + 'assets/images/content/about-wohp/Content.jpg',
			sitePath + 'assets/images/content/about-wohp/h1.png',
			sitePath + 'assets/images/content/about-wohp/action.jpg',
			sitePath + 'assets/images/content/contact/Content.jpg',
			sitePath + 'assets/images/content/contact/FDIWorldDentalFederation.png',
			sitePath + 'assets/images/content/contact/h1.png',
			sitePath + 'assets/images/content/gallery/Content.jpg',
/*			sitePath + 'assets/images/content/home/action.png',
			sitePath + 'assets/images/content/home/s-ui-col-1.jpg',
			sitePath + 'assets/images/content/home/s-ui-i-1.jpg',*/
			sitePath + 'assets/images/content/how-to-enter/action.png',
			sitePath + 'assets/images/content/how-to-enter/Content.jpg',
			sitePath + 'assets/images/content/how-to-enter/h1.png',
			sitePath + 'assets/images/content/prizes/Content.jpg',
			sitePath + 'assets/images/content/prizes/h1.png',
			sitePath + 'assets/images/content/submit/additional.png',
			sitePath + 'assets/images/content/submit/form.jpg',
			sitePath + 'assets/images/content/submit/h1.png',
			sitePath + 'assets/images/content/submit/submit.png',
			sitePath + 'assets/images/content/terms/Content.jpg',
			sitePath + 'assets/images/content/terms/h1.png');
	}

	/* 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();

});