//var m = window.location.hash.substr(1,window.location.hash.leght).split('&');
// for( el in m){
//   console.info(m[el].split("="))
// }

﻿$(document).ready(function() {
	$('#new-filtr label input').change(function() {
		sect = $("#new-filtr").serializeArray();

		$.ajax({
			type: "POST",
			cache: false,
			url: "/include/tovar_new.php",
			data: $.param(sect),
			success: function(html){
				$(".catalog-section").html(html);
				window.location = "#"+$.param(sect);   

			}
		});
	});

	$('.f-item-inside label').each(function() {
		var input = $(this).find('input');
			if (input.attr("checked")) {
				$(this).addClass('checked');
			} else {

			}
	});
	
	$('.f-item-inside label').click(function() {
		var input = $(this).find('input');
			if (input.attr("checked")) {
				$(this).addClass('checked');
				input.attr("checked","checked");
			} else {
				$(this).removeClass('checked');
				input.removeAttr("checked","checked");
			}
	});


$('table :has(tr td span.grey_style)').addClass('grey_table');
$('tr:has(td span.grey_style)').addClass('grey');
$('tr:first').addClass('head');
$('tr:first td:first').css('border-top-left-radius', '5px 5px');
$('tr:first td:last').css('border-top-right-radius', '5px 5px');
$('tr.grey').css('border-left', '1px solid #e7e7e7');
$('tr.grey').css('border-right', '1px solid #e7e7e7');
$('tr.grey td').css('border-left', 'none');
$('tr.grey td:last').css('border-right', '1px solid #e7e7e7');
var head = $('tr:first td').attr('rowspan');
if (head) $('tr:lt('+head+')').addClass('head');
var bottom = $('.grey_table tr:last td').attr('colspan');
if (bottom) $('.grey_table tr:last').addClass('bottom');



	$('.b-item-val input').live('change', function(){ 
		var value = $(this).val();
		var price = $(this).closest('.b-item-inside').find('.item-price-left').html();
		
		$(this).closest('.b-item-inside').find('.b-item-total-price').html(price*value);
		

		colleft = 0;
		$('.b-item-val input').each(function() {
			colleft += parseInt($(this).val());
		});

		$('.tt-price').html((colleft*price*value).toFixed(2));
		
		wleft = 0;
		$('.item-weight-left').each(function() {
			var weight = $(this).text();
			var co = $(this).parent().find('.b-item-val input').val();
			wleft += weight*co;
		});
		$('.tt-w').html(wleft.toFixed(2));

		wleft2 = 0;
		$('.item-obj-left').each(function() {
			var obj = $(this).text();
			var co = $(this).parent().find('.b-item-val input').val();
			wleft2 += obj*co;
		});
		$('.tt-obj').html(wleft2.toFixed(2));
		
		
	}).change;

	if (!$.browser.webkit) {
		$('INPUT[placeholder], TEXTAREA[placeholder]').blur(function(){ 
			if ($(this).val()=='') {
				$(this).val($(this).attr('placeholder'));
				$(this).addClass('m-placeholder');
			}
		}).focus(function(){
			$(this).removeClass('m-placeholder');
			if ($(this).val()==$(this).attr('placeholder'))
				$(this).val('');
		}).each(function(){
			if ( ($(this).val()=='') || ($(this).val()==$(this).attr('placeholder')) ) {
				$(this).val( $(this).attr('placeholder') );
				$(this).addClass('m-placeholder');
			}
			var form = $(this).closest('FORM');
			if (form.length)
				form.submit(function(){
					if ($(this).val()==$(this).attr('placeholder'))
						$(this).val('');
				});
		});	
	}
	

	$("#scroller").jCarouselLite({
		visible: 4,
		start: 0,
		speed: 500,
		auto: 5000,
		btnNext: ".right-arrow",
		btnPrev: ".left-arrow"
	});
	
	$(".scroller").jCarouselLite({
		visible: 4,
		start: 0,
		speed: 500,
		btnNext: ".r-arrow",
		btnPrev: ".l-arrow"
	});
	
	$(".scroller2").jCarouselLite({
		visible: 4,
		start: 0,
		speed: 500,
		btnNext: ".r2-arrow",
		btnPrev: ".l2-arrow"
	});
	
	$(".scroller3").jCarouselLite({
		visible: 4,
		start: 0,
		speed: 500,
		btnNext: ".r3-arrow",
		btnPrev: ".l3-arrow"
	});
	
	$(".scroller4").jCarouselLite({
		visible: 2,
		start: 0,
		speed: 500,
		btnNext: ".r4-arrow",
		btnPrev: ".l4-arrow"
	});

        $("#scroller2").jCarouselLite({
		visible: 4,
		start: 0,
		speed: 500,
		auto: 5000,
		btnNext: ".right-arrow2",
		btnPrev: ".left-arrow2"
	});
	
	$(".big-scroll-ins").jCarouselLite({
		visible: 1,
		start: 0,
		speed: 500,
		btnNext: ".b-next",
		btnPrev: ".b-prev",
		btnGo:
			['.b-prev-min1', '.b-prev-min2', '.b-prev-min3', '.b-prev-min4', '.b-prev-min5',
			 '.b-prev-min6', '.b-prev-min7', '.b-prev-min8', '.b-prev-min9', '.b-prev-min10']
	});


	$(".table-basket tr, .table-basket1 tr").each(colorize);
	function colorize(index) {
		if(index % 2 == 0)
		$(this).toggleClass("zebra");
	}


$(".table-basket tbody tr, .table-basket1 tbody tr").live('mouseover mouseout', function(event) {
  if (event.type == 'mouseover') {
    $(this).addClass('td-hover');
  } else {
    $(this).removeClass('td-hover');
  }
});

	simple_tooltip(".toolt","tooltip");
	simple_tooltip1(".toolt-b","toolt1");	
	simple_tooltip2(".toolt-t","toolt2");
	
	simple_tooltip3(".toolt-big","block-showed");
	
	
	
	$('.filt1 label, .filt2 label').each(function() {
		var input = $(this).find('input');
			if (input.attr("checked")) {
				$(this).addClass('checked');
			} else {

			}
	});
	
	$('.filt1 label').click(function() {
		var input = $(this).find('input');
			if (input.attr("checked")) {
				$(this).addClass('checked');
				input.attr("checked","checked");
			} else {
				$(this).removeClass('checked');
				input.removeAttr("checked","checked");
			}
	});

	$('.filt2 label').click(function() {
		var input = $(this).find('input');
			if (input.attr("checked")) {
				$(this).addClass('checked');
				input.attr("checked","checked");
			} else {
				$(this).removeClass('checked');
				input.removeAttr("checked","checked");
			}
	});

	$('.menu-li .show-sub').click(function() {
		$(this).parent().parent().find('.sub-items').slideToggle();
	});
	
	$('.qustion').click(function() {
		var block = $(this).attr('id');
		var x = $(this).position().left;
		var y = $(this).position().top;
		if ( $("."+block).css('display') == "block" ) {
			$("."+block).hide();
		} else {

			$("."+block).css({'left': x-255, 'top': y+35 }).show();
		}
		
	 })
	
	$('.block-hidden .ico-close').click(function() {
		$(this).parent().hide();
	});
	
	
    $(".item-value input").live('keypress', function (e){ 
		if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)){
			$(this).parent().find('.mesage').html("Только цифры").show().fadeOut(2500); 
			return false;
		}    
	});


	$('.item-value input').each(function(){ 
		var value = $(this).val();
		var price = $(this).closest('tr').find('.item-price').html();
		
		$(this).closest('tr').find('.item-price-total').html((price*value).toFixed(2));

		coll = 0;
		$('.item-value input').each(function() {
			coll += parseInt($(this).val());
		});

		$('.total-c').html(coll);

		w = 0;
		$('.table-basket tbody tr').each(function() {
			var weight = $(this).closest('tr').find('.item-weight').html();
			weight = parseFloat(weight.replace(",", "."));
			var co = $(this).find('.td-col input').val();
			w += weight*co;
		});
		$('.total-weight').html(w.toFixed(2));

		w2 = 0;
		$('.table-basket tbody tr').each(function() {
			var volume = $(this).closest('tr').find('.item-obj').html();
			if (volume)
			{
				volume = parseFloat(volume.replace(",", "."));
			}
			var co = $(this).find('.td-col input').val();
			w2 += volume*co;
		});
		$('.total-obj').html(w2.toFixed(4));
		

		var sum = 0;
		$('.item-price-total').each(function() {
			sum += parseInt(this.innerHTML);
		});
		$('.total-pr').html(sum);
		
		var skidka = 0;
		$('.sk').each(function() {
			skidka += parseInt(this.innerHTML);
		});		
		$('.skidka').html(sum/100*skidka);
		
		$('.total-price span').html(sum-(sum/100*skidka));
	});

	$('.item-value input').live('change', function(){ 
		var value = $(this).val();
		var price = $(this).closest('tr').find('.item-price').html();
		
		$(this).closest('tr').find('.item-price-total').html(price*value);
		

		coll = 0;
		$('.item-value input').each(function() {
			coll += parseInt($(this).val());
		});

		$('.total-c').html(coll);
		
		w = 0;
		$('.table-basket tbody tr').each(function() {
			var weight = $(this).find('.item-weight').text();
			weight = parseFloat(weight.replace(",", "."));
			var co = $(this).find('.td-col input').val();
			w += weight*co;
		});
		$('.total-weight').html(w.toFixed(2));

		w2 = 0;
		$('.table-basket tbody tr').each(function() {
			var volume = $(this).closest('tr').find('.item-obj').html();
			if (volume)
			{	
				volume = parseFloat(volume.replace(",", "."));
			}
			var co = $(this).find('.td-col input').val();
			w2 += volume*co;
		});
		$('.total-obj').html(w2.toFixed(4));
		
		var sum = 0;
		$('.item-price-total').each(function() {
			sum += parseInt(this.innerHTML);
		});
		$('.total-pr').html(sum);
		
		var skidka = 0;
		$('.sk').each(function() {
			skidka += parseInt(this.innerHTML);
		});		
		$('.skidka').html(sum/100*skidka);
		
		$('.total-price span').html(sum-(sum/100*skidka));
	}).change;
	

	$('#basket-input').each(function(){ 
		var value = $(this).val();
		var price = $(this).closest('table').find('.td-right2 span').html();

		$('.total-price2 strong').html(value*price);
	});

	$('#basket-input').live('change', function(){ 
		var value = $(this).val();
		var price = $(this).closest('table').find('.td-right2 span').html();

		$('.total-price2 strong').html(value*price);
	}).change;


	$(".tab-select a.tt").each(function(){
		var tab = $(this).parent();
		if( tab.hasClass('sel') )
			$("#"+tab.attr('id')+"-content").show();
	});
    
    
	$(".tab-select a.tt").click(function(){
	//	var tabc = $(this).parent();
	//	tabc.parent().find("li").removeClass('sel');
	//	tabc.addClass('sel');
	//	var tab = $("#"+tabc.attr('id')+"-content");
	//	tab.parent().find('div.tabc').hide();
	//	tab.show();
	//	return false;
	});
	

});



function simple_tooltip(target_items, name){
	$(target_items).each(function(i){
	$("body").append("<div class='"+name+"' id='"+name+i+"'><span></span><p>"+$(this).attr('title')+"</p></div>");
	var my_tooltip = $("#"+name+i);
		$(this).removeAttr("title").mouseover(function(){
			my_tooltip.css({display:"none"}).fadeIn(100);
		}).mousemove(function(kmouse){
				my_tooltip.css({left:kmouse.pageX+15, top:kmouse.pageY+15});
		}).mouseout(function(){
			my_tooltip.fadeOut(100);
		});
	});	
}

function simple_tooltip1(target_items, name){
	$(target_items).each(function(i){
	$("body").append("<div class='"+name+"' id='"+name+i+"'><div><span></span><p>"+$(this).attr('title')+"</p></div></div>");
	var my_tooltip = $("#"+name+i);

	$(this).removeAttr("title").mouseover(function(){
			my_tooltip.css({display:"none"}).fadeIn(100);
		}).mousemove(function(kmouse){
			my_tooltip.css({left:kmouse.pageX-92, top:kmouse.pageY+35});
		}).mouseout(function(){
			my_tooltip.fadeOut(100);
		});
	});
}

function simple_tooltip2(target_items, name){
	$(target_items).each(function(i){
	$("body").append("<div class='"+name+"' id='"+name+i+"'><div><span></span><p>"+$(this).attr('title')+"</p></div></div>");
	var my_tooltip = $("#"+name+i);
		$(this).removeAttr("title").mouseover(function(){
			my_tooltip.css({display:"none"}).fadeIn(100);
		}).mousemove(function(kmouse){
		my_tooltip.css({left:kmouse.pageX-92, top:kmouse.pageY-25});
		}).mouseout(function(){
			my_tooltip.fadeOut(100);
		});
	});
}


function simple_tooltip3(target_items, name){
	$(target_items).each(function(i){
	$("body").append("<div class='"+name+"' id='"+name+i+"'><div><span></span>" + "</div></div>");
	$("."+$(this).attr('rel') ).clone().appendTo( $("#"+name+i));
	var my_tooltip = $("#"+name+i);
		$(this).removeAttr("title").mouseover(function(){
			my_tooltip.css({display:"none"}).show();
		}).mousemove(function(kmouse){
			if ( kmouse.pageX >= 150 ) {
				my_tooltip.find('div span').css({'left':'100px'});
				my_tooltip.css({left:kmouse.pageX-150, top:kmouse.pageY+30});
			} else {
				my_tooltip.find('div span').css({'left':'30px'});
				my_tooltip.css({left:kmouse.pageX-80, top:kmouse.pageY+30});
			}
		}).mouseout(function(){
			my_tooltip.hide();
		});
	});
}
