jQuery.noConflict();

var Cookie = {
	get: function(key) {
		var keyEQ = key + "=";
		var ca = document.cookie.split(';');
		for( var i=0;i < ca.length;i++ ) {
			var c = ca[i];
			while ( c.charAt(0) == ' ' ) {
				c = c.substring( 1, c.length );
			}
			if ( c.indexOf(keyEQ) == 0 ) {
				return c.substring( keyEQ.length,c.length );
			}
		}
		return null;
	},
	set: function(key, value, ttl) {
		var expires = "";
		if ( ttl ) {
			expires = "; expires=" + Cookie.hoursToExpireDate( ttl );
		}
		return document.cookie = key + "=" + value + expires + "; path=/";
		
	},
	unset: function( key ) {
		if (Cookie.get(key)) Cookie.set(key, '', 'Thu, 01-Jan-70 00:00:01 GMT');
	},
	hoursToExpireDate: function(ttl) {
		if (parseInt(ttl) == 'NaN' ) return '';
		else {
			var now = new Date();
			now.setTime(now.getTime() + (parseInt(ttl) * 60 * 60 * 1000));
			return now.toGMTString();			
		}
	}
};


var plauditSite = (function($){
	
	var setupOnLoad = function() {
		uiElements.init();
	}
	
	var uiElements = {
		
		init: function() {
			uiElements.externalPlugins.init();
			uiElements.plauditPlugins.init();
		},
		
		externalPlugins: {
			
			init: function() {
				this.swfObject();
				this.videoOverlay();
				this.videoPlayer();
				this.newsTicker();
			},
			
			swfObject: function() {
				if ( $("body").attr("id") == "home" ){
					var flashvars = {};
					var params = { wmode: "transparent"};
					var attributes = {};
						
					swfobject.embedSWF("assets/flash/home.swf", "animationInner", "605", "325", "9.0.0", "assets/flash/expressInstall.swf", flashvars, params, attributes);
				}
			},

			videoOverlay: function(){
				var lightBox = $(".pp_description");
				
				var options = {
					animationSpeed: 'normal',
					allowresize: false,
					showTitle: false,
					theme: 'light_rounded'
				};
				$("a[rel^='lightbox']").prettyPhoto(options);
			},
			
			/*
			 * Video Player Module
			 */
			
			videoPlayer: function(){
				
				var obj = this,
				
					// Empty vars for use later
					selectedClip,
				
					// DOM Elements
					overlayContainer = "video-container-wrap",
					videoContainer = "video-container",
					videoFile = $("a.video"),
					
					// Flow Player Vars
					flowPlayerSWF = "assets/flash/flowplayer-3.2.5.swf",
					flowPlayerOptions = {
						clip: {
							autoPlay: false,
							autoBuffering: true
						}
					},
					
					// Overlay Vars 
					overlayOptions = {
						mask: {
							color: '#000000',
							loadSpeed: 200,
							opacity: 0.8
						},
						
						onLoad: loadVideo,
						onBeforeClose: hideVideo,
						closeOnClick: false
					},
					
					videoPlayer = null,
					// Flowplayer video setup 
					
					// Overlay setup
					overlay = $("#" + overlayContainer).overlay(overlayOptions),
					overlayApi = overlay.data("overlay");
		
				// End Variable Setup
			
				
				// Start function definitions
				
				function getClipURL(elem){
					return elem.attr("href");
				}
				
				function loadVideo(){
					if (videoPlayer == null){
						videoPlayer = flowplayer(videoContainer,flowPlayerSWF,flowPlayerOptions);
						videoPlayer.load(function(){
							videoPlayer.play(selectedClip);
						});
					} else {
						videoPlayer.show();
						videoPlayer.play(selectedClip);
					}
				}
				
				function hideVideo(){
					videoPlayer.hide();
				}
				
				videoFile.click(function(e){
					e.preventDefault();
					selectedClip = getClipURL($(this));
					overlayApi.load();
				});
					
			},
			
			newsTicker: function(){
				$("#newsTicker ul").cycle({
					fx: 'fade',
					pause: 1,
					timeout: 4000,
					speed: 750
				});
			}
		},
		
		plauditPlugins: {
			
			init: function() {
				this.parallax();
				this.officeLocator.init();
			},
			
			parallax: function() {
				
				var position;
				var parallaxBack = $("#parallaxBack");
				var parallaxMid = $("#parallaxMid");
				var parallaxFore = $("#parallaxFore");
				var totalScreenWidth = $(window).width();
				var speeds = [ 0.0145, 0.040, 0.18 ] // Array for speeds [ parallaxBack, parallaxMid, parallaxFore ]
				               
				var setXPos = function(parallaxBack, position, speed) {
					var val = $(window).width() * (position * speed)/100;
					parallaxBack.css(	"background-position",	val+"px 0px" );
				}
					
				var animateParallax = function(mousePosition) {
					if( mousePosition != null ) {
						Cookie.set( "mousePosition", mousePosition, 30*24 );
						
						position = parseInt( (mousePosition / totalScreenWidth) * 100 ); //Calculate current mouse position and divided by total screen width
						
						setXPos( parallaxBack,	position,speeds[0] );
						setXPos( parallaxMid,	position,speeds[1] );
						setXPos( parallaxFore,	position,speeds[2] );
					}
				}
				
				$(document).mousemove(function(e){
					animateParallax(e.pageX);
				});
				
				animateParallax( Cookie.get("mousePosition") );
			},
			
			officeLocator: {
				
				init: function() {
					var defaultLocation = "mainOffice"; // Select which location should be the default
					
					$("#" + defaultLocation).fadeIn(); // Fade in default locations...
					this.addActiveState(defaultLocation); // And add active state to default location
					this.selectLocation(); // Initalize selectLocation event handler
					this.setupToolTip();
				},
				
				addActiveState: function(location) {
					var locationName = "." + location;
					
					$("#map span").removeClass("active mainOfficeActive"); // Remove active class from any current items
					
					// Select active class name based on location. Main office uses a different image sprite
					if( locationName === ".mainOffice") {
						$(locationName).addClass("mainOfficeActive"); 
					} else {
						$(locationName).addClass("active");
					}
				},
				
				selectLocation: function() {
					var $this = this; // Get a referece to this so we can use it inside of the click method
					
					var mapLocations = $("#map span");
					var selectedLocation;
					
					mapLocations.click(function(e){
						selectedLocation = $(this).attr("class").split(" "); // Get the clicked elements class name and create an array.
						selectedLocation = selectedLocation[0]; // Select first class name from array
						
						$(".location", $("#locationInfo")).fadeOut(400); // Fade out current location
						$("#" + selectedLocation).delay(500).fadeIn(400); // Fade in new location
						
						$this.addActiveState(selectedLocation); // Add active class to new item
						
						e.preventDefault();
					});
				},
				
				setupToolTip: function(){
					$("#map span").each(function(){
						$(this).removeAttr("title"); // Remove deafult tooltip which is include for users with js turned off
					});
					
					var toolTip = $("#toolTip"),
						adjustYOffset = 10,
						adjustXOffset = -20,
						documentSize = $(window).width(),
						toolTipX,
						toolTipY;
					
					$("#map span").hover(
						function(){
							var $this = $(this) // cache this
							var offset = $this.offset(); //get the offset of the current link
							var content = $this.html();
							
							toolTipX = offset.left - adjustXOffset;
							toolTipY = offset.top - adjustYOffset;
							
							toolTip.stop().css({
								top: toolTipY,
								left: toolTipX
							});
							
							toolTip.empty().append(content);
							
							if ($.support.opacity) {
								toolTip.fadeTo(350, 1);
							} else {
								toolTip.show();
							}
							
						}, function(){
							toolTip.hide();
						}
					);
				} // Setup Tooltip
				
			} // Office Locator
			
		} // Plaudit Plugins
		
	}; // Ui object
	
	return {
		init: setupOnLoad()
	}
	
})(jQuery);

jQuery(function(){ plauditSite.init; });


