/* --- TEST FOR IE VERSION -- */
function isIE() {
	// Returns the version of Internet Explorer or a -1
	// (indicating the use of another browser).
	var rv = -1; // Return value assumes failure.
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null) {
			rv = parseFloat( RegExp.$1 );
		}
	}
	return rv;
}

/* --- TWITTER FISH --- */
function showTilly(type) {
	var ifie2 = isIE();
    if((ifie2 > 6 || ifie2 == -1) && type == 'TillyAnimation') {
	  $('#tilly-anim').flash({
		src: 'http://eattilapia.com/cms/wp-content/themes/under-the-sea/flash/'+type+'.swf',
		width: 115,
		height: 80,
		menu: false,
		wmode: 'transparent'},
		{ version: 8 },
		function(htmlOptions) {	
			var $thisani = $('#tilly-anim img');
			htmlOptions.flashvars = 'wmode=transparent&allowscriptacess=never';
			$thisani.before($.fn.flash.transform(htmlOptions));
			$thisani.css('display','none');
			$('#tilly').css('display','block');
		}
	);
	} else {
		$('#tilly').css('display','block');
	}
}

$(document).ready(function(){
	/* EXTERNAL LINKS */
	$('a[rel="external"]').attr('target','_blank');
	$('form#newsletter-signup, form#newsletter-signup2').attr('target','_blank');
	
	/* FORMS */
	$('input[type="text"], textarea').focus(function() {
		$(this).val('');
		$(this).removeClass('unfocused');
	});
	
	$('input#go, input#formGo, input#go2').hover(
		function(){ $(this).attr('class','hoveron') .css('cursor','pointer'); },
		function(){ $(this).attr('class','hoveroff') .css('cursor','default'); }
	);
	
	$('form#nutritionist-form').submit(function(){
		var email = $("input#formEmail").val();
		var name = $("input#formName").val();
		var zip = $("input#formZip").val();
		var question = $("input#formQuestion").val();
		if((email === "") || (email == "(Type your email here)") || (email == "This field is required.")) {
			$("input#formEmail").focus();
			$("input#formEmail").val("This field is required.");
			return false;
		} else if((name === "") || (name == "(Your Name)") || (name == "This field is required.")) {
			$("input#formName").focus();
			$("input#formName").val("This field is required.");
			return false;
		} else if((zip === "") || (zip == "(ZIP Code)") || (zip == "This field is required.")) {
			$("input#formZip").focus();
			$("input#formZip").val("This field is required.");
			return false;
		} else if((question === "") || (question == "(Type your question here)") || (question == "This field is required.")) {
			$("input#formQuestion").focus();
			$("input#formQuestion").val("This field is required.");
			return false;
		} else {
			var dataString = $(this).serialize();
			//window.alert(dataString);
			$("#formResponse").load('http://eattilapia.com/cms/wp-content/themes/under-the-sea/scripts/email-form.php?'+dataString);
			return false;
		}
	});

	/* NAV-SUBNAV HIGHLIGHTING */
	var subnavs = ['about','recipes','products','press','sustainability','operations','contact'];
	var path = document.location.pathname.split('/');
	var folder = path[1];
	var subfolder = path[2];
	if(folder !== '') { //If Level 2 Page
		$('#nav-' + folder + ' a').attr('id','active');
		if(jQuery.inArray(folder,subnavs) > -1) { //If Category with Subnavs
			$('#subnav-' + folder).css('display','');
			if(subfolder !== '') { //Highlight Subnav Category Name
				$('#sub-' + subfolder + ' a').attr('id','selected');
			}
		}
	}
	//Rollover Functionality - Subnavs
	$('#nav li a').hover(
		function() {
			//turn off all other subnavs + main nav
			$('.subnav').css('display','none');
			//turn on selected subnav if applicable (main nav hovers on)
			var mainID = $(this).parent().attr('id');
			var main = mainID.split('-');
			if(jQuery.inArray(main[1],subnavs) > -1) {
				$('#subnav-'+main[1]).css('display','');
			}
		},
		function() {
			//turn off unfocused subnav if applicable (main nav hovers off)
			var mainID = $(this).parent().attr('id');
			var main = mainID.split('-');
			if(jQuery.inArray(main[1],subnavs) > -1) {		
				$('#subnav-'+main[1]).css('display','none');
			}
			//if on Level 2 Page, turn back on
			if(jQuery.inArray(folder,subnavs) > -1) {
				$('#nav-'+folder+ ' a').attr('id','active');
				$('#subnav-'+folder).css('display','');
			}
		}
	);
	$('.subnav').hover(
		function() {
			//turn off all other subnavs + main nav
			$('.subnav').css('display','none');
			//turn on selected subnav + main nav
			$(this).css('display','');
			var mainID = $(this).attr('id');
			var main = mainID.split('-');
			$('#nav-'+main[1]+ ' a').attr('id','active');
		},
		function() {
			//turn off unfocused subnav + main nav
			var mainID = $(this).attr('id');
			var main = mainID.split('-');
			if(jQuery.inArray(main[1],subnavs) > -1) {		
				$(this).css('display','none');
				$('#nav-'+main[1]+ ' a').attr('id','');
			}
			//if on Level 2 Page, turn back on
			if(jQuery.inArray(folder,subnavs) > -1) {
				$('#nav-'+folder+ ' a').attr('id','active');
				$('#subnav-'+folder).css('display','');
			}
		}
	);
	/* FRONT PAGE HEADER SLIDESHOW */
	if(folder === '') {
		$('#header-image-home').cycle({
			fx: 'fade',
			sync: true,
			delay: 200,
			timeout: 4000
		});
	}	
	/* DISTRIBUTION MAP SWF */
	if(folder == 'operations' && subfolder == 'distribution') {
		$.preload([ 'MapAnimation' ], {
			base: 'http://eattilapia.com/cms/wp-content/themes/under-the-sea/flash/',
			ext:'.swf',
			onComplete:function(){
				$('#distribution-map').flash({
					src: 'http://eattilapia.com/cms/wp-content/themes/under-the-sea/flash/MapAnimation.swf',
					width: 590,
					height: 590,
					wmode: 'transparent'
				});
			},
			onFinish:function(){
				$('#distribution-map').css('display','block');	
			}
		});
	}
});

/* --- RECIPE MENU FUNCTIONS --- */
function showCarousel(type) {
	//turn off all types
	$('.carousel').css('display','none');
	$('#recipe-tabs li a').attr('id','');
	//turn on selected type
	$('#recipe-carousel-'+type).css('display','');
	$('li#link-'+type+' a').attr('id','current');
	//set up carousel
	$('#recipe-carousel-'+type).smoothDivScroll({
		visibleHotSpots: 'always',
		autoScroll: 'onstart',
		autoScrollDirection: 'endlessloopright',
		scrollStep: 2,
		scrollInterval: 65,
		autoScrollStep: 2,
		autoScrollInterval: 65,
		mouseDownSpeedBooster: 3
	});
	$('#recipe-carousel-'+type+' .scrollableArea').hover(
		function() { $('#recipe-carousel-'+type).smoothDivScroll('stopAutoScroll'); },
		function() { $('#recipe-carousel-'+type).smoothDivScroll('startAutoScroll'); }
	);
}

function showRecipe(id,type) {
	$('#recipe-carousel-'+type).smoothDivScroll('stopAutoScroll');
	window.scroll(0,0);
	$('#popups').fadeIn(500,function(){
		$('#rblock-'+id).fadeIn(500,function(){
			if($('#rvideo-'+id).length) { //If Video Post, Load IFrame
				var videoURL = $('#rvideo-'+id+' a');
				var video = videoURL.attr('href');
				var v = video.split('/v/');
				videoURL.hide();
				playVideo('rvideo-'+id,v[1]);
			}
		});
		$('#rclose-'+id).hover(
			function(){ $(this).css('cursor','pointer'); },
			function(){ $(this).css('cursor','default'); }
		);
		$('#rclose-'+id).click(function(){
			if($('#rvideo-'+id).length) { //If Video Post, Remove IFrame
				$('#rvideo-'+id+' iframe').remove();
				$('#rvideo-'+id+' a').show();
			}
			$('#popups').fadeOut(500,function(){
				$('#rblock-'+id).hide();
			});
		});
		$(window).keydown(function(e){
			if(e.which == 27){
				if($('#rvideo-'+id).length) { //If Video Post, Remove IFrame
					$('#rvideo-'+id+' iframe').remove();
					$('#rvideo-'+id+' a').show();
				}
				$('#popups').fadeOut(500,function(){
					$('#rblock-'+id).hide();
				});
			}
		});
	});
}

function popupHome(video) {
	window.scroll(0,0);
	$('#popups').fadeIn(500,function(){
		$('#hblock').fadeIn(500);
		$('#hclose').hover(
			function(){ $(this).css('cursor','pointer'); },
			function(){ $(this).css('cursor','default'); }
		);
		var v = video.split('/v/');
		playVideo('hvideo',v[1]);
		$('#hclose').click(function(){
			$('#hvideo').empty();
			$('#popups').fadeOut(500,function(){
				$('#hblock').hide();
				return false;
			});
		});
		$(window).keydown(function(e){
			if(e.which === 27){
				$('#hvideo').empty();
				$('#popups').fadeOut(500,function(){
					$('#hblock').hide();
					return false;
				});
			}
		});
	});
}

function playVideo(loc,video) {
	//Hide the link box, load video
	$('#'+loc+' a').hide();
	var iframe = '<iframe title="YouTube video player" width="735" height="438" src="http://www.youtube.com/embed/'+video+'" frameborder="0" allowfullscreen></iframe>';
	$('#'+loc).append(iframe);
}

function printRecipe(permalink) {
	window.open(permalink,'recipe','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=500,height=600,left=25,top=25');
}

function swapRecipeList() {
	$('.swaps').toggle(function(){
		$('#recipe-carousel-23').smoothDivScroll('startAutoScroll');
		$('#recipe-box-spacer').toggle(0);
	});
}

/* --- SEAFOOD HANDLING & NEWSLETTER LINK TAB FUNCTIONS --- */

function showTab(type,id,first,second) {
	first = first || 0;
	second = second || 1;
	//Remove Classes on all Tabs
	$('ul#'+type+'-tabs li a').attr('class','rNone sprite');
	//Change Classes based on Selection
	if(id == first) { //First Tab
		var nextid = id+1;
		$('li#tab-'+id+' a').attr('class','rSelect sprite');
		$('li#tab-'+nextid+' a').attr('class','rLeft sprite');
	} else if(id == second) { //Last Tab
		var previd = id-1;
		$('li#tab-'+previd+' a').attr('class','rRight sprite');
		$('li#tab-'+id+' a').attr('class','rSelect sprite');
	} else { //All Other Tabs
		var ltab = id-1;
		var rtab = id+1;
		$('li#tab-'+ltab+' a').attr('class','rRight sprite');
		$('li#tab-'+id+' a').attr('class','rSelect sprite');
		$('li#tab-'+rtab+' a').attr('class','rLeft sprite');
	}
	//Hide all Content Boxes Except Selected
	$('#'+type+'-content .box-inner').css('display','none');
	$('#'+type+'-'+id).css('display','');
}

/* --- SUSTAINABILITY POPUP FUNCTIONALITY --- */
function showSustainability(id) {
	window.scroll(0,325);
	$('#sblock-'+id).fadeIn(500,function(){
		$('.sclose').hover(
			function(){ $(this).css('cursor','pointer').css('color','#5184AF'); },
			function(){ $(this).css('cursor','default').css('color','#000000'); }
		);		
		$('.sclose').click(function(){
			$('#sblock-'+id).fadeOut(500);
		});
		$('.snext').hover(
			function(){ $(this).css('cursor','pointer'); },
			function(){ $(this).css('cursor','default'); }
		);
		$('.snext a').click(function(){
			var currPg = $('#sblock-'+id+' .scontent .scopy:visible').attr('id');
			//window.alert('Current: '+currPg);
			var currID = currPg.split('-');
			var nextID = parseInt(currID[2],10)+1;
			var lastPg = $('#sblock-'+id+' .scontent div:last-child').attr('id');
			//window.alert('Last Page: '+lastPg);
			var nextPg = '';
			if(currPg == lastPg) {
				nextPg = 'page-'+id+'-0';
			} else {
				nextPg = 'page-'+id+'-'+nextID;
			}
			//window.alert('Next: '+nextPg);
			$('#'+currPg).css('display','none');
			$('#'+nextPg).css('display','');
			return false;
		});
		$(window).keydown(function(e){
			if(e.which == 27){
				$('#sblock-'+id).fadeOut(500);
			}
		});
	});
}	
