$(document).ready(function(){
    /*var timer, timer2, timer3;
    $('#mn').mouseover(function(){
            clearTimeout(timer);
            clearTimeout(timer2);
            clearTimeout(timer3);
            if ($('#mnt').attr('_h1') > 0)
            {
                $('#mnt').attr('_h1', 0).stop(true).animate({height: $('#mnt').attr('_h')}, 3000);
                $('#mnr').stop(true);
                if ($('#mnr').height() == $('#mnt').attr('_h')) 
                    $('#mnr').animate({height: $('#mnr').attr('_h')}, 500);
                $('#omn').stop(true).hide();
            }
          }).mouseout(function(){
            $('#mnt').attr('_h1', 2);           
            timer=setTimeout("if($('#mnt').attr('_h1') == 2) $('#mnt').slideUp(3000) ;", 6000); 
            timer2=setTimeout("if($('#mnt').attr('_h1') == 2) $('#mnr').slideUp(500) ;", 9000); 
            timer3=setTimeout("if($('#mnt').attr('_h1') == 2) $('#omn').slideDown('slow');", 9500);    
        });        
    $('#omn').click(function(){
            $('#mnr').slideDown(500);
            $('#mnt').slideDown(3000);
            $('#mn').unbind('mouseover').unbind('mouseout');
        });
    $('#mnt').attr('_h', $('#mnt').height()).attr('_h1', 1);    
    $('#mnr').attr('_h', $('#mnr').height());
    $('#omn').attr('_h', $('#omn').height());
    setTimeout("if($('#mnt').attr('_h1') == 1) $('#mnt').slideUp(3000) ;", 3000); 
    setTimeout("if($('#mnt').attr('_h1') == 1) $('#mnr').slideUp(500) ;", 6000); 
    setTimeout("if($('#mnt').attr('_h1') == 1) $('#omn').slideDown('slow');", 6500);    */
	$('.registration').click(function(){
		$('#qform input[name=cf]').val(3);
		$('#qform').submit();
		return false;
	});
	
	$('.map-in span').click(function(){
		var a = $(this), ul = $(this).next().next();
		if (a.is('.collapsed')) {
		  a.addClass('expanded').removeClass('collapsed');
		  ul.show();
		} 
		else
		if (a.is('.expanded')) {
		  a.addClass('collapsed').removeClass('expanded');
		  ul.hide();
		}
	  });
  
    
    $('.block').click( function(){
        window.location = $('#i'+this.id).attr('href');
    });
    $('.card').click( function(){
        eval($(this).attr('href'));
        return false;
    });
    
              
    $('#singIn').click( function(){
        $.post('/profile/',$('#sign').serializeArray(),function (data) {
        if (data == 1) window.location='/profile/'; else if ($('#er').length == 0) {$('#TB_ajaxContent').height(155); $('#pr').after('<div id="er" class="cost">Вы ввели неверный логин или пароль</div>');}});
        return false;
    });
    $('#il').height($('#img1').height());
	var incVal;
	var decVal;
     if ($.browser.safari){
	    $(".nav-holder .nav td a").css({fontWeight:"normal"})
     };	
	$(".opp").hoverIntent(function() {
            var opp = this;
            $('.grey', opp).fadeIn(300);
            $('.grey', opp).css({height: $('.white', opp).height()});
        }, function() {
            var opp = this;
            $('.grey', opp).fadeOut(300);
        });

	$('.more').click( function(){
			incVal = $(this).prev().prev().attr("value");
			incVal++;
			$(this).prev().prev().attr({value:incVal});
			calculate();
		});
	$('.less').click( function(){
			decVal = $(this).next().attr("value");
			if (decVal > 1){
				decVal--;
				$(this).next().attr({value:decVal});
				calculate();
			}
		});
    $('.db').click(function(){
        $.cookie("basket",null,{path: '/'});
        window.location='/cart/';
    });		
    $('.zakaz').click(function(){
        $.post('/zakaz/',{'hide': this.id.replace('id', '')},function (data) {});
        window.location='/zakaz/';
    });	
    if($('#current').length 
           && ($.cookie("currency") > 0)
           && ($('#current').val() != $.cookie("currency"))) {
        converting($.cookie("currency"));
    }
	$('#img2').css({"display":"none"});
	$(".activeimage img").css({opacity:"0.5"});
	$(".activeimage").css({cursor:"default"});
	var kolvo = $("#imgholder img").length;
	for (i=0;i<kolvo;i++){
		$("#imgholder img").height(35);
	}
	leng = $(".baner-pages a").length;
	$(".baners div").css({"display":"none"});
	$(".baners .active").css({"display":"block"});
	speed = 1000;
	for(i=0;i<leng;i++){
		tempA = $(".baner-pages a")[i];
		$(tempA).attr("id","banerpage"+i);
	}
	$(".baner-pages a").click(function(){
		if (!($(this).hasClass("active"))){
			_id = $(this).attr("id");
			$(".baner-pages a").removeClass("active");
			$(this).addClass("active");
			//$(".baners .active").hide(speed).removeClass("active");
			//$("."+_id).show(speed).addClass("active");
			$(".baners .active").fadeOut(speed).removeClass("active");
			$("."+_id).fadeIn(speed).addClass("active");
		};
	});	
	$("#imgholder img").hover(
			function(){
					if (!($(this).parent().hasClass("activeimage"))){
						$(this).animate({opacity:"0.5"}, 500)
					}
				},
			function(){
					if (!($(this).parent().hasClass("activeimage"))){
						$(this).animate({opacity:"1"}, 500)
					}
			});
    $("#is_company").click(			 
			  function () {
	             if (this.checked)	$(".company").fadeIn(300);
	             else  $(".company").fadeOut(300);
			  }			  
			);
	$('.br').hover(
			function(){
			        $($(this).children()[1]).fadeIn(1500);
			        $($(this).children()[0]).fadeOut(1500);
				},
			function(){
			        $($(this).children()[0]).fadeIn(1500);
			        $($(this).children()[1]).fadeOut(1500);
			});		
			
			
});

function change_image(d,_src) {   
		var srcPrev = $('#img1').attr("src");
		if (_src && (srcPrev != _src)){
    		$('#imgholder td a').removeClass('activeimage');
			$("#imgholder img").css({opacity:"1"});
			$("#imgholder a").css({cursor:"pointer"});
			$('#img1').attr({src:_src}).css({zIndex:"1",display:"none"});
			$('#img2').attr({src:srcPrev}).css({zIndex:"2", display:"block"});
			$('#'+d).addClass('activeimage');
			$(".activeimage img").css({opacity:"0.5"});
			$(".activeimage").css({cursor:"default"});
			$("#img1").fadeIn(1500);
			$("#img2").fadeOut(1500);
		}
}
function converting(id) {
    var i=$('#current').val();
    $('#cur'+id).html('<span>'+$('#cur'+id).text()+'</span>');
    $('#cur'+i).html('<a href="javascript:converting(' + i + ');">'+$('#cur'+i).text()+'</a>');
    $('#current').val(id);
    var ids =$('.prc');
    var price=0;
    for (var j=0; j<ids.length; j++) {
        price = $(ids[j]).text().replace(' '+$('#cur'+i).text(), '');
        price = Math.round(price * $('#cur_'+i).val() / $('#cur_'+id).val()*100)/100;
        $(ids[j]).text(price + ' ' + $('#cur'+id).text());
    }
    $.cookie("currency", id, {expires: 7, path: '/'});
}
function calculate() {
    var i=$('#current').val();
    var ids =$('.cprc');
    var idjs =$('.counter');
    var price=0;
    for (var j=0; j<ids.length; j++) 
        price += parseFloat($(ids[j]).text().replace(' '+$('#cur'+i).text(), '')) * parseInt($(idjs[j]).val());
    $('#tcart').text(price + ' ' + $('#cur'+i).text());         
    price = Math.round(price * $('#cur_'+i).val() / $('#main_currency').val()*100)/100;    
    var total=0;
    for (var j=0; j<idjs.length; j++) 
       total += parseInt($(idjs[j]).val());
    $('#bcount').text(total);
    $('#bprice').text(price);        
}
function del_filter(url,id) {
    $('#'+id).remove();
    $('.back').attr('href',url);
}

function dbask(id,price){
if ($('#i'+id).length)
{
    $('#i'+id).addClass('content2grey');
    $('#i'+id).removeClass('content');
}
$.cookie('basket', $.cookie('basket')+';'+id+':1:'+price+';', {expires: $.datemaker('+5m'), path: '/'});
$('<span class="button1 card">'+$('#b'+id).html().replace('корзину', 'корзине')+'</span>').insertAfter($('#b'+id));
$('#b'+id).remove();
$('#bcount').text(parseInt($('#bcount').text())+1);
$('#bprice').text(parseFloat($('#bprice').text())+price);
$('#ecart').css({display:"none"});
$('#mcart').css({display:"block"});
}
function delbask(id){
var ck=(';'+$.cookie('basket')).replace(new RegExp(';'+id+':[0-9]+:[0-9\.\,]+;'),';').replace(';;', ';').replace(/^[;]+/, '');
if (ck == '') {
    $.cookie('basket', null,{path: '/'});
    window.location='/cart/';
}
else {
    $.cookie('basket', ck, {expires: $.datemaker('+5m'), path: '/'});
    $('#r'+id).remove();
    calculate();
}
}

