/*
	Copyright Code Computerlove Ltd 2010
	Build: 0.0.80.368
	Date: 10/06/2010 16:12:01
*/

// handle firebug console issues - set to false to catch those left in before going live
var debuggering = true;
if (typeof console == "undefined") var console = { log: function() {} };
else if (!debuggering || typeof console.log == "undefined") console.log = function() {};


// blanks default value on focus and swaps back if no val entered
$.fn.iDefaultSwap = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
}; /// 


// center an element within the screen
$.fn.extend({
	center: function (options) {
	var options =  $.extend({transition:300, minX:0, minY:0}, options);
	return this.each(function() {
		$(this).css('position', 'absolute');
		var top = ($(window).height() - $(this).outerHeight())/2+$(window).scrollTop();
		var left = ($(window).width() - $(this).outerWidth())/2+$(window).scrollLeft();
		
		$(this).css({
			'top': top + 'px',
			'left': left + 'px'
		});
		/*
		$(this).animate({
		        top: (top > options.minY ? top : options.minY)+'px',
		        left: (left > options.minX ? left : options.minX)+'px'
		}, options.transition);
		*/
		return $(this);
	});
	}
});


(function($j){

	Code.registerNamespace('Website');

	Website.Core = {
	
		content: null,
		selectors : {
			emailAlertInput : "ctl00_MainContentPlaceHolder_ctl01_EmailAlertCapture",
			emailAlertButton : "ctl00_MainContentPlaceHolder_ctl00_SubmitBtn",
			tabbedContent : "js-tabbed-content",
			tabs : "js-tabs",
			panes : "js-tab-pane",
			required : "required",
			modalMask : "mask",
			modalClass : "modal",
			modalTrigger : "js-modal-trigger"
		},
		
		checkBrowser: function(){
			if(Code.UserAgent.isSafari){
				$j('body').addClass('safari');
			}
		},
		
		injectStyleSheet: function(){
			$j('head').prepend('<link type="text/css" rel="stylesheet" href="/_Client/Styles/JSHide.css" media="screen" />');
		},
		
		generateFlashContent: function(target, swf, width, height){
		
			var $target = $j('#' + target);
			$target
				.css("width", width + "px")
				.css("height", height + "px");
			
			// Store the flash data.
			// We'll use this everytime the modal is recreated.
			$target[0].flashData = { src: Code.resolveUrl('~/_Client/Flash/' + swf + '.swf'),
					width: width,
					height: height,
					allowFullScreen: "true",
					bgcolor: '#000000',
					wmode: "opaque" },
				{ version: 9 };
				
						/*
			$j('#' + target).flash(
				{ src: Code.resolveUrl('~/_Client/Flash/' + swf + '.swf'),
					width: width,
					height: height,
					allowFullScreen: "true",
					bgcolor: '#000000',
					wmode: "opaque" },
				{ version: 9 }
			);
			*/
		},
		
		_handleTabs: function() {
			_self = this;
			
			// find all tab sets - the tabs and the panels
			jTabbedContent = $j("." + _self.selectors.tabbedContent);
			
			// for each set of tabs do the following
			jTabbedContent.each(function(index, elm){
				
				currentTabs = this;
				
				// on click of the tab
				$j("." + _self.selectors.tabs + " li a").click(function(e){

						e.preventDefault();
						
						// hide all panes
						$j(elm).find("." + _self.selectors.panes).hide();
						
						// grad href from tab anchor to be used as pointer to pane we'd like to show
						paneToShow = $j(this).attr("href");
						
						// remove active class from all tab buttons
						$j(elm).find("." + _self.selectors.tabs  + " li a").removeClass("active");
						
						// add active class the tab button just clicked
						$j(this).addClass("active");
						
						// show the pane linked to by tab just clicked
						$j(paneToShow).show();

				});
				
				// here we are showing the first pane and setting the first tab to active
				$j("." + _self.selectors.tabs  + " li a:first").addClass("active");

				$j("." + _self.selectors.panes, elm).each(function(i){
					
					if(i == 0) {
						$j(this).css("display", "block");
					}
					
				});
				
			});

			
		},
		
		// getPageScroll() by quirksmode.com
		_getPageScroll : function () {
		    var xScroll, yScroll;
		    if (self.pageYOffset) {
		      yScroll = self.pageYOffset;
		      xScroll = self.pageXOffset;
		    } else if (document.documentElement && document.documentElement.scrollTop) {
		      yScroll = document.documentElement.scrollTop;
		      xScroll = document.documentElement.scrollLeft;
		    } else if (document.body) {// all other Explorers
		      yScroll = document.body.scrollTop;
		      xScroll = document.body.scrollLeft;
		    }
		    return new Array(xScroll,yScroll);
		},

		// Adapted from getPageSize() by quirksmode.com
		_getPageHeight : function () {
		    var windowHeight;
		    if (self.innerHeight) { // all except Explorer
		      windowHeight = self.innerHeight;
		    } else if (document.documentElement && document.documentElement.clientHeight) {
		      windowHeight = document.documentElement.clientHeight;
		    } else if (document.body) { // other Explorers
		      windowHeight = document.body.clientHeight;
		    }
		    return windowHeight;
		},
		
		
		_handleModal : function() {
		
			var _self = this;
			
			maskId = "#" + _self.selectors.modalMask;
			
			jModalMask = $j(maskId);
			
			if(jModalMask.length < 1) {
				$j('<div id="' + _self.selectors.modalMask + '"></div>').appendTo('body');
			}
			
			//select all the a tag with name equal to modal
			$j("." + _self.selectors.modalTrigger).click(function(e) {
					
				// Cancel the link behavior
				e.preventDefault();

				//Get the A tag
				var targetModal = $j(this).attr('href');

				//set the max height and width
				var maskHeight = $j(document).height();
				var maskWidth = $j(window).width();

				//Set heigth and width to mask to fill up the whole screen
				jModalMask.css({'width':maskWidth,'height':maskHeight});

				//transition effect		
				jModalMask.fadeIn("fast");	
				jModalMask.fadeTo("fast",0.8);	

				//Get the window height and width
				var winH = $j(window).height();
				var winW = $j(window).width();
				
				// center the modal
				var jTargetModal = $j(targetModal);
				jTargetModal.center();
				
				// center modal upon trigger of window resize or scroll
				$j(window).bind('resize', function() { 
					jTargetModal.center();
					$j(maskId).css({'width': $j(window).width() });
				});
				$j(window).scroll(function() {
					jTargetModal.center();
				});

				//transition effect
				var jWorldClassRegionVideo = $j('#WorldClassRegionVideo', jTargetModal);
				if (jWorldClassRegionVideo.length == 1){
					jWorldClassRegionVideo.flash(jWorldClassRegionVideo[0].flashData);
					//console.log(jWorldClassRegionVideo[0].flashData.wmode);
				}
				jTargetModal.fadeIn("fast"); 

			});
				
			//if close button is clicked
			$j("a.btn-close").click(function(e) {
				//Cancel the link behavior
				e.preventDefault();
				jModalMask.hide();
				
				var jModal = $j("." + _self.selectors.modalClass);
				var jWorldClassRegionVideo = $j('#WorldClassRegionVideo', jModal);
				if (jWorldClassRegionVideo.length == 1){
					jWorldClassRegionVideo.empty();
				}
				jModal.hide();
			});		

			//if mask is clicked
			$j(maskId).click(function () {
				// if you want to hide modal on clicking mask uncomment lines below
				//$j(this).hide();
				//$j("." + _self.selectors.modalClass).hide();
			});
			
		},
		
		onReady: function(){
		
			var _self = this;
			
			jEmailAlertInput = $j("#" + _self.selectors.emailAlertInput);
			jEmailAlertButton = $j("#" + _self.selectors.emailAlertButton);
			jTabbedContent = $j("." + _self.selectors.tabbedContent);
			jRequiredFieldNotes = $j("span." + _self.selectors.required);
			jModals = $j("." + _self.selectors.modalClass);
			
			_self.injectStyleSheet();
			_self.checkBrowser();
			
			// if tabs are on the page run tabs function
			if(jTabbedContent.length) {
				_self._handleTabs();
			}
			
			// if find require span fill text with * instead of "required"
			if(jRequiredFieldNotes.length) {
				jRequiredFieldNotes.text("*");
			}
			
			// if email alerts input box exist handle the show/hide default value
			if(jEmailAlertInput.length) {
				$j(jEmailAlertInput).iDefaultSwap();
				$j(jEmailAlertButton).addClass('cancel'); // prevents the jQuery validation from firing
			}
			
			
			if(jModals.length) {
				_self._handleModal();
			}
			
			// if the video target div is on the page then use generate flash to embed the flash.
			
			if ($j("#WorldClassRegionVideo").length) {
				// use core flash function to populate a target div (arg1) with a swf in /Flash/ folder (arg2), width (arg3) height (arg4)
				Website.Core.generateFlashContent("WorldClassRegionVideo","WorldClassRegion", 640, 385);
			}
			
		}
		
	};

	$j().ready(function(){
		Website.Core.onReady();
	});

})(jQuery);	


