var searchval, hoverInterval;
$(function(){
	$('#header .country ul')
		.hover(
			function(){
				clearTimeout(hoverInterval)
				$(this).addClass('hover')
			},
			function(){
				element = $(this);
				interval = setTimeout ("element.removeClass('hover')", 300);
			}
		)
	
	$('#header .search .input input')
		.focus(function(){
			$(this).addClass('focus').select()
		})
		.blur(function(){
			$(this).removeClass('focus')
		})
		
	$('a.fancybox').fancybox({
		padding: 3,
		showCloseButton: true,
		hideOnContentClick: false,
		hideOnOverlayClick: true,
		frameWidth: 684,
		frameHeight: 331
	})
	
	$('#right-product .next').live('click', function(){
		$('.step').hide()
		
		var currentDivId = $(this).parents('div.step').attr('id');

		if( currentDivId == "step2" || currentDivId == "step3" || currentDivId == "step4" || currentDivId == "step7" ) {
			$('input[name=' + currentDivId + ']').val( $(this).attr('name') )
		}

		if( currentDivId == "step4" ) {
			if( $(this).attr('href') == "#step8" ) {
				$('#step8 .button .prev').attr('href', '#step4');
			}
		}

		if( currentDivId == "step5" ) {
			var allergy = $('#right-product input[name=step5]').val();
			
			switch (allergy) {
				case "no":
				case "gluten":
					$('#step6 .input-wrapper').html('<ul class="radiobuttons"><li><a href="#" class="selected" name="chicken">' + RightProduct.chicken + '</a></li><li><a href="#" name="lamb">' + RightProduct.lamb + '</a></li><li><a href="#" name="salmon">' + RightProduct.salmon + '</a></li></ul>')
					$('input[name=step6]').val('chicken')
					break;
				case "protein":
				case "chicken":
					$('#step6 .input-wrapper').html('<ul class="radiobuttons"><li><a href="#" class="selected" name="lamb">' + RightProduct.lamb + '</a></li><li><a href="#" name="salmon">' + RightProduct.salmon + '</a></li></ul>')
					$('input[name=step6]').val('lamb')
					break;
				case "fish":
					$('#step6 .input-wrapper').html('<ul class="radiobuttons"><li><a href="#" class="selected" name="chicken">' + RightProduct.chicken + '</a></li><li><a href="#" name="lamb">' + RightProduct.lamb + '</a></li></ul>')
					$('input[name=step6]').val('chicken')
					break;
				case "lamb":
					$('#step6 .input-wrapper').html('<ul class="radiobuttons"><li><a href="#" class="selected" name="chicken">' + RightProduct.chicken + '</a></li><li><a href="#" name="salmon">' + RightProduct.salmon + '</a></li></ul>')
					$('input[name=step6]').val('chicken')
					break;
			}
			
			$('#right-product input[name=step5]').val($('a[name=' + allergy + ']').attr('title'));
		}

		if( $(this).attr('href') == "#step4" ) {
			if( $(this).attr('name') == "male" ) {
				$('#step4 .puppy .select').html('<select name="puppy"><option value="3">' + RightProduct.puppy_age_1 + '</option><option value="36">' + RightProduct.puppy_age_2 + '</option><option value="69">' + RightProduct.puppy_age_3_male + '</option></select>')
				$('#step4 .adult .desc').html(RightProduct.adult_age_male)
			} else {
				$('#step4 .puppy .select').html('<select name="puppy"><option value="3">' + RightProduct.puppy_age_1 + '</option><option value="36">' + RightProduct.puppy_age_2 + '</option><option value="69">' + RightProduct.puppy_age_3_female + '</option></select>')
				$('#step4 .adult .desc').html(RightProduct.adult_age_female)
			}
		}

		if( currentDivId == "step8" ) {
			var mr = [ 0, 98, 165, 223, 277, 328, 376, 422, 466, 509, 551, 592, 632, 671, 709, 747, 784, 820, 856, 892, 927, 961, 996, 1029, 1063, 1096, 1128, 1161, 1193, 1225, 1256, 1288, 1319, 1349, 1380, 1410, 1440, 1470, 1500, 1529, 1559, 1588, 1617, 1646, 1674, 1703, 1731, 1759, 1787, 1815, 1843, 1870, 1898, 1925, 1952, 1979, 2006, 2033, 2060, 2086, 2113, 2139, 2165, 2191, 2217, 2243, 2269, 2295, 2321, 2346, 2372, 2397, 2422, 2447, 2473, 2498]
			var pic = ['product_image_134x170_1.jpg', 'product_image_134x170_2.jpg', 'product_image_134x170_3.jpg', 'product_image_134x170_4.jpg', 'product_image_134x170_5.jpg']
			var coeff = [424, 393, 416, 420, 423] // puppy, senior, checken, lamb, salmon
			var coeff_id; // puppy, senior, checken, lamb, salmon
			var gr = 1;
			var high_text = "";
			var prefered_text = "";
			var link_url = "";
			
			var dogAge = $('#right-product input[name=step4]').val(); // possible values: puppy, senior, adult
			
			var product_name = dogAge.toUpperCase()

			if( dogAge == "puppy" ) {
				pic = pic[3]
				coeff_id = 0
				prefered_text = "chicken &amp; rice"
				link_url = RightProduct.puppy_chicken_rice_url
				
				var package = RightProduct.puppy_chicken_rice_package;
				var price = RightProduct.puppy_chicken_rice_price;
				var disclaimer = RightProduct.puppy_chicken_rice_disclaimer;
				
			} else if( dogAge == "senior" ) {
				pic = pic[4]
				coeff_id = 1
				prefered_text = "chicken &amp; rice"
				prefered_text = RightProduct.senior_chicken_rice_prefered; //"chicken &amp; rice"
				link_url = RightProduct.senior_chicken_rice_url
				
				var package = RightProduct.senior_chicken_rice_package;
				var price = RightProduct.senior_chicken_rice_price; 
				var disclaimer = RightProduct.senior_chicken_rice_disclaimer;
			} else if( dogAge == "adult" ) {
				if( $('#right-product input[name=step6]').val() == "chicken" ) {
					pic = pic[0]
					coeff_id = 2
					prefered_text = "chicken &amp; rice"
					link_url = RightProduct.adult_chicken_rice_url;
					
					var package = RightProduct.adult_chicken_rice_package;
					var price = RightProduct.adult_chicken_rice_price; 
					var disclaimer = RightProduct.adult_chicken_rice_disclaimer;
					
				} else if( $('#right-product input[name=step6]').val() == "lamb" ) {
					pic = pic[1]
					coeff_id = 3
					prefered_text = "lamb &amp; rice"
					link_url = RightProduct.adult_lamb_rice_url;
					var package = RightProduct.adult_lamb_rice_package;
					var price = RightProduct.adult_lamb_rice_price;
					var disclaimer = RightProduct.adult_lamb_rice_disclaimer;
					
				} else if( $('#right-product input[name=step6]').val() == "salmon" ) {
					pic = pic[2]
					coeff_id = 4
					prefered_text = "salmon &amp; rice"
					link_url = RightProduct.adult_salmon_rice_url;
					
					var package = RightProduct.adult_salmon_rice_package;
					var price = RightProduct.adult_salmon_rice_price;
					var disclaimer = RightProduct.adult_salmon_rice_disclaimer;
				}
			}
			
			if( dogAge == "puppy" )
				var puppyAge = $('#right-product select[name=puppy]').val();
				var puppyCoef = 0;
				//console.log(puppyAge)
				//3 36 69 912
	
				if (puppyAge == 3) puppyCoef = 2;
				else if (puppyAge == 36) puppyCoef = 1.6;
				else if (puppyAge == 69 || puppyAge == 912) puppyCoef = 1.2;
				
				gr = mr[$('#right-product input[name=step8]').val()] * puppyCoef / coeff[coeff_id] * 100 
			if( dogAge == "senior" )
				gr = mr[$('#right-product input[name=step8]').val()] / coeff[coeff_id] * 100
			if( dogAge == "adult" )
				gr = mr[$('#right-product input[name=step8]').val()] / coeff[coeff_id] * 100

			if( $('#right-product input[name=step7]').val() == "high" ) {
				gr = Math.round( gr * 1.37 )
				high_text = RightProduct.activity_level_high;
			} else {
				gr = Math.round( gr )
				high_text = RightProduct.activity_level_normal;
			}
				
				
				
			$('.result_dog_name').html($('#right-product input[name=step1]').val());
			$('.result_pic').attr('src', '/images/' + pic);
			$('.result_product_name').html(product_name + ' ' + prefered_text);
			$('.result_feeding').html(gr + ' g');
			
			$('.result_price').html(estimate_price = Math.round(price / package * gr));
			$('.result_disclaimer').html(disclaimer);
			
			$('.result_product_url').attr('href', link_url);
			
			// SAVE DATA
			
			$.get(RightProduct.root + '/products/register', $('form').serialize() + '&price=' + estimate_price + "&dosage=" + gr + "&href=" + link_url + '&prefered=' + escape(prefered_text));

			//if( $('#right-product input[name=step8]').val())		
			if( dogAge == "adult" ) {
				$('.result_allergies').html($('#right-product input[name=step5]').val());
				$('.result_prefered').html(prefered_text);
				$('.result_activity').html(high_text);
				
				$('.result_for_adult').show();
			} else if( dogAge == "puppy" ) {
				$('.result_activity').html(high_text);
				
				$('.result_for_puppy').show();
			} else if( dogAge == "senior" ) {
				
			}

	}

		$($(this).attr('href')).show()
		return false
	})
	$('#right-product .prev').live('click', function(){
		$('.step').hide()
		$($(this).attr('href')).show()
		return false
	})

	$('#right-product .radiobuttons a, #right-product .checkboxes a').live('click', function(){
		$(this).parents('ul').find('a').removeClass('selected')
		$(this).addClass('selected')

		$('input[name=' + $(this).parents('div.step').attr('id') + ']').val( $(this).attr('name') );
	
		if( $(this).parents('div.step').attr('id') == "step5" && $(this).attr('name') == "other" ) {
				$('#step5 .button .next').attr('href', '#step7')
				$('input[name=step6]').val('salmon');
				$('#step7 .button .prev').attr('href', '#step5');
		} else if( $(this).parents('div.step').attr('id') == "step5" ) {
				$('#step5 .button .next').attr('href', '#step6')
				$('#step7 .button .prev').attr('href', '#step6');			
		}

		return false
	})
})
