$(document).ready(function(){
	
  
	//слайдер события
	if($('.slider-body').length > 0){
		
		var currentElem = 0; //текущий выбранный элемент
		var totalSize = $('#ageList li').size();
		var totalWidth = $('#ageList').width();
		var elemWidth = 60;
		var elemVisibleCount = totalWidth / elemWidth;
		var totalSdvig = 0; //сдвиг ползунка при перемещение шкалы
		var correctStep = 0; //корректировка шага для промежуточных шагов
		var promStepSdvig = 0; //сдвиг ползунка для промежуточных шагов
		var elemsToHide = totalSize - elemVisibleCount; //кол-во элементов для скрытия
		var promFlag = 1; //находимся на промежуточном шаге
		
		var elemLeftPosLeft = totalWidth / (totalSize);
		$('#ageList li').each(function(e){
			$(this).css('left', elemWidth*e);
			if(e >= elemVisibleCount){
				$(this).hide();
			}
		});
		$('#ageList li.small').each(function(){
			cLeft = +$('#big_' + $(this).attr('rev')).css('left').split('px')[0] + elemWidth / 2;
			$(this).css('left', cLeft);
			
		});
		
		//itemCounter
		
		function custAnimate(elemToShow){
			//переключение активного элемента
			if(elemToShow == -1){
				$('.slider-img:visible').hide('fade', function(){
					$('.main-img:eq(' + (currentElem) + ')').show('fade');				
				});
				$('.slider-txt:visible').hide('fade', function(){
					$('.main-text:eq(' + (currentElem ) + ')').show('fade');
				});
			} else {
				elId = $('#small_' + elemToShow).attr('rev');
				$('.slider-img:visible').hide('fade', function(){
					$('#img_' + elId).show('fade');				
				});
				$('.slider-txt:visible').hide('fade', function(){
					$('#text_' + elId).show('fade');
				});
				promFlag = 0;
			}
			
			//счетчик
			$('#itemCounter').html(currentElem + 1 + '/' + totalSize);	
			
			//анимация ползунка
				if(promStepSdvig == 0){
					newLeftCoor = (currentElem - correctStep)*elemWidth - 6 - totalSdvig + promStepSdvig;
				} else {
					newLeftCoor = (currentElem)*elemWidth - 6 - totalSdvig + promStepSdvig;
				}
			
			$('.slider-handle').animate({'left': newLeftCoor}, 500);
		}
		
		$('.slider-arrow-right').click(function(){
			currentElem++;
			if(currentElem >= totalSize){
				currentElem = totalSize - 1;
			} else {
				//промежуточные шаги
				if($('#ageList li:eq(' + (currentElem - 1) + ')').find('small').length > 0 && promFlag == 1){
						promStepSdvig = 30;
						promFlag = 0;
						currentElem--;
						custAnimate(currentElem);
				} else {
					promStepSdvig = 0;
					promFlag = 1;
					//сдвиг шкалы
					if(currentElem == elemVisibleCount && totalSdvig == 0){
						totalSdvig = elemsToHide * elemWidth;
						$('#ageList li').show(); //показываем скрытые элементы
						for(i = 0; i < elemsToHide; i++){ //скрываем элементы в начале списка
							$('#ageList li:eq(' + i + ')').hide();
						}
						$('#ageList li').each(function(){
							promLeft = +$(this).css('left').split('px')[0] - elemsToHide * elemWidth; 
							$(this).css('left', promLeft);
						});
					}
					custAnimate(-1);
				}
			}
			
			return false;
		});
		$('.slider-arrow-left').click(function(){
			currentElem--;
			if(currentElem < 0){
				currentElem = 0;
			} else {
			
				//промежуточные шаги
				if($('#ageList li:eq(' + (currentElem) + ')').find('small').length > 0 && promFlag == 1){
						promStepSdvig = 30;
						promFlag = 0;
						custAnimate(currentElem);
						currentElem++;
				} else {
					promStepSdvig = 0;
					promFlag = 1;
					//сдвиг шкалы
					if(currentElem == elemsToHide && totalSdvig != 0){
						totalSdvig = 0;
						$('#ageList li').show(); //показываем скрытые элементы
						for(i = (totalSize - elemsToHide); i < totalSize; i++){ //скрываем элементы в начале списка
							$('#ageList li:eq(' + i + ')').hide();
						}
						$('#ageList li').each(function(){
							promLeft = +$(this).css('left').split('px')[0] + elemsToHide * elemWidth;
							$(this).css('left', promLeft);
						});
					}
					custAnimate(-1);
				}
			}
			
			return false;
		});
		
	}
	
    $("#p_f_3_1").change(function(){
        $("#city_choise").submit();
    });

    $("#p_f_3_2").change(function(){
        $("#city_choise").submit();
    });
    
    
    var count = $(".cart-filter select").length;
    var width = $(".page-body-in2").width();
    //alert(width);
    
    var final_width = ((width - 30)-((count - 1) * 30)) / count;
    //alert(final_width);
    
    $(".cart-filter select").width(final_width);

    $(".cart-filter select").change(function(){
        $(".cart-filter form").submit();
    });
    
    $(".icon").mouseover(function(){
        var idx = $(this).attr("id");
        var text = $(".icon_text_" + idx).html();
        $(".tech_desc").html(text);
    });
    
    $(".icon").mouseout(function(){
        $(".tech_desc").html($(".icon_text_1").html());
    });
    
    $(".poll_elem table.faq").visualize({type:"pie", height: "300px", width: "420px", appendTitle: false, pieLabelPos: "inside"});
    
    $(".quest_wrap").click(function(){
        $(this).next().slideToggle();
        return false;
    });
    
    $(".parent_parent").mouseover(function(){
        $("#mainnav ul ul").hide();
        var idx = $(this).attr("rel");
        $("#parent_" + idx).show();
    });
    
    $(".child").mouseover(function(){
        $("#mainnav ul ul ul").hide();
        var idx = $(this).attr("rel");
        $("#parent_" + idx).show();
    });
    
    $("#content, #header, #leftcolumn").mouseover(function(){
        $("#mainnav ul ul").hide();
        //$(".left-menu ul ul").hide();
    });
    
    /*$(".left-menu .parent_").mouseover(function(){
        $(".left-menu ul ul").slideUp(1000);
        var idx = $(this).attr("rel");
        $("#parent_" + idx).slideDown(1000);
    });*/
    
    if($(".small_img img").length > 0){
        $(".big_img").cycle({
            fx:'fade',
            speed:1500,
            timeout:0,
            pager: '.small_img',
            pagerAnchorBuilder: function(idx, slide) {
                return '.small_img img:eq(' + idx + ')';
            }
        });
    }
    
    if($(".big_img a").length > 0){
        $(".big_img a[rel^='prettyPhoto']").prettyPhoto({
            animation_speed: 'fast',
            slideshow: false,
            autoplay_slideshow: false,
            opacity: 0.80,
            show_title: false,
            allow_resize: false,
            counter_separator_label: ' / ',
            theme: 'facebook',
            hideflash: false,
            wmode: 'opaque',
            autoplay: false,
            modal: false,
            overlay_gallery: false,
            keyboard_shortcuts: false
        });
    }
    
    if($("#icons_desc div").length > 0){
        $(".icon a[rel^='prettyPhoto']").prettyPhoto({
            animation_speed: 'fast',
            slideshow: false,
            autoplay_slideshow: false,
            opacity: 0.80,
            show_title: false,
            allow_resize: false,
            counter_separator_label: false,
            theme: 'facebook',
            hideflash: false,
            wmode: 'opaque',
            autoplay: false,
            modal: false,
            overlay_gallery: false,
            keyboard_shortcuts: false
        });
    }
    
    if($(".cart a").length > 0){
        $(".cart a[rel^='prettyPhoto']").prettyPhoto({
            animation_speed: 'fast',
            slideshow: false,
            autoplay_slideshow: false,
            opacity: 0.80,
            show_title: false,
            allow_resize: false,
            counter_separator_label: ' / ',
            theme: 'facebook',
            hideflash: false,
            wmode: 'opaque',
            autoplay: false,
            modal: false,
            overlay_gallery: false,
            keyboard_shortcuts: false
        });
    }
    
    if($(".inn").length > 0){
        $(".inn_link[rel^='prettyPhoto']").prettyPhoto({
            animation_speed: 'fast',
            slideshow: false,
            autoplay_slideshow: false,
            opacity: 0.80,
            show_title: false,
            allow_resize: false,
            counter_separator_label: false,
            theme: 'facebook',
            hideflash: false,
            wmode: 'opaque',
            autoplay: false,
            modal: false,
            overlay_gallery: false,
            keyboard_shortcuts: false
        });
    }
    
    $(".send-res").click(function(){
    	$("#res-form").slideToggle(700);
    });
    
    if($(".sysFalse, .sysTrue").length > 0){
    	$("#res-form").show();
    }
 
});


$(window).load(function(){
	
		// jQuery Plugin equalHeight

(function($) {
$.fn.equalHeight = function() {
var group = this;
$(window).bind('resize', function(){
var tallest = 0;
$(group).height('auto').each(function() {
tallest = Math.max(tallest, $(this).height());
}).height(tallest);
}).trigger('resize');
}
})(jQuery) 
	
	$(".similar-item").equalHeight();
});
