// JavaScript Document


$(document).ready(
	function() {
		
		$(".enter .head strong").toggle( function() { 
			$(".enter .body").animate({ opacity: "show" }, "normal");
		},function(){
			$(".enter .body").animate({ opacity: "hide" }, "normal");		
			$(this).removeClass("click");
		} );
		
		$(".enter .close").click( function() { 
			$(".enter .body").animate({ opacity: "hide" }, "normal");
		} );
		
		$(".tabs > ul").tabs({
			fxFade: true,
			fxSpeed: 'normal'
		});
		
		if ( $('#sec').length ) {
			$('#carousel').jcarousel({
				scroll: 4
			});
		}
		else {
			$('#carousel').jcarousel({
				scroll: 6
			});		
		}
		
		$(".navigation ul a").hover(function(){
			$(this).next("span").addClass("hover");
			},function(){
			$(this).next("span").removeClass("hover");
		});
		
		$(".ui-tabs-panel a").hover(function(){
			$(this).next("span").addClass("hover");
			},function(){
			$(this).next("span").removeClass("hover");
		});
		
		$(".type .control").hover(function(){
			$(this).children("span").addClass("hover");
			},function(){
			$(this).children("span").removeClass("hover");
		});
		
		$(".type .control").toggle(function(){
			$(".type .body").animate({ opacity: "show" }, "fast");								
			$(this).addClass("click");
			},function(){
			$(".type .body").animate({ opacity: "hide" }, "fast");		
			$(this).removeClass("click");
		});

		$(".number .dec").click(function(){
			if ( parseInt( $(this).next("span").text() ) > 1 ) {
				$(this).next("span").text( $(this).next("span").text() - 1 );
				$(this).next().next("input").val( $(this).next("span").text() );
			}
		});

		$(".number .inc").click(function(){
			$(this).prev().prev("span").text( parseInt( $(this).prev().prev("span").text() ) + 1 );
			$(this).prev("input").val( $(this).prev().prev("span").text() );
		});

		if ( $('#cont form').length ){
			if ( $.browser.msie && $.browser.version < 7 ) {
				
			}
			else {
				$('#cont form').jqTransform();
			}
		}

		$("#cont input[name*='weight_']").change(function(){
			var weigth = '';
			var id = $(this).attr("prod_id");
			
			var weight_kg_input = $("input[name=weight_kg_d_"+id+"]");
			var weight_kg = $(weight_kg_input).val();
			var weight_g_input = $("input[name=weight_g_d_"+id+"]");
			var weight_g = $(weight_g_input).val();

			if ( weight_kg == '' ) { weight_kg = 0 }
			if ( weight_g == '' ) { weight_g = 500 }

			if ( weight_g < 10 ) {
				weight_g = weight_g * 100;
			}
			else if ( weight_g < 100 ) {
				weight_g = weight_g * 10;
			}
			else {
				weight_g = weight_g;
			}

			$(weight_kg_input).val( weight_kg );
			$(weight_g_input).val( weight_g );

			weigth = weight_kg + '.' + weight_g;

			$("input[id=d_"+id+"]").val( weigth );
		});

		$("#cont input[name*='weight_']").keypress(function (e) {
		  if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
			return false;
		  }
		});

		$("#cont input[name='region']").click(function (e) {
			var orderSum = $( "#cont #orderSum" ).text();
			var deliveryBig = '';
			if ( orderSum > 3000 ) {
				var deliveryBig = '3000';
			}
			var deliverySum = $( "#cont #region_" + $(this).attr('value') + deliveryBig ).text();
			var orderSumWithDelivery = parseFloat(orderSum) + parseFloat(deliverySum);
			$( "#cont #total_sum span" ).text( orderSumWithDelivery );
		});

		if ( checkedRegion = $("#cont input[name='region']:checked") ) {
			var orderSum = $( "#cont #orderSum" ).text();
			var deliveryBig = '';
			if ( orderSum > 3000 ) {
				var deliveryBig = '3000';
			}
			var deliverySum = $( "#cont #region_" + checkedRegion.attr('value') + deliveryBig ).text();
			var orderSumWithDelivery = parseFloat(orderSum) + parseFloat(deliverySum);
			$( "#cont #total_sum span" ).text( orderSumWithDelivery );
		}

		$("a.d_reg").click(function(){
			$("a.d_reg > div").hide();
			$("div", this).show();
			$("div", this).click( function(){
				$(this).unbind('click');
				$(this).hide();
				return false;
			});
			return false;
		});

		if ( $("#datepicker").length ) {
			$("#datepicker").change( function () {
				var html = $.ajax( {
					type: "POST",
					url: _root + "include/getdeliverytime.php",
					async: false,
					data: "date="+$(this).val()
				}).responseText;
				$("#t").html( html );
			});
		}
		
		/*
		$("#datepicker").change(function () {
			var d = new Date();
			if ( $(this).val() != $('#today').val() ) {
				$("#t").hide();
				$("#t_full").show();
			}
			else{
				$("#t_full").hide();
				$("#t").show();
			}
		});
		*/


	});

$(".navigation").ready(function() {

	$(".navigation li.sel").parents('li').addClass("sel");
	$(".navigation li ul").hide();
	$(".navigation li ul li ul li").hide();
	$(".navigation .b").addClass('no');
	$(".navigation .sel .b").removeClass('no');
	$(".navigation li.sel ul").show();
	$(".navigation li.sel ul li.sel ul li").show();

	$(".navigation li>div>div>a").click(function() {
		$(".navigation li ul").hide();
		$(".navigation .b").addClass('no');
		$(".navigation>li").removeClass('sel');
		$(".navigation .sel .b").removeClass('no');

		$(this).parents('li').addClass('sel');
		$(".navigation li.sel ul").show();
		$(this).parents('div.b').removeClass('no');

		return false;
	});	

});
