/*!
	Site:		Corby
	File:		/assets/scripts/global.js
	Author:		Creative Lynx (Mods by Dan Hill)		
----------------------------------------------- */

/*	Global Configuration
----------------------------------------------- */
var $j = jQuery;
var isIE = false;
var isIE6 = false;
var isSafari = false;

/*	Plugins
----------------------------------------------- */

/*	Global Literal Object: Site-wide functions
----------------------------------------------- */
var SGlobal = {

	/* Configuration, Flags and Messages */

	/* jQuery Object References  */
	jEOBody: null,
	jEOPrintPage: null,
	jEOPrintPageLoc: null,
	jEONavPrimary: null,
	jEONavSecondary: null,

	/* Utility: Browser Tests / Specific Fixes/Hooks */
	utilBrowserTests : function() {

		if ($j.browser.msie) { isIE = true; }

		/* Detect if User Agent is IE6 using object detection, apply fix for background flicker bug */
		if (typeof document.body.style.maxHeight == 'undefined') { isIE6 = true; try { document.execCommand('BackgroundImageCache', false, true); } catch(e) { } }

		/* Detect if User Agent is Safari, add class '.safari' to div#Container */
		if ($j.browser.safari) { isSafari = true; $j('div#Wrapper').addClass('safari'); }

	},

	/* Utility: Print Page */
	utilPrintPage : function() {

		var cc = this;

		cc.jEOPrintPage = $j('<li class="print"><a href="#PrintPage" title="Print this page"><span></span>Print</a></li>');
		cc.jEOPrintPageLoc = $j('#Utilities li.email');

		/* Insert cc.jEOPrintPage to cc.jEOPrintPageLoc */
		cc.jEOPrintPageLoc.after(cc.jEOPrintPage);
		$j('a', cc.jEOPrintPage).click(function() {
			window.print();
			return false;
		});

	},

	/* NavPrimary Events: Workaround for li:hover IE */
	eventsNavPrimary : function() {

		var cc = this

		$j('ul > li', cc.jEONavPrimary).hover(function() {
			var jThis = $j(this);
            jThis.addClass('hover');
            if (jThis.hasClass('last')) {
                jThis.addClass('last-hover');
            }
            if (jThis.hasClass('has-children')) {
                jThis.addClass('has-children-hover');
            }
		}, function() {
			var jThis = $j(this);
            jThis.removeClass('hover');
            jThis.removeClass('last-hover');
            jThis.removeClass('has-children-hover');
		});

	},
	/* NavSecondary Events: Workaround for li:hover IE */
	eventsNavSecondary : function() {

		var cc = this

		$j('ul > li', cc.jEONavSecondary).hover(function() {
			var jThis = $j(this);
            jThis.addClass('hover');
            if (jThis.hasClass('last')) {
                jThis.addClass('last-hover');
            }
            if (jThis.hasClass('has-children')) {
                jThis.addClass('has-children-hover');
            }
		}, function() {
			var jThis = $j(this);
            jThis.removeClass('hover');
            jThis.removeClass('last-hover');
            
            jThis.removeClass('has-children-hover');
		});

	},

	init : function() {

		var cc = this;

		/* ADD class to body so we can apply CSS only for when jQuery is available, different to hasJS */
		cc.jEOBody = $j('body');
		cc.jEOBody.addClass('hasjQ')

		cc.jEONavPrimary = $j('#NavPrimary');
		cc.jEONavSecondary = $j('#NavSecondary');

		cc.utilBrowserTests()
		
		if (cc.jEONavPrimary.length) {
			cc.eventsNavPrimary();
		}
		if (cc.jEONavSecondary.length) {
			cc.eventsNavSecondary();
		}

		/* If print support is available, initialise utilPrintPage */
		if (window.print) { cc.utilPrintPage(); }

	}

};

/*	Module Object
----------------------------------------------- */
var MFeatured = {

	/* jQuery Object References  */
	jEOFeatured: null,

	mBuild : function() {

		var cc = this;

		$j('.m-b div.item .s-i-i', cc.jEOFeatured).css('opacity', 0.6);

		$j('.m-h ul', cc.jEOFeatured).tabs('div.m-b > div.item', { 
			event: 'mouseover',
			rotate: true
		}).slideshow({
			clickable: false,
			autoplay: true,
			interval: 3000
		});

	},

	init : function() {

		var cc = this;

		cc.jEOFeatured = $j('div#jsFeatured');

		/* If cc.jEOFeatured markup is present and UA is IE enable hover */
		if (cc.jEOFeatured.length && isIE) {

		}

		if (cc.jEOFeatured.length) {
			cc.mBuild();
		}

	}

};

/*	DOM Ready events
----------------------------------------------- */
$j(function() {

	SGlobal.init();

	//MFeatured.init();
	
	$(document).ready(function() {
	    $("a[rel=external]").attr('target', '_blank');
	    
	});

});

/*	LINK TO NEW BROWSER WINDOW
----------------------------------------------- */

function externalLinks() {  
 if (!document.getElementsByTagName) return;  
 var anchors = document.getElementsByTagName("a");  
 for (var i=0; i<anchors.length; i++) {  
   var anchor = anchors[i];  
   if (anchor.getAttribute("href") &&  
       anchor.getAttribute("rel") == "external")  
     anchor.target = "_blank";  
 }  
}  
window.onload = externalLinks;