var video = {
	setGlobals: function() {
		video.openLinks = jQuery('a[rel="open-video"]');
		video.closeRel = 'close-video-window';
		video.videoWindowHTML = '<div id="video-window"><a href="#" id="close-link" rel="' + video.closeRel + '">close window</a><a id="view-collection" href="" class="displayNone">View Collection</a><div id="video-player"></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, videoHeight) {
		var vUrl = videoUrl;
		jQuery('div#wrapper').append(video.videoWindowHTML);
		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); }
		);
		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('main-flash');
		}
		
		brizo.sideNav.fadeOut('fast');
		brizo.scrollerTitles.fadeOut('fast');
		brizo.thumbWrapper.fadeOut('fast');		
		video.vw.animate({ opacity: 1, top: '120px', left: '214px', width: '688px', height: '542px' }, 100, 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();
			}			
		}).css({ zIndex: 79 });
	},
	
	// 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(){			
			video.vw.remove();
			brizo.sideNav.fadeIn('fast');
			brizo.thumbWrapper.fadeIn('fast');
			// If we're on the homepage, we need to reinstate the existing Flash
			if(jQuery('body#home').length) {
				jQuery('#content-body').append('<div id="main-flash"></div>');
				home.animateFlash();
			}
			brizo.scrollerTitles.fadeIn('fast', function(){
				jQuery('#thumb_scroller').css({ width: '680px', display: 'block', opacity: 1 });
			});
			
		});
	},
	
	setOpenLinks: function() {
		video.openLinks.bind('click', function(){
			brizo.hideContentBody();
			video.maskBackLink();
			var vUrl = jQuery(this).attr('href');
			var $thisId = jQuery(this).attr('id');
			var $this = jQuery(this);
			setTimeout(function(){ video.openVideo(vUrl); video.grabCollAndRoom($this, $thisId); }, 100);
			return false;
		});
	},

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

	playVideo: function(videoUrl) {
		// If we're on the homepage, add autoplay param
		if(jQuery('body#home').length) {
			flashvars.videoURL = videoUrl;
			flashvars.playerAutoPlay = "true";
			swfobject.embedSWF( "/flash/videoplayer.swf", "video-player", "688", "522", "9.0.0", "flash/expressinstall.swf", flashvars, 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();
	}
};