$(window).load(function() {

	$(".row").each(function(){
		columns = $(this).children((($.browser.msie && $.browser.version.substr(0,1)<"8")?".cell, ":'')+".item");
		var tallestcolumn = 0;
		columns.each(function(){
			var currentHeight = $(this).height();
			if (currentHeight > tallestcolumn) tallestcolumn = currentHeight;
		});
		columns.height(tallestcolumn);  
	});
});



$(document).ready(function() {

//	$(document).pngFix({ blankgif: '/images/blank.gif' }); 

	$(".box").colorbox({close: 'x'});

	$(".zebra li:even").addClass('even');
	
	$("a[href^=http://]").attr('target','_blank');

	$("ul").filter(".navigation, .menu, .path").each(function(){
	    $(this).children("li:first").css('border-left', 'none').css('padding-left','0');
	    $(this).children("li:last").css('border-right', 'none').css('padding-right','0');
	    
	    $(this).children("li.after:last").css('content', ' ');
	});


    $('input[title]').bind({
        focus: function(){
            if ($(this).attr('title') == $(this).attr('value')) $(this).attr('value', '');
        },
        blur: function(){
            if ($(this).attr('value') == '') $(this).attr('value', $(this).attr('title'));
        }
    });


    $("input.toggle:checkbox:enabled").click(function(){
        $("input.checkbox:checkbox:enabled", $(this).parents("form")).each(function(){
            $(this).attr('checked', !$(this).attr('checked'));
        });
    });


    $(".tabs > .tab").click(function(){		
        $(this).parent().children().removeClass('current');
        $(this).addClass('current');
        $("div[id^='tab_']").removeClass('tab_current');
        $("#tab_"+$(this).attr('id')).addClass('tab_current');
    });



	$(".checkbox.trigger").change(function(){
		$('.'+$(this).attr("id")).toggle();
		$(this).val($(this).attr("checked")?'1':'0');
	}).filter("[type=checkbox], [type=radio]").each(function(){
                if ($(this).attr("value")=='0') $(this).val('');
		if ($(this).attr("value")) $(this).attr('checked', 'true');
	}).filter(":checked").each(function(){	
		$("."+$(this).attr("id")).show();
	});


	$(".row").each(function(){
		$(this).children(".cell:last").css('border-right', 'none');
	}).each(function(){
		$(this).children(".break").prev().css('border-right', 'none');
	});
 

	var discount = $("#discount").text();
	if (!(discount==null) && !(discount=='') && !(discount==0)) {
		$(".price").each(function(){
			price = $(this).find("span[id^='price']").text();
			$(this).find("span[id^='discount']").html(price-(price/100*discount));
		}).find(".price_discount").show();	
	}

    
    $("select.countries_select").each(function(){
    	var countries_obj = this;
    	var countries_current = $(countries_obj).val()==''?'':$("ul.countries span.name:contains('"+$(countries_obj).val()+"')").text();
    	$(countries_obj).empty();
    	$('<option value="">&nbsp;выберите&nbsp;</option>').appendTo(countries_obj);
    	$("ul.countries span.name").each(function(){
            $('<option'+(countries_current==$(this).text()?' selected="true"':'')+'>'+$(this).text()+'</option>').appendTo(countries_obj);
	});
    }).change(function(){
    	var cities_obj = $("form #parent_"+$(this).attr('id'));
    	$(cities_obj).empty();
    	$('<option value="">&nbsp;выберите&nbsp;</option>').appendTo(cities_obj);
    	$("ul.countries span.name:contains('"+$(this).val()+"')").parent().find("ul.cities li").each(function(){
    	    $('<option>'+$(this).text()+'</option>').appendTo(cities_obj);
        });
    }).each(function(){
    	var cities_obj = $("form #parent_"+$(this).attr('id'));
    	var cities_current = $(cities_obj).val()==''?'':$(cities_obj).val();
    	$(cities_obj).empty();
    	if (cities_current=='') return;
    	$('<option value="">&nbsp;выберите&nbsp;</option>').appendTo(cities_obj);
    	$("ul.countries span.name:contains('"+$(this).val()+"')").parent().find("ul.cities li").each(function(){
    	    $('<option'+(cities_current==$(this).text()?' selected="true"':'')+'>'+$(this).text()+'</option>').appendTo(cities_obj);
        });
    });


	$("div[id^='payment_']").each(function(){
		$('<option>'+$(this).attr('title')+'</options>').appendTo("#payment_");
	});
	$("div[id^='delivery_']").each(function(){
		$('<option>'+$(this).attr('title')+'</options>').appendTo("#delivery_");
	});


	$("form .payment_select, form .delivery_select").each(function(){
	    if (!$(this).val()) return;
		$("#"+$("[title='"+$(this).val()+"']").attr('id')).clone(true).children().appendTo("#"+$(this).attr('id')+"content");
	}).change(function(){
		$("#"+$(this).attr('id')+"content").empty();
		$("#"+$("[title='"+$(this).val()+"']").attr('id')).clone(true).children().appendTo("#"+$(this).attr('id')+"content");
	});


    $(".brands_select").each(function(){
    	var brands_obj = this;
    	var brands_current = $(brands_obj).val()==''?'':$(brands_obj).val();
    	if (brands_current=='') brands_current = $.url.param("filter[2]");
	    $("#brands li a").each(function(){
    	    $('<option'+(brands_current==$(this).text()?' selected="true"':'')+'>'+$(this).text()+'</option>').appendTo($(brands_obj));
	    });
	}).change(function(){
    	var brands_current = $(this).val()==''?'':$(this).val();
    	window.location = $.url.attr('path')+(brands_current==''?'':'?filter[2]='+brands_current);
    });


});






function addBasket(item, amount, price, netto)
{
    if (item == null || item == '') return false;
    if (amount == null || amount == '') amount = 1;
    if (price == null || price == '') price = 0;
    if (netto == null || netto == '') netto = 0;

    $("#netto").text(Number($("#netto").text())+Number(netto)*Number(amount));
    $("#amount").text(Number($("#amount").text())+Number(amount));
    $("#bill").text(Number($("#bill").text())+((price-(price/100*Number($("#discount").text())))*Number(amount)));

    cookies = null;
    
    begin = document.cookie.indexOf('basket'+'=');
    if (begin != -1) {
        begin = document.cookie.indexOf('=', begin) + 1;
        end = document.cookie.indexOf(';', begin);
        if (end == -1) end = document.cookie.length;
        cookies = unescape(document.cookie.substring(begin, end));
    }

    if (cookies == null || cookies == ' ') cookies = '';
    if ((begin_item = cookies.indexOf(item)) != -1) {
        begin_amount = cookies.indexOf('(', begin_item) + 1;
        end_amount = cookies.indexOf(')', begin_item);
        amount = Number(amount)+Number(cookies.substring(begin_amount, end_amount));
        cookies = cookies.substring(0, begin_amount)+amount+cookies.substring(end_amount);
    } else 
        cookies = cookies+(cookies.length>1?'-':'')+item+'('+amount+')';
        
    document.cookie = 'basket'+'='+escape(cookies)+"; path=/";

    return false;
}






