$(document).ready(function() {
	

	var is_iphone = 0;
	var agent=navigator.userAgent.toLowerCase();
	is_iphone = (agent.indexOf('iphone')!=-1);

	$.ajax({
		url: 'loader.php',
		type: 'POST',
		dataType: 'json',
		data: 'request=load&ref='+document.referrer+'&iphone='+is_iphone
	});
	
	$('.banner').cycle('fade');
	
	$('#webown').bind('click', function(){
		$(this).text('Full Details Coming Soon!');
		web = setTimeout(function(text){
			$('#webown').text('Website Owner? Click Here!');
		}, 1000);
		$.ajax({
			url: 'loader.php',
			type: 'POST',
			dataType: 'json',
			data: 'request=owner',
		});
		return false;
	});
	
	$('#meat').bind('change', function(){
		if($('#meat option:selected').hasClass('nostyle')){
			$('#style').attr('disabled', true);
		}else{
			$('#style').attr('disabled', false);
		}
	});
	
	$('#unit').bind('change', function(){
		if($(this).val() == 'kg'){
			$('#w1').text('(kilos)');
			$('#w2').text('(grams)');
		}else if($(this).val() == 'lb'){
			$('#w1').text('(lbs)');
			$('#w2').text('(oz)');
		}
	});

	$('#gettime').bind('click', function(){
		if(timer > 0){
			if(unlockOn != 1){
				$('#data').html('<p style="text-align:center;"><img src="ajaxloader.gif"></p>');
				unlock();
			}
		}else{
			if(ajaxOn != 1){
				$('#data').html('<p style="text-align:center;"><img src="ajaxloader.gif"></p>');
				$('#result').slideDown('slow');
//				if($('#email').val() == ''){
//					$('#data').html("");
//				}else{
					ajaxOn = 1;
					ajaxTimer = setTimeout(function(){
						ajaxOn = 0;
					}, 10000);
					$(this).parents('form').ajaxSubmit({
						success: function(data){
							ajaxOn = 0;
							clearTimeout(ajaxTimer);
							timer = 5;
							lock();
							$('#data').html(data);
						},
						dataType:  'html',
						resetForm: false,
						error: function(){
							ajaxOn = 0;
							clearTimeout(ajaxTimer);
							$('#data').html("Sorry we've encountered an error, please try again.");
							$('#result').slideDown('slow');
						}
					});
//				}
			}
		}
		return false;
	});

	$('.pic').hide();
});

var ajaxOn = 0;
var ajaxTimer;
var timer;
var timeout;
var count;
var unlockOn = 0;

function unlock(){

	unlockOn = 1;
	clearTimeout(timeout);
	count = setTimeout(function(){
		if(timer < 1){
			$('#data').html('<h3 style="text-align:center;color:green;">Calculator Unlocked!</h3>');
			clearTimeout(count);
			timer = 0;
			unlockOn = 0;
		}else{
			$('#data').html('<h3 style="text-align:center;color:red;">**SPAM PROTECTION**</h3><p style="text-align:center;">auto unlock in.. '+timer+' secs</p>');
			timer--;
			unlock();
		}
	}, 1000);
}

function lock(){

	clearTimeout(count);
	unlockOn = 0;
	timer--;
	timeout = setTimeout(function(){
		lock();
	}, 1000);
}
