//On Hover Over
function megaHoverOver(){
	$(this).find(".sub").stop().fadeTo(100, 1).show(); //Find sub and fade it in
	$(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
}
//On Hover Out
function megaHoverOut(){
  $(this).find(".sub").stop().fadeTo(50, 0, function() { //Fade to 0 opactiy
	  $(this).hide();  //after fading, hide it
  });
}

$(document).ready(function() 
{

	//Set custom configurations
	var config = {
		 sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
		 interval: 100, // number = milliseconds for onMouseOver polling interval
 		 timeout: 800, // number = milliseconds delay before onMouseOut
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};

	//$("ul#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations

	/**
	* <Image slider>
	*/
	if(!!$.prototype.easySlider){
		$("#slider").easySlider({
			auto: true,
			continuous: true,
			numeric: true,
			nextId: "slider1next",
			prevId: "slider1prev"
		});

		$("#slider2").easySlider({
			numeric: true
		});
	}
	
	/**
	* </Image slider>
	*/

	/**
	 * <Notification>
	 */
	$('#add_notifier').click(function() {
		var error = false;

		if( $('#notifier_email').val() == '' ||
			!$('#notifier_email').val().match(/^([a-zA-Z0-9])(([a-zA-Z0-9])*([\._\+-])*([a-zA-Z0-9]))*@(([a-zA-Z0-9\-])+(\.))+([a-zA-Z]{2,4})+$/)){
			error = true;
			setElementError( $('#notifier_email'));
		}

		if($('#notifier_name').val() == ''){
			error = true;
			setElementError($('#notifier_name'));
		}

		if($('#notifier_phone').val() == '' && $('#notify_sms').attr('checked')){
			error = true;
			setElementError($('#notifier_phone'));
		}

		if(error) return;

		var data = serializeData('notifyOnActiveForm');
		data+='&product_id='+$('#product_id').val();
		
		$('#notifyOnActiveForm #notifier_loading').show();

		$.ajax({
			type: 'POST',
			url: $('#notifyOnActiveForm #action_notify').val(),
			data: data,
			success: function(response){
				$('#notifyOnActiveForm #notifier_loading').hide();
				if(response.errors!=''){
					
					$('#notifier_error').html(response.errors);

					$('#notifier_error').toggle('show');
					setTimeout( function(){
						$("#notifier_error").fadeOut("slow");
					}, 8000);

				} else if(response.success){
					$('#notifyOnActiveForm').html(response.message);
				}
			},
			dataType: 'json'
		});

	});

	$('#notify_sms').change(function(){
		$('#notify_sms_table').toggle();
	});

	$('#notifier_phone').keypress(function(event) {

		if (($('#notifier_phone').val().length >= 12 || event.which < 48 || event.which > 59) && event.which!=8 && event.which!=0) {
			event.preventDefault();
		}
	});
	/**
	 * </Notification>
	 */
});


function serializeData(id){
	var toReturn    = [];
	var els         = $('#'+id).find(':input').get();
	$.each(els, function() {
		if (this.name && !this.disabled && (this.checked || /select|textarea/i.test(this.nodeName) || /text|hidden|password/i.test(this.type))) {
			var val = $(this).val();
			toReturn.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( val ) );
		}
	});
	return  toReturn.join("&").replace(/%20/g, "+");
}

function setElementError(el){
	var error_style = '2px solid #C8352A';
	var normal_style = '2px inset #EEE';

	el.css('border',error_style);
	setTimeout(function(){
		el.css('border',normal_style);
	}, 3000);
	return;
}

