function openVerMenu(mid){
	if($("#vsubmenu_"+mid).css("display")=="none"){
		$("#vsubmenu_"+mid).show(200);
		saveOpenedMenu(mid,1);
	}else{
		$("#vsubmenu_"+mid).hide(200);
		saveOpenedMenu(mid,0);
	}
}
function saveOpenedMenu(mid,stat){
	 $.ajax({
	   type: "get",
	   url: saveMenuScript,
	   data: "mid="+mid+"&stat="+stat,
	   success: function(msg){
		 void(0);
	   }
	 });
}

window.onload=function() {
	if($('#motioncontainer').css("display")) fillup();
	if($('#basket').css('display')) countBasketPrice();
	/* -------- FOR INPUT TITLE TO VALUE ------------------- */
	$('input[type=text][title!=""]').each(function() {
	if ($.trim($(this).val()) == '') $(this).val($(this).attr('title'));
	if ($(this).val() == $(this).attr('title')) $(this).addClass('exampleText');
	}).focus(switchText).blur(switchText);
	/* -------- END FOR INPUT TITLE TO VALUE ------------------- */
}

function changeBasketTabs(tab){
	if($('.basketTab'+tab).attr('id') != 'activeTab' && basketError==0){
		//countDelivery(0);
		$('#deliverySelect').find('option:first').attr('selected', 'selected').parent('select');
	}
	
	$('#baskTab1').attr('checked',false);
	$('#baskTab2').attr('checked',false);
	$('#bTabContent1').hide();
	$('#bTabContent2').hide();
	
	$('#baskTab'+tab).attr('checked',true);
	$('#activeTab').removeAttr('id');
	
	$('.basketTab'+tab).attr('id','activeTab');
	$('#bTabContent'+tab).show();
	return true;
}

function countDelivery(select){
	if(select > 0){
		$.ajax({
			type: "POST",
			url: "?getDelivery",
			data: {'action':'getDelivery','id':select, 'mass':parseFloat($('#kop_svars').html())},
			success: function(response){
				response=(response)?response:'0.00';
				$('#deliveryPrice').html(response);
				$('#deliveryPrice_input').attr('value',response);
				$('#kop_price3').html( roundNumber((parseFloat($('#kop_price').html())+parseFloat(response))-parseFloat($('#kop_price_offers').html()),2) );
			}
		});
	}else{
		$('#deliveryPrice').html('0.00');
		$('#deliveryPrice_input').attr('value','0.00');
		$('#kop_price3').html( roundNumber(parseFloat($('#kop_price').html())-parseFloat($('#kop_price_offers').html()),2) );
	}
	return true;
}


function changeCountBasketEl(value, id, pid){
	count_var=parseFloat($('#b_count_'+id).attr('value'));
	if(value == '+'){
		count_var=count_var+1;
	}else{
		if(count_var > 1){
			count_var=count_var-1;
		}
	}
	 $.ajax({
			type: "POST",
			url: "?countVal",
			data: {'count_val':count_var,'count_id':id,'p_id':pid}
		});
	$('#b_count_'+id).attr('value',count_var);
	$('#b_count2_'+id).html(count_var);
	countBasketPrice(id);
}

function countBasketPrice(){
	var price = 0;
	var firstpay = 0;
	for(i=0;i<$('div.bPriceOrig').length;i++){
		tmp=$($('div.bPriceOrig')[i]).attr('id').split('_');
		var count=$('#b_count_'+tmp[3]).attr('value');
		var orig_price=$($('div.bPriceOrig')[i]).html();
		var orig_firstpay=$('#b_firstpay_orig_'+tmp[3]).html();		
		$('#b_price_'+tmp[3]).html(roundNumber((orig_price*count),2));
		$('#b_price_'+tmp[3]+'_2').html(roundNumber((orig_price*count),2));
		price+=orig_price*count;
		firstpay+=orig_firstpay*count;
	}
	$('#kop_price').html(roundNumber(price,2));
	$('#kop_price3').html(roundNumber(price,2));
	if(firstpay > 0){
		$('#kop_firstpay').html(roundNumber(firstpay,2));
		$('#kop_firstpay_value').show();
		$('#kop_firstpay_text').show();
		$('#kop_firstpay_radio').show();
	}else{
		$('#kop_firstpay_value').hide();
		$('#kop_firstpay_text').hide();
		$('#kop_firstpay_radio').hide();
	}
	return true;
}

function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  return newnumber; // Output the result to the form field (change for your purposes)
}

function plus(id) {
	if($('#countval_' + id).attr('value')){
			val=parseFloat($('#countval_' + id).attr('value'));
			$('#countval_' + id).attr('value',val+1);
	}else{
		$('#countval_' + id).attr('value',0);
	}
}
function minus(id) {
	if($('#countval_' + id).attr('value')){
		if($('#countval_' + id).attr('value') > 0){
			val=parseFloat($('#countval_' + id).attr('value'));
			$('#countval_' + id).attr('value',val-1);
		}
	}else{
		$('#countval_' + id).attr('value',0);
	}
}

function ajax_sendnews(link, container, value) {
	if(value != ''){
		$.ajax({
			url : link,
			data : { email:value,action:'asdsa' },
			type : 'post',
			success : function(msg){
				$('#'+container).html(msg).fadeIn(100);
				$('#'+container).fadeOut(6000);
			}
		});
	}
}
function switchText(){
	if ($(this).val() == $(this).attr('title'))
		$(this).val('').removeClass('exampleText');
	else if ($.trim($(this).val()) == '')
		$(this).addClass('exampleText').val($(this).attr('title'));
}

function showRightSearchSubSelect(sel,id){
	for(i=0;i<$('select.sideSearchSelId').length;i++){
		if($($('select.sideSearchSelId')[i]).css("display") != 'none'){
			$($('select.sideSearchSelId')[i]).hide();
		}
		$($('select.sideSearchSelId')[i]).find('option:first').attr('selected', 'selected').parent('select');
	}
	if($(sel).attr('value') > 0){
		$('#'+id+$(sel).attr('value')).show();
	}
	return true;
}
function showRightSearchSubSelect2(sel,id){
	for(i=0;i<$('select.sideSearchSelId2').length;i++){
		if($($('select.sideSearchSelId2')[i]).css("display") != 'none'){
			$($('select.sideSearchSelId2')[i]).hide();
		}
		$($('select.sideSearchSelId2')[i]).find('option:first').attr('selected', 'selected').parent('select');
	}
	if($(sel).attr('value') > 0){
		$('#'+id+$(sel).attr('value')).show();
	}
	return true;
}
function showRightSearchSubmit(){
	for(i=0;i < $('select.sideSearchSelId').length;i++){		
		if($($('select.sideSearchSelId')[i]).css('display') == 'none'){
			$($('select.sideSearchSelId')[i]).attr('disabled',true);
		}
	}
	$('#searchSideSmall').submit();
}
function showRightSearchSubmit2(){
	for(i=0;i < $('select.sideSearchSelId2').length;i++){
		if($($('select.sideSearchSelId2')[i]).css('display') == 'none'){
			$($('select.sideSearchSelId2')[i]).attr('disabled',true);
		}
	}
	$('#searchSideSmall2').submit();
}
