document.cookie = "jstest=1; path=/;";
(function($){$.fn.extend({limit:function(limit,element){var interval,f;var self=$(this);$(this).focus(function(){interval=window.setInterval(substring,100)});$(this).blur(function(){clearInterval(interval);substring()});substringFunction="function substring(){ var val = $(self).val();var length = val.length;if(length > limit){$(self).val($(self).val().substring(0,limit));}";if(typeof element!='undefined')substringFunction+="if($(element).html() != limit-length){$(element).html((limit-length<=0)?'0':limit-length);}";substringFunction+="}";eval(substringFunction);substring()}})})(jQuery);

jQuery.extend({console:function(data){if(window.console&&window.console.log)console.log(data);}});
jQuery.fn.tabs=function(){
	var $this=$(this),$top=$(".productMenu",$this),$cont=$(".contBox2 ul",$this);
	$("li",$top).click(function(){
		$o=$(this);
		if(!$o.hasClass("active")){
			$("li",$top).removeClass("active");
			$o.addClass("active");
			i=$("li",$top).index($o);
			$("li",$cont).removeClass("active");
			$("li.item:eq("+i+")",$cont).addClass("active");
			$.console(i);
		}
		return false;
	});
}


$(document).ready(function(){
	
	if ( $('#your_msg').length > 0 ) {       
		$('#your_msg').limit('500');
	}
	
	var defaultShipping = parseFloat($('.fieldBox1 span', $('#shipping input:checked').parents('li:first')).text());
	//alert(defaultShipping);
	
	
	
	$("#tabs").tabs();
	
	
	$("#currencySel").change(function(){
		$("#currencySel").submit();
	});
	// CHANGE LANG
	$("#selectLang").change(function(){
		$("#selectLang").submit();
	});
	
	//auto height
	$('.productsList .line').each(function(){
		maxH = 0;
		$('div.e .p', this).each(function(){
			if ( maxH < $(this).height() ) {
				maxH = $(this).height();
			}
		});
		$('div.e a .p', this).css('height', maxH);
		maxH = 0;
		$('div.e .h1', this).each(function(){
			if ( maxH < $(this).height() ) {
				maxH = $(this).height();
			}
		});
		$('div.e a .h1', this).css('height', maxH);
	});
	
	
	// shipping
	$('#shipping li').click(function(){
		$('input', this).attr('checked', 'checked');
		valForAjax = $("input", this).val();
		serVals = $("#resultArrC").val();
		shipPrice = $('.fieldBox1 span', this).text();
		shipPrice = parseFloat(shipPrice);
		//alert(shipPrice);
		resultSum = parseFloat($("#totalPrice").text()) - (defaultShipping) + (shipPrice);
		//alert(shipPrice);
		//alert(resultSum);
		resultSum = resultSum.toFixed(2);
		
		link = ajaxPath + 'session_shipping_type_editor.php?SID=' + session_id;
		//alert(link);
		
		pointer = this;
		
		$.post(
			link, {"type": valForAjax, "ser": serVals, "shipPrice": shipPrice},
			function(data) {
				//alert(data);
				
				//alert(data);
				inArr = JSON.parse(data);
				ser = inArr.ser;
				type = inArr.type;
				$("#resultArrC").val(ser);
				$('input', pointer).attr('checked', 'checked');
				$('#totalPrice').html(resultSum);
				//$('#totalPrice').html
				defaultShipping = shipPrice;
				//alert(defaultShipping);
				
			}
		);
	});
	
	function showPopup(){
		onShadow();
		t = parseInt($(document).scrollTop()) + 25;
		$('#popup').css('top', t);
		$('#popup').fadeIn('fast');
	}
	
	function hidePopup() {
		hideShadow();
		$('#popup').fadeOut('fast');
	}
	
	function onShadow(){
		h = $(document).height();
		$('#shadow').css('height', h);
		$('#shadow').fadeIn('fast');
	}
	
	function hideShadow() {
		$('#shadow').fadeOut('fast');
	}
	
	// bonus
	$('#changeBonus').click(function(){
		showPopup();
		return false;
	});
	
	$('#popup li').hover(function(){
		if ( !$(this).parent('ul').hasClass('grey') ) {
			$(this).addClass('hover');
		}
	}, function(){
		$(this).removeClass('hover');
	});
	
	$('#popup li').click(function(){
		if ( !$(this).parent('ul').hasClass('grey') ) {
			$('input', this).attr('checked', true);
			// ajax
			link = ajaxPath + 'session_digit_editor.php?SID=' + session_id;
			serVals = $("#resultArrC").val();
			fld = $("input", this).val();
			activeBonus = $('label strong', $(this)).html();
			$.post(
				link, {
					'bonus_choise': fld,
					"ser": serVals
				},
				function(data) {
					inArr = JSON.parse(data);
					ser = inArr.ser;
					$("#resultArrC").val(ser);
					
					$('#activeBonus').html(activeBonus);
				}
			);
			
			hidePopup();
		}
		
	});
	
	
	
	
	
	
	
	// AJAX
	$("body").ajaxStart(function(){
		onShadow();
		windowH = parseInt($(window).height());
		topH = parseInt($(window).scrollTop()) + parseInt(windowH/2);
		
		$('#ajaxLoader').css('top', topH);
		$('#ajaxLoader').css('display', 'block');
	});
	$("body").ajaxComplete(function(){
		hideShadow();
		$('#ajaxLoader').css('display', 'none');
	});
	$("body").ajaxError(function(){
		alert('ERROR OF AJAX, CALL TO DEVELOPERS!');
	});
	
	/*
	$('.faqList .answer:first').css('display', 'block');
	$('.faqList .e').click(function(){
		if ( $('.answer', this).css('display') != 'block' ) {
		$('.faqList .answer').slideUp('fast');
			$('.answer', this).slideDown('fast');
		}
		
	});
	*/
	
	
	$('#shadow, #close_popup').click(function(){
		hidePopup();
		$("#searchResults").fadeOut('fast');
	});
	
	$('.discountBox .text .learn_more .reactor').hover(function(){
		$('.discountBox .text .learn_more .hide').css('display', 'block');
	}, function(){
		$('.discountBox .text .learn_more .hide').fadeOut('fast');
	});
	
});
