/*
Custom functionality for Naked Ambition.com
By Eli Horne
http://elihorne.com
*/

// Safari glitch when it detects page height. Allows the overlay to vertically center
function getHeight() {
	var safariHeight = $('#wrap').outerHeight();
	var frameHeight = $(window).height();
	$('#wrap .content-wrap').height(safariHeight);
	$('#overlay').css({'top': '21.5%'});
	$('#flipOverlay').css({'top': '13%'});
}	

// Module post "filter" system from News and Store
function postFilter() {
	var linkText = $('.content-nav li a').text();	
	$('.content-nav li a').click(function() {
		$('.content-nav li a').removeClass('active');
		$(this).addClass('active');
		var filterClass = $(this).text();
		$('.post').each(function(){
			if($(this).hasClass(filterClass)) {
				$(this).slideDown('slow');
			} else {
			if( filterClass == 'all') {
				if($(this).is(':hidden')) {$(this).slideDown('slow')}
			} else {
					$(this).slideUp('slow');
				};
			};
		});
		return false;
	});
};

// General page setup (hides the flyout, distributes necessary class modifications)
function nakedAmbition() {
	$('#overlay').hide(); // hide the flyout
	$('#flipOverlay').hide(); // hide the flipbook flyout
	$('.content-nav li a em').each(function() { //get each content nav
		var contentNavClass = $(this).text().replace('/','-').replace('/','-'); // read the link text
		$(this).parent().addClass(contentNavClass); // apply the class to the parent
	});

				
	// Global call to kill background dimming				
	function brightenTheLights() {
		$('#lights-out').fadeOut('slow', function() { 
			$('#lights-out').css({'opacity': 1});
			$('#lights-out').remove(); 
			
		});
	};
						
	// Global call to create background dimming			
	function dimTheLights() {
		$('#wrap').prepend('<div id="lights-out"></div>');
		$('#lights-out').css({'opacity': 0});
		$('#lights-out').animate({
			opacity : 0.6,
		}, 700);
	};
					
					
	// Get hash command, trigger appropriate flyout or nav filtration				
	function getAnchor(){ // parse the url
		var url = window.location.hash; // get everything after the #
		
		if(url == '#trailer') {movieClick() }; // if its trailer, show the trailer
		if(url == '#book') {bookClick() }; // if its book, show the book
		if(url == '#flipbook') {flipBookClick() }; // if its book, show the book
	
		
		// todo: store detection should be more modular
		if(url == '#tickets') { // if its tickets (store)
			$('.content-nav li a.Tickets').addClass('active'); 
			$('.content-nav li a.all').removeClass('active');
			$('.post').each(function(){
				if($(this).hasClass('Tickets')) {
					$(this).slideDown('slow');
				} else {
					$(this).slideUp('slow');
				}
			})
		};
		if(url == '#books') { // if its books (store)
			$('.content-nav li a.Books').addClass('active'); 
			$('.content-nav li a.all').removeClass('active');
			$('.post').each(function(){
				if($(this).hasClass('Books')) {
					$(this).slideDown('slow');
				} else {
					$(this).slideUp('slow');
				}
			})
		};
		if(url == '#prints') { // if its books (store)
			$('.content-nav li a.Prints').addClass('active'); 
			$('.content-nav li a.all').removeClass('active');
			$('.post').each(function(){
				if($(this).hasClass('Prints')) {
					$(this).slideDown('slow');
				} else {
					$(this).slideUp('slow');
				}
			})
		};
		if(url == '#ppv') { // if its ppv (store)
			$('.content-nav li a.DVD-soundtrack-ppv').addClass('active'); 
			$('.content-nav li a.all').removeClass('active');
			$('.post').each(function(){
				if($(this).hasClass('DVD')) {
					$(this).slideDown('slow');
				} else {
					$(this).slideUp('slow');
				}
			})
		};
		if(url == '#dvd') { // if its soundtrack (store)
			$('.content-nav li a.DVD-soundtrack-ppv').addClass('active'); 
			$('.content-nav li a.all').removeClass('active');
			$('.post').each(function(){
				if($(this).hasClass('DVD')) {
					$(this).slideDown('slow');
				} else {
					$(this).slideUp('slow');
				}
			})
		};
		
		if(url == '#soundtrack') { // if its soundtrack (store)
			$('.content-nav li a.DVD-soundtrack-ppv').addClass('active'); 
			$('.content-nav li a.all').removeClass('active');
			$('.post').each(function(){
				if($(this).hasClass('DVD')) {
					$(this).slideDown('slow');
				} else {
					$(this).slideUp('slow');
				}
			})
		};
		if(url == '#apps') { // if its apps (store)
			$('.content-nav li a.Apps').addClass('active'); 
			$('.content-nav li a.all').removeClass('active');
			$('.post').each(function(){
				if($(this).hasClass('Apps')) {
					$(this).slideDown('slow');
				} else {
					$(this).slideUp('slow');
				}
			})
		};
	}; // end getAnchor
	getAnchor(); // init the function
	
	// Move the arrow out out the viewport				
	function arrowOut(){
		$('#tag-line').hide("slide", 
			{ direction: "left" },
			650);
	};
	
	// Move the arrow into the viewport
	function arrowIn(){
		$('#tag-line').show("slide", 
			{ direction: "left" },
			650);
	};
	
	// Global call to hide the overlay, and destroy the ajax temp loader element
	function closeOverlay(){
		$('.close').hide();	
		$('#overlay').hide("slide", { 
			direction: "down" 
		}, 650);
		$('.temp-loader').remove();
			return false;
	};
	
	function closeFlipOverlay(){
		/*$('.flipClose').hide();*/
		$('#lights-out').css({'z-index': 9});
		$('#flipOverlay').fadeOut('slow', function() { 
			$('#flipOverlay').css({'opacity': 1});
		});
		
		/*$('#flipOverlay').hide("slide", { 
			direction: "down" 
		}, 650);*/
			return false;
	};
	
	// Book Overlay
	function bookClick() {
		$('.r-rating').hide();
		$('.quote').hide();		
		if (($('#overlay').is(':hidden')) && ($('#flipOverlay').is(':hidden'))) {
			dimTheLights();
			arrowOut();
		}
		
		if ($('#flipOverlay').is(':visible'))
			closeFlipOverlay();
	
		if ($('#overlay:visible').hasClass('movie-toggle')) {
			closeOverlay();	
		}
		
		if ($('#overlay:visible').hasClass('book-toggle')) {
			$('#overlay').hide("slide", 
			{ direction: "down" },
			650);
			brightenTheLights();
			$('#tag-line').show("slide", 
			{ direction: "left" },
			650);
			return false;
		}
		
		$('.close').hide();
		$('#overlay').removeClass('movie-toggle');
		$('#overlay').removeClass('flip-toggle');
		$('#overlay').addClass('book-toggle');
		$('a.switch-tabs').text('Go To The Movie');
		$('#overlay').show("slide", 
			{ direction: "down" },
			650, function() { $('.close').fadeIn('slow') } );
		return false;
	};
	
	// Flipbook overlay				
	function flipBookClick(passedurl) {
		$('.r-rating').hide();
		$('.quote').hide();	
		
		if ($('#overlay').is(':hidden')) {
			dimTheLights();
			arrowOut();
		}
		
	
		if ($('#overlay:visible').hasClass('movie-toggle')) {
			closeOverlay();
		}
	
		/*if ($('#overlay:visible').hasClass('book-toggle')) {
			//alert ('book toggle visible');
			$('#overlay').removeClass('book-toggle');
			$('#overlay').addClass('flip-toggle');
			//closeOverlay();
		};*/
												
		$('.flipClose').hide();
		/*
		$('#overlay').removeClass('book-toggle');
		$('#overlay').removeClass('movie-toggle');*/
		$('#flipOverlay').addClass('flip-toggle');
		/*$('a.switch-tabs').text('Buy The Book');
		$('a.switch-tabs').attr({
			href: 'http://www.amazon.com/gp/product/0979331404/104-0034583-9936747?ie=UTF8&tag=nakeambibym0f-20&linkCode=xm2&camp=1789&creativeASIN=0979331404',
			target: '_blank'
		});*/
		$('#lights-out').css({'z-index': 16});
		$('#flip-bin').hide();
		$('#flipOverlay').fadeIn("slow", function() { 
				$('.flipClose').fadeIn('slow');
				
				$('#flip-player').prepend('<div class="temp-loader"><iframe id="flipbook" src="'+passedurl+'"></iframe></div>');
				
				$('#flip-bin').show();
			 }
		);
		return false;
	};
					
	// Movie overlay	
	function movieClick() {
		$('.r-rating').show();
		$('.quote').show();
		$('.temp-loader').remove();
		if (($('#overlay').is(':hidden')) && ($('#flipOverlay').is(':hidden'))) {
			dimTheLights();
			arrowOut();
		}
		
		if ($('#flipOverlay').is(':visible'))
			closeFlipOverlay();
		
		if ($('#overlay:visible').hasClass('book-toggle')) {
			closeOverlay();
		}
	
		if ($('#overlay:visible').hasClass('movie-toggle')) {
			$('#overlay').hide("slide", 
			{ direction: "down" },
			650);
			brightenTheLights();
			$('#tag-line').show("slide", 
			{ direction: "left" },
			650);
			return false;
		};
		
		$('.close').hide();
		$('#overlay').removeClass('book-toggle');
		$('#overlay').removeClass('flip-toggle');
		
		$('#overlay').addClass('movie-toggle');
		$('a.switch-tabs').text('Go To The Book');
		
		$('#overlay').show("slide", 
			{ direction: "down" },
			650, function() { 
				$('.close').fadeIn('slow');
				$('#vid-player').prepend('<div class="temp-loader"></div>');
				$('.temp-loader').load('/wp-content/themes/naked-ambition/trailer.php');
			 }
		);
		return false;
	};
	
	// Make the text shake
	function textShake() {
		$('#tag-line-text').effect("shake", { times:2 }, 700);
	};	
	
	// Init the function
	textShake();
					
	//
	// Misc. freeform functions, not wrapped
	//				
						
	// Overlay close button
	$('.close').click(function(){
		closeOverlay();
		arrowIn();
		brightenTheLights();
		return false;
	});
	
	$('.flipClose').click(function(){
		closeFlipOverlay();
		if ($('#overlay').is(':hidden')) {
			arrowIn();
			brightenTheLights();
		}
		return false;
	});
	
	// Nav book click
	$('#book-overlay').click(function(){
		bookClick();
		return false;
	});
	$('#nav li.book a').click(function(){
		bookClick();
		return false;
	});
	$('#store-inventory a.bookposter').click(function(){
		bookClick();
		return false;
	});
	
	// Nav movie click
	$('#movie-overlay').click(function(){
		movieClick();
		return false;
	});
	$('#nav li.movie a').click(function(){
		movieClick();
		return false;
	});
	$('#store-inventory a.movieposter').click(function(){
		movieClick();
		return false;
	});
	
	/* Thumbnail clicks (to trigger flipbook)*/
	$('.post.Books .thumbnail a#standardEd').click(function(){
		flipBookClick("http://www.nakedambition.com/newFlip/index.html?c=2");
		return false;
	});
	
	$('.post.Books .thumbnail a#collectorsEd').click(function(){
		flipBookClick("http://www.nakedambition.com/newFlipCollectors/index.html?c=2");
		return false;
	});
	
	$('#book-bin #standard-ed a.cover').click(function(){
		flipBookClick("http://www.nakedambition.com/newFlip/index.html?c=1");
		return false;
	});
	
	$('#book-bin #collectors-ed a.cover').click(function(){
		flipBookClick("http://www.nakedambition.com/newFlipCollectors/index.html?c=1");
		return false;
	});
	
	/* Edge cases when in the flyout
	$('#collectors-ed a').click(function(){
		brightenTheLights();
		closeOverlay();
		//window.location.reload();
	});*/
	
	$('#vid-copy a').click(function(){
		brightenTheLights();
		closeOverlay();
	});
	
	// Tab switcher
	$('a.switch-tabs').click(function(){
		$('#tag-line').hide();
		if($('#overlay').hasClass('movie-toggle')) {
			bookClick();
			return false;
		} else if($('#overlay').hasClass('flip-toggle')) {
			return true;
		} else {
			movieClick();
			return false;
		};
	});
};