/*
	Site:		Shock Absorber: Run Ball Racket
	File:		/assets/scripts/global.js
	Author:		Andrew Disley, Simplified http://simplified.co.uk/
	Version:	2009-12-01
----------------------------------------------- */

/*	Global Configuration
----------------------------------------------- */
var $j = jQuery;
var isIE = false;
var isIE6 = false;
var isSafari = false;

jQuery.fn.jWait = function(duration) {
	jQuery(this).animate({ dummy: 1 }, duration);
	return this;
};

var getStyleProperty = (function() {
	var prefixes = ['Moz', 'Webkit', 'Khtml', 'O', 'Ms'];
	function getStyleProperty(propName, element) {
		element = element || document.documentElement;
		var style = element.style,
		prefixed;
		if (typeof style[propName] == 'string') {
			return propName;
		}
		propName = propName.charAt(0).toUpperCase() + propName.slice(1);
		for (var i=0, l=prefixes.length; i<l; i++) {
			prefixed = prefixes[i] + propName;
			if (typeof style[prefixed] == 'string') {
				return prefixed;
			}
		}
		return false;
	}
	return getStyleProperty;
})();

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

	/* Configuration, Flags and Messages */
	cWindowWidth: null,
	cWindowWidthReq: null,
	
	/* jQuery Object References  */
	jEOBody: null,
	jEONavigationGlobal: 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#Container').addClass('safari'); }

	},

	styleGlobalNav : function(){

		var cc = this;
		
		cc.jEONavigationGlobal.css('left', '-35px');

		/* If window width >= cWindowWidthReqhide show jEONavigationGlobal else only show 12px and enable hover effects */
		if (cc.cWindowWidth >= cc.cWindowWidthReq) {
			cc.jEONavigationGlobal.jWait(1000).css('left', '-35px').animate({ left: '+=35px' });
		} else {
			cc.jEONavigationGlobal.jWait(1000).css('left', '-35px').animate({ left: '+=12px' });
			cc.jEONavigationGlobal.hover(function() {
				cc.jEONavigationGlobal.animate({ left: '+=23px' });
			}, function() {
				cc.jEONavigationGlobal.animate({ left: '-=23px'});
			});
		}

	},

	init : function() {

		var cc = this;

		cc.cWindowWidth = $j(window).width();
		cc.cWindowWidthReq = '1000';

		cc.jEOBody = $j('body').addClass('jQ');
		cc.jEONavigationGlobal = $j('div#NavigationGlobal');

		cc.utilBrowserTests();
		cc.styleGlobalNav();

	}

};

/*	Components Object
----------------------------------------------- */
var SComponents = {

	/* Configuration */
	cModalExpose: null,

	/* jQuery Object References */
	jEOHomeCTA: null,
	jEOProduct: null,
	jEOProductCTA: null,
	jEOFeatures: null,
	jEOFeaturesCTA: null,
	jEOFocusOnTabs: null,
	jEOFocusOnTabsNav: null,

	sCHomeCTA : function() {

		var cc = this;

		$j('.c-b', cc.jEOHomeCTA).flashembed({
			version: [10, 0],
			bgcolor: '#000000',
		    expressInstall: 'assets/swf/expressinstall.swf',
			src: 'assets/swf/preloader.swf',
			menu: false,
			wmode: 'opaque',
			quality: 'high'
		}, {
			swf: 'assets/swf/c-home-cta.swf',
			runFind: 'run-sportsbra.html',
			runBuy: 'checkout-runbra.html',
			ballFind: 'ball-sportsbra.html',
			ballBuy: 'checkout-ballbra.html',
			racketFind: 'racket-sportsbra.html',
			racketBuy: 'checkout-racketbra.html'
		});

	},

	sCProductModal : function() {

		var cc = this;

		/* Using CFT test if borderRadius is supported */
		if (typeof getStyleProperty('borderRadius') == 'string') {
			/* If borderRadius is supported apply to Modal */
			cc.jEOFeatures.css(getStyleProperty('borderRadius'), '10px');
			cc.jEOFeaturesItems.css(getStyleProperty('borderRadius'), '10px');
		}

		cc.jEOFeaturesCTA.overlay({
			expose: cc.cModalExpose,
			left: 40,
			target: cc.jEOFeatures,
			top: 10
		});

	},

	sCProductHover : function() {

		var cc = this;

		cc.jEOProductCTA.hover(function() {
			cc.jEOProduct.css('display', 'none');
		}, function() {
			cc.jEOProduct.removeAttr('style');
		});

	},

	sCFocusOnTabs : function() {

		var cc = this;

		cc.jEOFocusOnTabsNav.tabs(cc.jEOFocusOnTabs, {
			current: 'active',
			effect: 'fade',
			fadeOutSpeed: "slow"
//			,rotate: true
		});

	},

	init : function() {

		var cc = this;

		/* Configuration */
		if (isIE) {
			cc.cModalExpose = null;
		} else {
			cc.cModalExpose = {
				color: '#000000',
				loadSpeed: 200,
				maskId: 'ModalExpose',
				opacity: 0.8
			};
		}

		/* jQuery Object References */
		cc.jEOHomeCTA = $j('#Content div.c-home-cta');
		cc.jEOProduct = $j('.c-product h3 img');
		cc.jEOProductCTA = $j('.c-content .c-b strong, .c-product .c-b h4');
		cc.jEOFeatures = $j('#Features');
		cc.jEOFeaturesItems = $j('li span', cc.jEOFeatures);
		cc.jEOFeaturesCTA = $j('#Content div.c-product h3, #Content div.c-product li.features a');
		cc.jEOFocusOnTabs = $j('body.focuson #Content .c-item');
		cc.jEOFocusOnTabsNav = $j('body.focuson #TOC');

		if (cc.jEOHomeCTA.length) {
			cc.sCHomeCTA();
		}

		if (cc.jEOFeatures.length && cc.jEOFeaturesCTA.length) {
			cc.sCProductModal();
		}

		if (cc.jEOProduct.length && cc.jEOProductCTA.length) {
			cc.sCProductHover();
		}


/*
		if (cc.jEOFocusOnTabs.length) {
			cc.sCFocusOnTabs();
		}
*/

	}

};

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

	SGlobal.init();

	SComponents.init();

});
