var experience = {
	setGlobals: function() {
		adCampaignLi = jQuery('a#link-ad-campaign').parent('li');
		productVideosLi = jQuery('a#link-product-videos').parent('li');
		vidLi = jQuery('a#link-essence-video').parent('li');
		animateVisible = '0px';
		animateOffLeft = '-638px';
		animateOffRight = '738px';
		
		expPhotos = jQuery('div#experience-photos');
		campaignWindow = jQuery('div#campaign-window');
		productVideosWindow = jQuery('div#product-videos-window');
	},

	setAdCampaign: function() {	
		// Variable assignment
		var campaignLinks = jQuery('a[rel="open-campaign"]');
		var navCampaignLink = jQuery('a#link-ad-campaign');
		var imageLinks = jQuery('ul#catalog-thumbs a');
		var largeContainer = jQuery('div#catalog-large-image');
		var collectionLinks = jQuery('a.collection-link');
		var defaultCollectionLink = collectionLinks.eq(0);		

		// Array to store image paths
		var largeImages = [];

		// Add video close action to Ad Campaign link
		campaignLinks.attr('rel', 'close-video-window');

		var _images = [];

		// store images paths in array
		imageLinks.each(function(i){
			largeImages[i] = this.href;
			_images.push(largeImages[i]);
		});

		// Hide Collection links
		collectionLinks.hide();

		// Add additional HTML/CSS
		largeContainer.append('<img src="'+ largeImages[0] +'" class="essence-photo" />');
		defaultCollectionLink.show().css({ opacity: 0 });
		campaignWindow.css({ position: 'absolute', left: animateOffRight, top: 0, width: '688px', height: '534px', opacity: 0, zIndex: '60' });

		// Set up image links
		imageLinks.bind('click', function(){
			var newImg = this.href;
			brizo.changePhoto(jQuery('div#catalog-large-image img'), newImg);
			collectionLinks.hide();
			jQuery('a#essence-'+ jQuery(this).attr('href').replace('/images/pic/experience/', '').replace('.jpg', '') +'-link').show();
			return false;
		});

		// Slide Ad Campaign into view
		function showCampaign() {
			campaignActive = 1;
			
			if(productVideosActive == 1) {
				productVideosActive = 0;
				productVideosWindow.removeClass('product-videos-activated');
			
				campaignWindow.addClass('campaign-activated');
				productVideosWindow.animate({ left: -250, opacity: 0 }, 350, 'easeInBack', function(){
					defaultCollectionLink.animate({ opacity: 1 }, 500);
					productVideosWindow.css({ left: animateOffRight });
				});
				campaignWindow.animate({opacity: 0}, 200).animate({ left: animateVisible, opacity: 1 }, 350, 'easeOutExpo');
			
			} else {

				experience.showBackLink();
				campaignWindow.addClass('campaign-activated');
				expPhotos.animate({ left: animateOffLeft, opacity: 0 }, 350, 'easeInBack', function(){
					defaultCollectionLink.animate({ opacity: 1 }, 500);
					expPhotos.css({ top: '1500px' });
				});
				campaignWindow.animate({opacity: 0}, 200).animate({ left: animateVisible, opacity: 1 }, 350, 'easeOutExpo');
			}
		}

		// Set up links which show Ad Campaign
		campaignLinks.bind('click', function(){
			// Set delay for campaign animation if needed
			var campaignDelay = (jQuery('div#video-window').length && !jQuery('div.campaign-activated').length) ? 275 : 0;

			setTimeout(function(){
				jQuery('#sidenav > li').removeClass('active');
				adCampaignLi.addClass('active');
				// Make sure campaign window isn't already open
				if(!jQuery('div.campaign-activated').length) {
					brizo.preloadImages(_images);
					showCampaign();
					return false;
				}
			}, campaignDelay);
			return false;
		});

		function closeCampaign() {
			campaignActive = 0;
			jQuery('a#link-ad-campaign').parent('li').removeClass('active');
			defaultCollectionLink.animate({ opacity: 0 }, 100);
			expPhotos.css({ top: '62px' });
			brizo.hideBackLink();
			campaignWindow.removeClass('campaign-activated');
			campaignWindow.animate({ opacity: 0, left: animateOffRight }, 350, 'easeInBack');
			expPhotos.animate({opacity: 0}, 200).animate({ opacity: 1, left: animateVisible }, 350, 'easeOutExpo');
		}

	},
	
	autoOpen: function() {
		if((document.location.hash) && (document.location.hash === '#open-ad-campaign')) {
			experience.showCampaign();
		}
	},
	
	setVideo: function() {
		video.init();
	},
	
	setProductVideos: function() {
	
		// Variable assignment
		var productVideosLinks = jQuery('a[rel="open-product-videos"]');
		var navProductVideosLink = jQuery('a#link-product-videos');
		
		// Add video close action to Product Videos link
		productVideosLinks.attr('rel', 'close-video-window');		

		
		var videoCount = jQuery('div#product-videos-window ul li').length;
		
		
		// Add additional HTML/CSS
		productVideosWindow.css({ position: 'absolute', left: '600px', top: 0, width: '688px', height: '534px', opacity: 0 });
		productVideosWindow.append(
			jQuery('<div />').attr({ id: 'product-videos-pagination' }).text('1-3 of '+videoCount)
		);
		var videoPager = jQuery('#product-videos-pagination', productVideosWindow);
		
		
		// functions for video carousel callbacks
		function videosCarousel_initCallback(carousel) {
			//alert('init callback');
		}
		function videosCarousel_itemFirstInCallback(carousel, li, index, state) {
			//alert(videoPager);
			videoPager.text(index+'-'+(index+2)+' of '+videoCount);
		}
		
		// Initialize video carousel
		jQuery('div#product-videos-window ul').jcarousel({
			initCallback: videosCarousel_initCallback,
			itemFirstInCallback: videosCarousel_itemFirstInCallback
		});

		//Video title rollover for IE6
		jQuery('li a', productVideosWindow).hover(
			function(){
				jQuery('.video-title', this).css({ backgroundPosition: '-200px 0' });
			},
			function(){
				jQuery('.video-title', this).css({ backgroundPosition: '0 0' });
			}	
		);

		// Slide Product Videos into view
		function showProductVideos() {
			productVideosActive = 1;
			
			// "Back" link functionality
			backLink.unbind();
			backLink.bind('click', function(){
				closeProductVideos();
				return false;
			});
			
			if(campaignActive == 1) {
				campaignActive = 0;
				campaignWindow.removeClass('campaign-activated');
			
				productVideosWindow.addClass('product-videos-activated');
				campaignWindow.animate({ left: -250, opacity: 0 }, 350, 'easeInBack', function(){
					campaignWindow.css({ left: animateOffRight });
				});
				productVideosWindow.animate({opacity: 0}, 200).animate({ left: animateVisible, opacity: 1 }, 350, 'easeOutExpo');
			
			} else {
				
				experience.showBackLink();
				productVideosWindow.addClass('product-videos-activated');
				expPhotos.animate({ left: animateOffLeft, opacity: 0 }, 350, 'easeInBack', function(){
					expPhotos.css({ top: '1500px' });
				});
				productVideosWindow.animate({opacity: 0}, 200).animate({ left: animateVisible, opacity: 1 }, 350, 'easeOutExpo');
			}
		}
		
		// Set up links which show Product Video Carousel
		productVideosLinks.bind('click', function(){
			// Set delay for campaign animation if needed
			var productVideosDelay = (jQuery('div#video-window').length && !jQuery('div.product-videos-activated').length) ? 275 : 0;

			setTimeout(function(){
				jQuery('#sidenav > li').removeClass('active');
				productVideosLi.addClass('active');
				// Make sure campaign window isn't already open
				if(!jQuery('div.product-videos-activated').length) {
					showProductVideos();
					return false;
				}
			}, productVideosDelay);
			return false;
		});
		
		function closeProductVideos() {
			productVideosActive = 0;
			navProductVideosLink.parent('li').removeClass('active');
			productVideosWindow.removeClass('product-videos-activated');
			expPhotos.css({ top: '62px' });
			brizo.hideBackLink();
			//expPhotos.animate({ opacity: 1 }, 350, 'easeOutExpo');
			productVideosWindow.animate({ left: animateOffRight, opacity: 0 }, 350, 'easeInBack');
			expPhotos.animate({opacity: 0}, 200).animate({ opacity: 1, left: animateVisible }, 350, 'easeOutExpo');
		}

	},
	
	urlOpenVideo: function(){
		if(document.location.hash.indexOf('smarttouch') != -1) {
			setTimeout(function(){
				jQuery('#product-videos-pic').click();
			}, 500);			
			setTimeout(function(){
				jQuery('#coll-smarttouch-technology').click();
			}, 1000);
		}
		if(document.location.hash.indexOf('video') != -1) {
			setTimeout(function(){
				jQuery('#promo-video-pic').click();
			}, 500);
		}
	}
};



// Initialize JS functions
$(document).ready(function(){
	experience.setGlobals();
	experience.setVideo();
	experience.setAdCampaign();
	experience.setProductVideos();
	experience.urlOpenVideo();
	//experience.autoOpen();
});