var video = {
	setGlobals: function(){
		video.openLinks = jQuery('a[rel="open-video"], a.open-videoBTN');
		video.closeRel = 'close-video-window';
		video.videoWindowHTML = '<div id="video-window"><a href="#" id="close-link" rel="' + video.closeRel + '">close window</a><div style="height:522px;"><div id="video-player"></div></div><a href="" class="alt-video-link link-ipod">iPod Download</a><a href="" class="alt-video-link link-wmv">WMV Download</a><div id="addthis-toolbox" class="addthis_toolbox addthis_default_style"><a href="http://addthis.com/bookmark.php?v=250" class="addthis_button_compact">Share</a><span class="addthis_separator"></span><a class="addthis_button_facebook"></a><a class="addthis_button_twitter"></a><a class="addthis_button_googlebuzz"></a><a class="addthis_button_email"></a><span class="addthis_separator"></span><a class="addthis_button_facebook_like"></a></div></div>';
		video.backButtonMask = '<div style="position: absolute; top: 0; left: 0; cursor: default; z-index: 100; background: white; height: 36px; width: 41px; opacity: 0;"></div>';
	},
	
	grabCollAndRoom: function(myTarget, myId){
		var thisTarget = myTarget;
		var thisId = myId;
		var $thisArr = [];
		var $thisRoom = '';
		var $thisCollection = '';
		var $viewCollLink = jQuery('div#video-window a#view-collection');
		if(thisId.indexOf('coll-') !== -1) {
			$thisArr = thisId.split('-').splice(1, 2);
			$thisCollection = $thisArr[0];
			$thisCategory  = $thisArr[1];
			// console.log('$thisArr: ', $thisArr);
			// console.log('$thisCategory: ', $thisCategory);
			// console.log('$thisCollection: ', $thisCollection);
			var collUrl;
			var collId = 'link-to-' + $thisCollection;
			
			switch ($thisCollection) {
				case 'magnedock': case 'h2okinetic':
					collUrl = '/' + $thisCategory + '/' + $thisCollection + '.html';
					break;
				case 'smarttouch':
					collUrl = '/' + $thisCategory + '/smart-touch.html';
					break;
				case 'sensori':
					collUrl = '/' + $thisCategory + '/function/' + $thisCollection + '.html';
					break;
				default:
					collUrl = '/' + $thisCategory + '/collection/' + $thisCollection + '.html';
			}
			
			// if($thisCollection === ('magnedock' || 'h2okinetic' || 'smarttouch')) {
			// 	collUrl = '/' + $thisCategory + '/' + $thisCollection + '.html';
			// }else if($thisCollection === 'sensori') {
			// 	collUrl = '/' + $thisCategory + '/function/' + $thisCollection + '.html';
			// };
			
			$viewCollLink.addClass('area-collection-link');
			setTimeout(function() {
				$viewCollLink.removeClass('displayNone');
				$viewCollLink.attr('href', collUrl);
				$viewCollLink.attr('id', collId);
			}, 5); //This fixes a potential bug in IE7 where it does not want to update .attr changes immediately at the .click method.
		}
	},
	
	assignVideoWindow: function(){
		video.vw = jQuery('div#video-window');
	},

	openVideo: function(videoUrl, relatedVids,rel){		
		
		var vUrl = videoUrl;
		var vPath = jQuery.url.setUrl(vUrl).param("vUrl");
		
		// Video overlay config vars
		var config = {
			height: 542,
			width: 688,
			top: 120,
			left: 214,
			speed: 100,
			padLeft: 0
		};
		
		// Scroll window to top if scrolled down past 120 pixels
		if($(document).scrollTop() > 120) {
			$(document).scrollTop(120);
		}
		
		jQuery('div#wrapper').append(video.videoWindowHTML);
		if( videoUrl.indexOf('television-ad') !== -1 ) {
			jQuery('a.alt-video-link').remove();
		}
		video.assignVideoWindow();
		var cl = jQuery('a[rel="' + video.closeRel + '"]');
		var vcl = jQuery('div#video-window a[rel="' + video.closeRel + '"]');
		video.setCloseLinks(cl);
		vcl.addClass('hidden')
			.animate({ opacity: 0.5 }, 250)
			.hover(				
				function(){ jQuery(this).animate({ opacity: '0.85' }, 100); },
				function(){ jQuery(this).animate({ opacity: '0.5' }, 100); }
		);
	
		
		/**
		 * Insert URLs for alternate video format links
		 */
		
		// Set alternate video paths
		var altVidPaths = {
			ipod: vPath.replace('videos/', 'videos/m4v/').replace('flv', 'm4v'),
			wmv: vPath.replace('videos/', 'videos/wmv/').replace('flv', 'wmv')
		};
		
		// Update link hrefs
		$('a.link-ipod').attr('href', altVidPaths.ipod);
		$('a.link-wmv').attr('href', altVidPaths.wmv);
	
	
		/**
		 * Set up AddThis
		 */
		
		// Store location-related vars
		var docLoc = location.href;
		var docHash = location.hash;
		
		// Strip out any existing URL hash
		docLoc = docLoc.replace(docHash, '');
		
		// Build new URL to share
		var shareUrl = docLoc + '#' + jQuery.url.setUrl(vUrl).param("vUrl").replace('.flv', '');
		
		// Instantiate AddThis button
		// - We pass in custom URL which includes hash to auto-launch video
		addthis.toolbox("#addthis-toolbox", {}, {url: shareUrl});
				
		
		/**
		 * Add Related Videos when appropriate
		 */
		if(relatedVids !== 0 || $('body#products-shower').length) {
			// Change dimensions and placement to accommodate related videos
			config.width += 34;
			config.height += 328;
			config.left -= 214;
			config.padLeft += 214;
			
			// Set highlight class on thumb for default video
			var highlightDefault = function(){				
				$('li.related-video:first a img').addClass('highlight');
				// $('li.related-video a').each(function(){
				// 					var $this = $(this);
				// 					if($this.attr('href') === vPath) {
				// 						$('#related-videos img').removeClass('highlight');
				// 						$('img', $this).addClass('highlight');
				// 					}
				// 				});
			};
			// Add related videos container
			if(!$('#related-videos').length) {
				var currentRoom = location.href.indexOf('/bath/') === -1 ? 'kitchen' : 'bath';
				var urlToUse = '/experience/gallery/video-gallery/collection/' + currentRoom + '/' + relatedVids +'.html #content';
				if(rel != "open-video"){
					urlToUse = rel + " #content";
				}
				
				$('<div id="related-videos"/>').appendTo('body').load(urlToUse, function(){
					highlightDefault();
					
					// Make new video play in place when its thumb is clicked
					$('li.related-video a').click(function(e){
						e.preventDefault();
						
						var $this = $(this);
						var thisHref = $this.attr('href');						
						
						// Swap existing Flash object with div
						$('object#video-player').before('<div id="video-player"/>').remove();
						// Play new video
						video.playVideo(thisHref);
						// Add highlight class to thumbnail
						$('#related-videos img').removeClass('highlight');
						$('img', $this).addClass('highlight');
						
						// Update alternate format links
						var newIpod = thisHref.replace('videos/', 'videos/m4v/').replace('flv', 'm4v');
						var newWmv = thisHref.replace('videos/', 'videos/wmv/').replace('flv', 'wmv');
						$('a.link-ipod').attr('href', newIpod);
						$('a.link-wmv').attr('href', newWmv);
					});
				}).hide();
			} else {
				highlightDefault();
			}			
			
			// Move related videos into overlay
			$('#related-videos').appendTo('#video-window').show();			
		}
		
		
		/**
		 * Hide page elements when launching video overlay
		 */
		
		// Hide thumb scroller
		jQuery('#thumb_scroller').css({ width: '0px', display: 'none', opacity: 0 });
		
		// If we're on the homepage, we need to dismiss the existing Flash
		if(jQuery('body#home').length) {
			swfobject.removeSWF('home-flash');
		}
		
		// Hide remaining elements
		brizo.sideNav.fadeOut('fast');
		brizo.scrollerTitles.fadeOut('fast');
		brizo.thumbWrapper.fadeOut('fast');
		
		// Animate video window
		video.vw.animate({ opacity: 1, top: config.top+'px', left: config.left+'px', width: config.width+'px', height: config.height+'px', paddingLeft: config.padLeft+'px' }, config.speed, brizo.easing, function(){
			// ALTERNATE/MULTIPLE VIDEOS
			// Look for vUrl param and if it exists, play the flv specified in the URL rather than page default
			if (vUrl.indexOf('vUrl') != -1) {
				if(vUrl.indexOf('?' != -1) && vUrl.indexOf('flv=' != -1)) {
					var splitString = vUrl.split('vUrl=');					
					video.playVideo(splitString[1]);
				} else {
					video.playVideo(vUrl);
				}
			} else {
				video.playVideo();
			}
			
			$('a.alt-video-link').fadeIn(500);
		}).css({ zIndex: 79 });
		
		// Hide extended content on Collection page
		$('div.extended-content-group').css({ visibility: 'hidden' });
	},
	
	// Add overlay to mask back link if present when movie overlay is invoked
	maskBackLink: function(){
		if(jQuery('a.back-link').length){
			jQuery(video.backButtonMask).appendTo(jQuery('a.back-link')).animate({ opacity: 1 }, 250);
		}		
	},
	
	// Remove mask overlay from back link if present when movie overlay is closed
	unMaskBackLink: function(){
		if(jQuery('a.back-link').length){
			var bm = jQuery('a.back-link div');
			bm.animate({ opacity: 0 }, 250, function(){ bm.remove(); });
		}		
	},

	closeVideo: function(){
		video.assignVideoWindow();
		video.unMaskBackLink();		
		video.vw.animate({ opacity: '0', top: '275px', left: '401px', width: '300px', height: '200px' }, 200, brizo.easing, function(){			
			jQuery('#video-player').remove();
			video.vw.remove();
			brizo.sideNav.fadeIn('fast');
			brizo.thumbWrapper.fadeIn('fast');
			brizo.scrollerTitles.fadeIn('fast', function(){
				jQuery('#thumb_scroller').css({ width: '680px', display: 'block', opacity: 1 });
			});
			// Show extended Collection page content
			if($('div.extended-content-group').length) {
				$('div.extended-content-group').css({ visibility: 'visible' });
			}			
		});
		
		// If related videos exist
		if($('#related-videos').length) {
			// Move them back to the body tag for future use
			$('#related-videos').appendTo('body').hide();
		}	
		
	},
	
	setOpenLinks: function(){
		video.openLinks.bind('click', function(e){
			e.preventDefault();
			brizo.hideContentBody();
			video.maskBackLink();
			
			var relatedVids = 0;
			if(location.href.indexOf('shower') !== -1 || location.href.indexOf('collection') !== -1) {
				relatedVids = jQuery.url.attr('file').replace('.html', '');
			}
			
			var $this = $(this);
			var vUrl = $this.attr('href');
			var rel = $this.attr("rel");

			var $thisId = $this.attr('id');
			setTimeout(function(){ video.openVideo(vUrl, relatedVids,rel); video.grabCollAndRoom($this, $thisId); }, 100);			
		});
	},

	setCloseLinks: function(closeLink){
		closeLink.bind('click', function(){
			video.closeVideo();
			setTimeout(function(){ brizo.showContentBody(); }, 100);
			return false;
		});
	},

	playVideo: function(videoUrl){
		if(typeof(pageTracker) != "undefined"){
			pageTracker._trackPageview(videoUrl);
		}
		
		// If we're on the homepage, add autoplay param
		if(jQuery('body#home').length) {
			swfobject.embedSWF( "/flash/videoplayer.swf", "video-player", "688", "522", "9.0.0", "flash/expressinstall.swf", promoflashvars, params, attributes);
		} else {
			if (typeof videoUrl === 'undefined') {
				swfobject.embedSWF( "/flash/videoplayer.swf", "video-player", "688", "522", "9.0.0", "flash/expressinstall.swf", flashvars, params, attributes);
			} else {
				flashvars.videoURL = videoUrl;
				swfobject.embedSWF( "/flash/videoplayer.swf", "video-player", "688", "522", "9.0.0", "flash/expressinstall.swf", flashvars, params, attributes);
			}
		}		
	},
	
	init: function(openLinks){
		video.setGlobals();
		video.setOpenLinks();
	}
};