//отправляет добавление в корзину
function addToBusket(obj) {
    
    count = obj.parent().find('input').val();
    if(count == '')
    {
 		alert('Укажите количество');
        return false;
    }
    
    id = doc_id;
    var tr = obj.parent().parent().parent();
    mod = tr.find('td:first').html() + " (";
    
    var tr1 = tr.parent().parent().find('tr:first');
    var thc = tr1.find('th:eq(1)').html();
    var tdc = tr1.find('td:eq(1)').html();
    
    var regexp = /<[^>]+>/g;   
    
    if(thc)
    {    
    	thc = thc.replace(regexp,'');
    	mod += thc;
    }	
    if(tdc) 
    { 
    	tdc = tdc.replace(regexp,''); 	
    	mod += tdc;
    }
    
    mod += ': ' + tr.find('td:eq(1)').html() + ')';
    
    idmod = obj.attr('id');
             
    $('#gotobusketa').trigger('click');
      
    jQuery.post("/basket.html?mode=addToBasket", {
        'id': id,
        'count': count,
        'mod': mod,
        'key': id + idmod
    }, function()
    {
        $("#baskettop").fadeOut("slow", function(){
            $("#baskettop").load("/baskettop.html?mode=showTop", {}, function(){
                $("#baskettop").fadeIn("slow");
            });
        });
    });
    
    return false;
}

$(window).load(
function()
    { 
    	cards = $('.catalog>div');
		for(i = 0; i < cards.length; i += 2)
		{
			h1 = cards.eq(i).height();
			h2 = cards.eq(i + 1).height();
			h = Math.max(h1,h2);
			cards.eq(i).height(h);
			cards.eq(i + 1).height(h);
		}
	}
);

$(document).ready(
		function()
		{
			$("input.countCounter").change(function(){
		        id = $(this).attr('id');
		        
		        count = parseInt($(this).val());
		        jQuery.post("/basket.html?mode=newCountBasket", {
		            'key': id,
		            'count': count
		        }, function()
		        {
		            $("#baskettop").fadeOut("slow", function(){
		                $("#baskettop").load("baskettop.html?mode=showTop", {}, function(){
		                    $("#baskettop").fadeIn("slow");
		                });
		            });
		        });
		    });
        
		    $("img.cancelbuy").click(function(){
		        id = $(this).attr('id');
		        cost = parseInt($("span#cost" + id).html());
		        costTotal = parseInt($("span#costTotal").html());
		        countPro = parseInt($("#countPro").html());
		        $("tr#tr" + id).empty();
		        $("span#costTotal").html(costTotal - cost);
		        $("#countPro").html(countPro - 1);
		        jQuery.post("/basket.html?mode=dellFromBasket", {
		            'key': id
		        }, function()
		        {
		            $("#baskettop").fadeOut("slow", function(){
		                $("#baskettop").load("baskettop.html?mode=showTop", {}, function(){
		                    $("#baskettop").fadeIn("slow");
		                });
		            });
		        });
		    });
		    $("form#buybasket").submit(function(){           
		        if(('#basket_table td').size() < 1)
		        {
		            alert('корзина пуста');
		            return false;
		        }
		        return true;
		    }
		    );			
			
			//fancybox
		    $('a#gotobusketa').fancybox({   
		    'titleShow'     : false ,
					'transitionIn'		: 'none',
					'transitionOut'		: 'none'

			});
			
			$("div#description table").addClass('table').attr('border','0').attr('cellspacing','0');;
			
			var regexp = /basket/g;
		    var i = 0;
		    $('.content div#description table tr td').each(
		    function()
		    {
		    	if(regexp.test( $(this).html() ))
		    	{
		    		$(this).html('<form action="#" method="post" class="add"><input type="text" value="" /> шт.<br><input type="submit" value="" class="button" onclick="addToBusket($(this)); return false;" id="' + i + '"/></form>');   
		    		//$(this).html('<input type="text"/><a href="#" onclick="addToBusket($(this)); return false;" id="' + i + '">заказать</a>');        		
		    		$(this).addClass('tr_busket');
		    		i++;
		    	}	
		    }
		    );
			
			$("a[rel=image_group]").fancybox({
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'elastic',
				'titlePosition' 	: 'over',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over">' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
				}
			});
			
			$('#menu a.current').corner("7px");
			$('#main_products>li>div.slider').hide();
			//$('#main_products li').click(function(){$(this).find('div.arrow').trigger('click')});
			$('#main_products>li>a').attr('href','javascript:void(0);');
			$('#main_products>li').click(function(){
			//	curent_li=$(this).parent();
				curent_li=$(this);
				$(curent_li).attr('class','current');
				$('#main_products>li').not(curent_li).attr('class','').children('div.slider').hide();
				$('#main_products>li').children('div.arrow').children('div').attr('class','arrow_down');
				if(curent_li.children('div.slider').is(':hidden')){
							curent_li.children('div.slider').show();
							curent_li.children('div.arrow').children('div').attr('class','arrow_up');
						}
						else {
							curent_li.children('div.slider').hide()/*.children('ul').css("margin-left", "0")*/;
						}
				});
				
				
			$('.slider').each(function() {
				var obj = $(this);
				var s = $("li", obj).length;
				var w = $("li", obj).width();
				var h = $("li", obj).height();
				obj.width(980); //width of div with class slider 
				obj.height(h); 
				obj.css("overflow","hidden");
				
				var margin_left = $(document).width();
				margin_left = Math.ceil((margin_left - obj.width())/2);
				
				$("ul", obj).css('width',s*w);
				var padd = obj.width()/s/0.8; 
				var Xmax = obj.width() - padd*2;			
				var dx = (s*w-obj.width())/(Xmax);
				if(s*w > obj.width())
				$(obj).mouseover(function(){
						$(obj).mousemove(function(kmouse){
							var x = kmouse.pageX - margin_left-padd;
							if(x>0 && x<Xmax)
							$("ul",obj).css(
								{ left: -1*dx*x});
							else if(x<0)
							$("ul",obj).css(
								{ left: 0});
							else if(x>Xmax)
							$("ul",obj).css(
								{ left: -1*dx*Xmax});
						});
					}).mouseout(function(){
						$(obj).mousemove(function(kmouse){});
					});
			});
			$('#main_products div.arrow:first').click();
			$('.catalog>div:even').css('border-right','1px solid #ebebeb');
			$('#main_catalog tr td:even').css('border-right','1px solid #ebebeb');
			$('table.table tr:first th').not(':first').css('border-left','1px solid #ccc');
		}
	);	

