$(document).ready(function(){

	var fff = window.itemCounter - window.pageCurrent*window.currentPP;
	if (fff<=0) { $('.page_filter_button_right').fadeTo(200,0.1); }
	if (window.pageCurrent == 1) { $('.page_filter_button_left').fadeTo(200,0.1); }
				
				
//	$('#main_navigation').css('z-index','800');
//	$('#main_navigation').css('left','0px');
	$('#main_navigation').animate({top:'0px'}, 600);

	

	window.reHight = function() {
		var new_hight_left = parseInt($('#left_block').css('height'))-159;

		var hight_bookmarks = parseInt($('#bookmarks').css('height'));
		var hight_cart = parseInt($('#cart').css('height'));
		var new_hight_right = 170+hight_bookmarks+hight_cart;
		
		var hight_center = parseInt($('.catalog_inner').css('height'));		
		
		var new_hight = 600;

		if (new_hight_left > new_hight) new_hight = new_hight_left;
		if (new_hight_right > new_hight) new_hight = new_hight_right;
//		alert( new_hight_left+'-'+new_hight_right+'-'+hight_center+'-'+new_hight);		
		if (hight_center != new_hight) {
			$('.catalog_inner').animate({minHeight: new_hight});
		}
	}
//	window.reHight();
			
// currency
    $('.current_currency').click(function(){

        if ($('.currency_arrow').hasClass('open')) {
            $('.currency_arrow').removeClass('open');
            $('.currency_list').stop().fadeTo(600, 0, function(){
				$(this).hide();
			});
        }
        else {
            $('.currency_arrow').addClass('open');
            $('.currency_list').stop().fadeTo(600, 1);
        }
		return false;
    });

    $('ul.currency_list li').click(function(){
        if ($('.currency_arrow').hasClass('open')) {
            $('.currency_arrow').removeClass('open');
            $('.currency_list').stop().fadeTo(600, 0, function(){
				$(this).hide();
			});
        }
		var htmlStr = $(this).html();
		var htmlId  = $(this).attr('id');
		$(".current_currency").text(htmlStr);
		$.cookie("cur", htmlId, { path: '/' });
		var href = window.location.href;
		href = href.replace(/p:([0-9]+):([0-9]+):([0-9]+)\//gi, '');
		href = href.replace(/#([a-z]+)/gi, '');
		window.location.href = href;
		return false;
    });


// profile
    $('.current_profile').click(function(){
		//alert('Меню профайла пользователя');
        if ($('#profile').hasClass('open')) {
            $('#profile').removeClass('open');
            //$('.profile_list').fadeOut();
            $('.profile_list').stop().fadeTo(600, 0, function(){
				$(this).hide();
			});
        }
        else {
            $('#profile').addClass('open');
            //$('.profile_list').fadeIn();
            $('.profile_list').stop().fadeTo(600, 1);
        }
		return false;
    });


    $('.profile_list li a').click(function(){
		alert('Выбор пункта меню профайла пользователя');
        if ($('#profile').hasClass('open')) {
            $('#profile').removeClass('open');
            //$('.profile_list').fadeOut();
            $('.profile_list').stop().fadeTo(600, 0, function(){
				$(this).hide();
			});
        }
		return false;
    });

// office
    $('.current_office').click(function(){
		//alert('Выбор офиса');
        if ($('.office').hasClass('open')) {
            $('.office').removeClass('open');
            //$('.agency_list').fadeOut();
            $('.office_list').stop().fadeTo(600, 0, function(){
				$(this).hide();
			});
        }
        else {
            $('.office').addClass('open');
            //$('.agency_list').fadeIn();
            $('.office_list').stop().fadeTo(600, 1);
        }
		return false;
    });

    $(".office_list li").click(function () {

       if ($('.office').hasClass('open')) {
            $('.office').removeClass('open');
            $('.office_list').stop().fadeTo(600, 0, function(){
				$(this).hide();
			});
        }

		$(".office_list li").removeClass("current");
		$(this).addClass("current");
		var htmlStr = $(".office_list li.current span.town").html();
//		$(".current_office").text(htmlStr);
		var phone = $(".office_list li.current span.phone").html();
		$(".phones").html(phone);
		var htmlId  = $(this).attr('id');
		$.cookie("off", htmlId, { path: '/' });		
		window.location.reload();
		return false;
    });






// callback

    $('.callback_text').click(function(){
//	alert('Форма заказа обратного звонка');
        if ($('.callback').hasClass('open')) {
            $('.callback').removeClass('open');
            $('.callback_form').stop().fadeTo(600, 0, function(){
				$(this).hide();
			});
        }
        else {
            $('.callback').addClass('open');
            $('.callback_form').stop().fadeTo(600, 1);
        }
		return false;
    });

	$('.callback_form').load('/includes/~callback.html', function(){
		$('.send').click(function(){
		
			var name = $('#cbn').val();
			var phone = $('#cbp').val();
			var city = $('#cbc').val();
			var message = $('#cbm').val();
		
			$.post("/sendcb/", { n:name, p:phone, c: city, m:message }, function(data) { $.msgbox("Ваше сообщение отправлено. Спасибо!" + data, {type: "info"});}  );
		
			if ($('.callback').hasClass('open')) {
				$('.callback').removeClass('open');
				$('.callback_form').stop().fadeTo(600, 0, function(){
					$(this).hide();
				});
			}
			return false;
		});
	})



		$('#rsend').click(function(){
		
			var name = $('#rname').val();
			var text = $('#rtext').val();
			var rate = $('#rrate').val();
			var pid = $('#rid').val();
			if (rate > 0) {
				$.post("/sendrv/", { n:name, t:text, r:rate, p:pid }, function(data) { 
					$.msgbox("Ваше отзыв получен и отправлен на модерацию. Спасибо!", {type: "info"}); 
					$('.rf').fadeTo(600, 0);
				}  	);
			}
			return false;
		});
		


var current = 0;

var iterate = function() {
		$(".phones").hide();
		if (current == 1) {
			$(".phones").html('<img src=\"/img/phone_kiev.png\">');
			current = 2;
		} else if (current == 2) {	
			$(".phones").html('<img src=\"/img/phone_odessa.png\">');
			current = 3;
		} else if (current == 3) {	
			$(".phones").html('<img src=\"/img/phone_harkov.png\">');
			current = 1;
		}
		$(".phones").fadeTo(300, 1);
}

	if (!$.cookie("off")) {
		current = 1; 
		var slidetime = setInterval(iterate,3000);
	}

    /**********/



    $('.price_pay_arrow').click(function(){
        if ($('.price_pay_arrow').hasClass('open')) {
            $('.price_pay_arrow').removeClass('open');
            $('.price_pay_list').fadeOut();
        }
        else {
            $('.price_pay_arrow').addClass('open');
            $('.price_pay_list').fadeIn();
        }
		return false;
    });

	$('.price_pay_button').click(function(){
//		alert('Купить товар 2');
		var product = $(this).attr('rel');
	    buyproduct(product);
//	    shoppingcart();
		return false;
    });

	$('li.buy_now').click(function(){
//		alert('Купить сейчас товар');
		$('.price_pay_arrow').removeClass('open');
	    $('.price_pay_list').fadeOut();
	    var product = $(this).parent().attr('rel');
	    buyproduct(product);
		return false;
	});
	$('li.to_cart').click(function(){
//		alert('Положить в корзину товар');
		$('.price_pay_arrow').removeClass('open');
	    $('.price_pay_list').fadeOut();
	    var product = $(this).parent().attr('rel');
	    addproduct(product,'productlist');
		shoppingcart();
		return false;
	});
	$('li.to_bookmarks').click(function(){
//		alert('Добавить в закладки товар');
		$('.price_pay_arrow').removeClass('open');
	    $('.price_pay_list').fadeOut();
	    var product = $(this).parent().attr('rel');
	    addproduct(product,'wishlist');
		bookmarks();		
		return false;
	});





    /**********/



    $('.page_filter_text').click(function(){
//		alert('Выбор фильтра');
        if ($('.page_filter_arrow').hasClass('open')) {
            $('.page_filter_arrow').removeClass('open');
            $('.page_filter_list').fadeOut();
        }
        else {
            $('.page_filter_arrow').addClass('open');
            $('.page_filter_list').fadeIn();
        }
		return false;
    });


    $("ul.page_filter_list li a").click(function () {
//		alert('Выбор текущего фильтра page');
		var htmlStr = $(this).html();
		$('a.page_filter_text').text(htmlStr+ ' из ' + window.itemCounter);
		$('.page_filter_arrow').removeClass('open');
        $('.page_filter_list').fadeOut();
   		window.pageCurrent = $(this).parent().attr('value');
		reloadWithAjax();
		return false;
    });

// back
	$(".page_filter_button_left").click(function () {	
		if ( window.pageCurrent > 1 ) { 	
			window.pageCurrent--;	
//			if (window.pageCurrent > 1) { window.location.hash = window.pageCurrent } else { window.location.hash = '' }
			reloadWithAjax();
		} 
		return false;
    });
// front
	$(".page_filter_button_right").click(function () {
		var maxpage = Math.ceil(window.itemCounter/window.currentPP);
		if ( window.pageCurrent < maxpage ) { 	
			window.pageCurrent++;	
//			window.location.hash = window.pageCurrent;
			reloadWithAjax();		
		} 
		return false;
    });


    /**********/




// показывать все/не все
    $('.rating_filter_text').click(function(){
        if ($('.rating_filter_arrow').hasClass('open')) {
            $('.rating_filter_arrow').removeClass('open');
            $('.rating_filter_list').fadeOut();
        }
        else {
            $('.rating_filter_arrow').addClass('open');
            $('.rating_filter_list').fadeIn();
        }
		return false;
    });


    $("ul.rating_filter_list li a").click(function () {
		var htmlStr = $(this).html();
		var htmlId  = $(this).parent().attr('value');
		$.cookie("ps", htmlId, { path: '/' });	
		window.currentPS = htmlId;
		$("a.rating_filter_text").text(htmlStr);
		$('.rating_filter_arrow').removeClass('open');
        $('.rating_filter_list').fadeOut();
        var href = window.location.href;
        href = href.replace(/pg([0-9]+)\//gi, '');
		window.location.href=href;
		return false;
    });





    /**********/



//	выводить по
	$('.show_filter_text').click(function(){
        if ($('.show_filter_arrow').hasClass('open')) {
            $('.show_filter_arrow').removeClass('open');
            $('.show_filter_list').fadeOut();
        }
        else {
            $('.show_filter_arrow').addClass('open');
            $('.show_filter_list').fadeIn();
        }
		return false;
    });

//  выбор выводить по
    $("ul.show_filter_list li").click(function () {
		var htmlId  = $(this).attr('value');
		$.cookie("pp", htmlId, { path: '/' });
		window.currentPP = htmlId;
		$("a.show_filter_text").text('по ' + htmlId);
		$('.show_filter_arrow').removeClass('open');
        $('.show_filter_list').fadeOut();
  		window.pageCurrent = 1;
        reloadWithAjax();
        menuPagesClear();
		return false;		
    	});

function menuPagesClear() {
        $(".page_filter_list li").each(function () {        
        	$(this).remove();            
        });    
		$(".page").text('');
        var maxpage = Math.ceil(window.itemCounter/window.currentPP);
        var href = window.location.href;
		href = href.replace(/pg([0-9]+)\//gi, '');
		
		var maxtoppage = maxpage;
        if ( maxtoppage > 25 ) { maxtoppage = 25; }
		for(var i=1;i<=maxtoppage;i++) {
        	var mclass = '';
        	if (i==1) { mclass = 'class=\"first\"'; }
        	else if (i==maxtoppage) { mclass = 'class=\"last\"'; }
        	var pmin = (i-1) * window.currentPP + 1;
			var pmax = i * window.currentPP;
			if (pmax > window.itemCounter) { pmax = window.itemCounter; }
			$(".page_filter_list").append('<li value=\"'+i+'\" ' + mclass + '><a href=\"#\">'+pmin+'-'+pmax+'</a></li>');
		}
		$(".page").append(' ( страница ' + window.pageCurrent + ' из ' + maxpage + ' ) '  + '<a href=\"' + href + 'pg2/\">&rarr;</a>');
		return false;
}

    /**********/

	$('.output_filter_text').click(function(){
	    if ($('.output_filter_arrow').hasClass('open')) {
            $('.output_filter_arrow').removeClass('open');
            $('.output_filter_list').fadeOut();
        }
        else {
            $('.output_filter_arrow').addClass('open');
            $('.output_filter_list').fadeIn();
        }
		return false;
    	});

    $("ul.output_filter_list li a").click(function () {
		var htmlStr = $(this).html();
		var htmlId = $(this).parent().attr('value');
		$.cookie("po", htmlId, { path: '/' });
		window.currentPO = htmlId;	
		$("a.output_filter_text").text(htmlStr);
		$('.output_filter_arrow').removeClass('open');
        $('.output_filter_list').fadeOut();
        reloadWithAjax();
		return false;
    });





    /**********/


	$('#aut').click(function(){
		var ul = $('#profile').find('#ul').val();
		var up = $('#profile').find('#up').val();
		$.post("/authen/", { user_login:ul, user_password:up, authen_type:'inline' }, function(data) { 
			if(data == '0') {
				alert('Не получилось...');
			
			} else {
				$('#profile .info').html(data);		
				var href = window.location.href;
				window.location.href = href;					
			}
		});
		return false;
    });
    
    




	$(".left_nav li a.category_link").each(function (i) {
    
    $(this).click(function(){
	
    var menuID = $(this).attr('id');

		if ($(this).next().find('.subcategory').length>0 && $(this).next().find('.subcategory').is(':visible')) {
			$(this).next().find('.subcategory').fadeOut(1);
			$(this).parent().removeClass('active');
		}
		else {
			var link = $(this);
			$(this).next().load('/shop/~menu/', {mi: menuID}, function(data){
//menuShow();
				$('.subcategory').fadeOut(1);
				$(this).find('.subcategory').fadeIn(1);
				$('.left_nav li a.category_link').removeClass('active');
				$('.left_nav li').removeClass('active');
				link.addClass('active');
				link.parent().addClass('active');
			});
		}

		$('.subcategoryclosebutton').live('click', function(){
			$(this).parent().fadeOut(1);
		})

		return false;

    });

});


function menuShow () {

alert('1');
$('body').append("<div class=\"subcategory\" id=\"subcat1\"><div class=\"subcategoryleftarraw\"></div><div class=\"subcategoryclosebutton\"></div><div class=\"subcategory_head\"><div class=\"subcategory_head_left\"></div><div class=\"subcategory_head_center\"></div><div class=\"subcategory_head_right\"></div></div><div class=\"subcategory_content\">");

$('body').append("</div><div class=\"subcategory_footer\"><div class=\"subcategory_footer_left\"></div><div class=\"subcategory_footer_center\"></div><div class=\"subcategory_footer_right\"></div></div></div>");
	
	return false;
}



});

