//Functions
function mycarousel_initCallback(carousel) {
		$("#prodcaro-ctrl a").bind('click', function() {
				carousel.scroll($.jcarousel.intval($(this).attr("rel")));
				return false;
		});
};	
function mycarousel_itemVisibleInCallbackBeforeAnimation(carousel, item, idx, state) {
		// No animation on first load of the carousel
		if (state == 'init')
				return;
	 $('img', item).fadeIn('fast');
};
function mycarousel_itemVisibleOutCallbackBeforeAnimation(carousel, item, idx, state) {
	 $('img', item).fadeOut('fast');
};
function mycarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {
		//display('Item #' + idx + ' is now visible');
		$("#prodcaro-ctrl a").removeClass('active');
		$("#prodcaro-ctrl a[rel=" + idx+ "]").addClass('active');
		
};

//Close Form Panel
function closeSend() {
		$("#sendwrapper").slideToggle("fast");
		setTimeout(function() { resetSend(); }, 2000);
}		
//Reset Form
function resetSend() {
		$("#sendthanks").hide();					
		resetFields('#sendform');
		$("#sendform").show();
}			

//Reset Form Fields
function resetFields(id) {
	$(id).each(function(){
					this.reset();
	});
}		

//Close Form Panel
function closePanel() {
		$("#subpanel").slideToggle("fast");
		setTimeout(function() { resetForm(); }, 2000);
}		
//Reset Form
function resetForm() {
		$("#subthanks").hide();					
		resetFields('#subform');
		$("#subform").show();
}			





$(document).ready(function(){												 
				
	//Search 
	$("#sbutt").attr("value","");	
	$("#keywords").focus(function() {
		$(this).attr("value","");
	});
											 					
	//Project Slideshow				
	$('#banner-big').cycle({ fx:  'fade', random: 1, timeout: 8000});

	//Page Heights
	$(function(){
		 if ($('#prodnav').height() >= $('#page').height()) {
			$('#page').height($('#prodnav').height());
		 } 
		});
	
	//Home Promo Heights
	maxpromo = 0;
	$('div.promo-txt').each(function(index) {
		if ($(this).height()	>= maxpromo) {										 
    	maxpromo = $(this).height();
		}
  });
	maxpromo += 15;
	$('div.promo-txt').height(maxpromo)

	
	// Accordion
	$("#subpanel").hide();
	$("h3 a.butt-sign, a.butt-subclo").click(function() {
      $("#subpanel").slideToggle("fast");
			return false;	
		});	
	
	//Subscription Form
	$("#subform input.frmfld").focus(function() {
		$(this).attr("value","");
	});	
	
	// Subscription Validation
	$("#submitter").click(function() {
		// Set Validation Variables
		var error = 0;
		var msg ='';
		var errormsg = 'Please correct the following issues and try again:\n\n';			
		
		// Run Checks	
		// Name
		if (document.getElementById('name').value.length == 0 || document.getElementById('name').value == 'Name') { 
			error = 1;
			msg = 'Name field cannot be blank\n';
			errormsg += msg;
		}				
		// Email
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('email').value) == false) { 
			error = 1;
			msg = 'Email Address must be valid\n';
			errormsg += msg;	
		}			

		// Age
			if (error == 0 && document.getElementById('age').value == 'Age') { 
			document.getElementById('age').value = '';
		}			
		
		// Error
		if (error == 1) {
			alert(errormsg);			
		}
		else {
			
			// Grab form action
			formAction = $("form#subform").attr("action");			
			// Serialize form values to be submitted with POST
			var str = $("form#subform").serialize();			
			// Add form action to end of serialized data
			final = str + "&action=" + formAction;			
			//Hide the Form
			$("#subform").fadeOut('slow');
			// Submit the form via ajax
			$.ajax({
				url: document.getElementById('subcog').value,
				type: "POST",
				data: final,
				success: function(html){
					$("#subthanks").fadeIn('slow');
					setTimeout(function() { closePanel(); }, 3000);
				}
			});
		}		  	
		return false;														 		
	}); 

	// Recipe Lists
	$("ul.bicol li:even").addClass("alt");
		
	$("#prodcaro").jcarousel({
			scroll: 1,
			initCallback: mycarousel_initCallback,
			itemVisibleInCallback: {
					onBeforeAnimation: mycarousel_itemVisibleInCallbackBeforeAnimation,
					onAfterAnimation:  mycarousel_itemVisibleInCallbackAfterAnimation
			},
			itemVisibleOutCallback: {
					onBeforeAnimation: mycarousel_itemVisibleOutCallbackBeforeAnimation
			}
			
	});



	$("#sendwrapper").hide();
	$("a.butt-send").click(function () {
				$("#sendwrapper").slideToggle("fast");
				return false;
	});
	
// Subscription Validation
	$("#sender").click(function() {
		// Set Validation Variables
		var error = 0;
		var msg ='';
		var errormsg = '<b>Please correct the following issues and try again:</b><br />';			
		
		// Run Checks	
		// Your Name
		if (document.getElementById('yname').value.length == 0) { 
			error = 1;
			msg = 'Your Name field cannot be blank<br />';
			errormsg += msg;
		}				
		// Your Email
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('yemail').value) == false) { 
			error = 1;
			msg = 'Your Email must be valid<br />';
			errormsg += msg;	
		}			
		// Your Name
		if (document.getElementById('fname').value.length == 0) { 
			error = 1;
			msg = 'Friends Name field cannot be blank<br />';
			errormsg += msg;
		}				
		// Your Email
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('femail').value) == false) { 
			error = 1;
			msg = 'Friends Email must be valid<br />';
			errormsg += msg;	
		}				
		
		
		if (error == 1) {
			// Error
			document.getElementById('senderr').innerHTML = '<p>' + errormsg + '</p>';
		}	 else {
				//Bind Form to Ajax
				$('#sendform').ajaxForm(function() { 
						$("#sendform").hide();	
						$("#sendthanks").show();
						setTimeout(function() { closeSend(); }, 3000);
				});
				$('#sendform').submit();			
		}
		return false;
	});	
	
	

	// Popup Subscription	
	$("#subtrig").fancybox({
		'overlayColor' : '#000000',
		'overlayOpacity'	: 0.7,
		'padding' :  0,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});		
	$("#pop-subterms a").click(function () {
		$.fancybox.close();
	});	

	// Subscription Validation
	$("#pop-submitter").click(function() {
		// Set Validation Variables
		var error = 0;
		var msg ='';
		var errormsg = 'Please correct the following issues and try again:\n\n';			
		
		// Run Checks	
		// Name
		if (document.getElementById('pop-name').value.length == 0) { 
			error = 1;
			msg = 'Name field cannot be blank\n';
			errormsg += msg;
		}				
		// Email
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('pop-email').value) == false) { 
			error = 1;
			msg = 'Email Address must be valid\n';
			errormsg += msg;	
		}			
		
		// Error
		if (error == 1) {
			alert(errormsg);			
		}
		else {
			
			// Grab form action
			formAction = $("form#pop-subform").attr("action");			
			// Serialize form values to be submitted with POST
			var str = $("form#pop-subform").serialize();			
			// Add form action to end of serialized data
			final = str + "&action=" + formAction;			
			//Hide the Form
			$("#pop-subform").fadeOut('slow');
			// Submit the form via ajax
			$.ajax({
				url: document.getElementById('pop-subcog').value,
				type: "POST",
				data: final,
				success: function(html){
					$("#pop-subthanks").fadeIn('slow');
					setTimeout(function() { $.fancybox.close(); }, 3000);
				}
			});
		}		  	
		return false;														 		
	}); 	

	//Load Popup if cookies enabled and cookie not set
	$.cookie("cookact", 1, { path: '/'});
	if (($.cookie("cookact") == 1) && ($.cookie("popcook") != 1))
	{
		$("#subtrig").trigger('click');		
		$.cookie("popcook", 1, { path: '/', expires: 365});
	}

	
});





