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);
	   }
	 });
}
function disableEnterKey(e)
{
     var key;
     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox
     if(key == 13){
     		$('#sendnews a').trigger('click');
        return false;
     }else
          return true;
}
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});
$(document).ready(function() {
   if($.getUrlVar('catID') == 165){
		var ids = $.getUrlVar('param[]');
		$('#ids_'+ids).removeClass('left_menu_subgroup_item');
		$('#ids_'+ids).addClass('left_menu_subgroup_item_act');
   }
});


window.onload=function() {
	$("#slidePromo").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true);
	$("#prodImgList").tabs();
	$('.mainPromoTile:nth-child(4n)').css('margin-right', '0');
	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() {
		$("a.fancy").fancybox({
			'speedIn'		:	400, 
			'speedOut'		:	200, 
			'overlayShow'	:	true,
			'imageScale': true, 
	        'overlayColor': '#000', 
	        'overlayOpacity': 0.8
		});
	});
	$("#f_contact").validate({
	//set the rules for the field names
		rules: {
			name: {
				required: true,
				minlength: 2
				},
			email: {
				required: true,
				email: true
			},
			phone: {
				required: true
			},
			description: {
				required: true,
				minlength: 2
			}
		},
		//set messages to appear inline
		messages: {
			name: validName,
			email: validEmail,
			phone: validRequired,
			description: validRequired
			}
		});
}

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, max){
	count_var=parseFloat($('#b_count_'+id).attr('value'));
	if(value == '+'){
		if (count_var < max)
		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;
	var shipcost = 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 pricedel = count * $('#delprice_'+tmp[3]).html();
		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));
		$('#itemUnit_'+tmp[3]).html(count);
		if (pricedel > shipcost) shipcost=pricedel;
		price+=orig_price*count;
		firstpay+=orig_firstpay*count;
	}
	$('.deliveryCosts').html(shipcost);
	shipcost=parseFloat(shipcost);
	price+=shipcost;
	$('#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, max) {
	if($('#countval_' + id).attr('value')){
		if($('#countval_' + id).attr('value') < max){
			val=parseFloat($('#countval_' + id).attr('value'));
			$('#countval_' + id).attr('value',val+1);
			}
		else $('#countval_' + id).attr('value',max);
	}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();
}
