var tw = jQuery('#textwrapper').height();
var leftsidep = jQuery('#leftside');
var mainp = jQuery('#rightside');
var menuli = jQuery('.mainmenu li');
var prbar = jQuery('#product_bar li');

var addListUl = jQuery('#carousel_ul');
var addListLi = jQuery('#carousel_ul li');
var addLiSize = addListLi.size();
var addWrapArrow = jQuery('#additional_wrapper .arrow');
var addWrapBack = jQuery('#additional_wrapper .back');
var addWrapNext = jQuery('#additional_wrapper .next');

var wtb = jQuery('div#wtb');
var wtbOpen = jQuery('a.wheretobyu');
var wtbClose = jQuery('a#wbt_close');

function get_offset(obj)
{
	var top = 0, left = 0;
	while (obj)
	{
		top += obj.offsetTop;
		left += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return {top: top, left: left};
}	

function pz_submit_logon_form()
{
	document.getElementById('error_msg').style.display = 'none';
	document.getElementById('logon_form').submit();
	return false;
}

function recalc_footer()
{
	return false;
	jQuery("#footer").css({
			'position': 'absolute',
			'top': (Math.max(jQuery(window).height(),document.documentElement.clientHeight) - 5) + 'px'
		});
}

function trim(string)
{
	return string.replace(/(^\s+)|(\s+$)/g, "");
}

jQuery(window).bind("load resize", function() {
	var dHeight = function(){return Math.max(jQuery(window).height(),document.documentElement.clientHeight)};
	var minH = document.documentElement.clientHeight;
	if (tw < minH) {
		leftsidep.css({height:minH});
	} else {
		leftsidep.css({height:tw});
	}
	
	var body = jQuery("body");
	var width = body.width() - 200;
	var height = jQuery(window).height()
	jQuery("#hero-banner").css({'width': (document.documentElement.clientWidth - 400) + 'px', 'height': (document.documentElement.clientHeight - 80) + 'px'});
	
	recalc_footer();
});	
//
function search_close()
{
	window.search_results.css({'display': 'none'});
	jQuery("#search-model").val('Поиск по всем товарам');
}
function do_search(page)
{
	window.search_results.load('./quicksearch/?request=' + jQuery("#search-model").val() + '&p=' + page, function() {
			if (trim(window.search_results.html()).length > 20)
				window.search_results.css({display: 'block'});
			else
				window.search_results.css({display: 'none'});
		});
}
jQuery(document).ready(function() {	
	// .lg-button
	jQuery(".lg-button").each(function() {
			jQuery(this).html("<div><span class=\"lg-button-left\"></span><span class=\"lg-button-contents\">" + jQuery(this).html() + "</span></div><br clear=\"all\" />");
		});
	//jQuery(".button").prepend(jQuery("<span class=\"lg-button-left\"></span>")).append(jQuery("<span class=\"lg-button-right\"></span>"));
	
	// Поиск по сайту
	window.search_results = jQuery('<div class="search-results"></div>');
	window.search_results.css({'display': 'none'});
	jQuery(".search-close").click(function() {
			return false;
		});
	jQuery('body').append(window.search_results);
	jQuery("#search-model").focus(function() {
			if (this.value == 'Поиск по всем товарам') this.value = '';
		}).blur(function() {
			if (this.value == '') this.value = 'Поиск по всем товарам';
		}).val('Поиск по всем товарам').keyup(function() {
			if (trim(this.value).length == 0)
			{
				clearTimeout(window.search_tm);
				window.search_tm = null;
				return;
			}
			if (window.search_tm) clearTimeout(window.search_tm);
			window.search_tm = setTimeout(function() {
					window.search_tm = null;
					do_search(0);
				}, 300);
		});

	// Ширина табов
	var pbar = jQuery("#product_bar li");
	var size = pbar.size();
	if (size)
	{
		if (pbar.index(jQuery("#product_bar li.double-width")) > -1)
			size++;
		var width = Math.floor(712 / size);
		pbar.css({'width': width + 'px'});
		jQuery("#product_bar li.double-width").css({'width': (width * 2) + 'px'});
	}
		
	//Ховеры меню						  
	menuli.bind("mouseover", function() {	
		if	(jQuery(this).hasClass("level")) {											   
			jQuery(this).addClass("open");
		}
	});	
	menuli.bind("mouseout", function() {	
		if	(jQuery(this).hasClass("level")) {											   
			jQuery(this).removeClass("open");
		}
	});		
	//Ховеры плашки продукта
	prbar.bind("mouseover mouseout", function() {											   
		jQuery(this).toggleClass("focus");
		jQuery("a",this).toggleClass("focus");
	});
	
	//Переключение табов контента
	var barLi = jQuery('#product_bar li');
	barLi.click(function(){
		var tabName = jQuery(this).attr("id");
		jQuery('div.product_review').css({display:"none"});
		jQuery('div.product_gallery').css({display:"none"});
		jQuery('div.product_specs').css({display:"none"});
		jQuery('div.product_files').css({display:"none"});
		jQuery('div.'+tabName).css({display:"block"});

		// страница партнерской зоны
		jQuery('div.pz_catalogues').css({display:"none"});
		jQuery('div.pz_videos').css({display:"none"});
		jQuery('div.pz_photos').css({display:"none"});
		jQuery('div.pz_press').css({display:"none"});
		jQuery('div.pz_manuals').css({display:"none"});
		jQuery('div.pz_adv_materials').css({display:"none"});
		
		// страница справочника покупателя
		jQuery('div.bg_general').css({display:"none"});
		jQuery('div.bg_home').css({display:"none"});
		jQuery('div.bg_commercial').css({display:"none"});

		jQuery('#product_bar li a').removeClass("product_open");
		jQuery("a",this).addClass("product_open");
		
		recalc_footer();
		return false;
	});	
	//Переключение табов гарантии
	barLi.click(function(){
		var tabName = jQuery(this).attr("id");
		jQuery('div.guarantee_desc').css({display:"none"});
		jQuery('div.guarantee_period').css({display:"none"});
		jQuery('div.guarantee_obligation').css({display:"none"});
		jQuery('div.guarantee_restriction').css({display:"none"});
		jQuery('div.guarantee_malfunction').css({display:"none"});
		jQuery('div.'+tabName).css({display:"block"});
		jQuery('#product_bar li a').removeClass("product_open");
		jQuery("a",this).addClass("product_open");
		recalc_footer();
		return false;
	});		
	//Переключение табов гарантии
	jQuery('a.showguarantee').click(function(){
		jQuery('div.guarantee_desc').css({display:"block"});
		jQuery('div.guarantee_period').css({display:"none"});
		jQuery('div.guarantee_obligation').css({display:"none"});
		jQuery('div.guarantee_restriction').css({display:"none"});
		jQuery('div.guarantee_malfunction').css({display:"none"});
		recalc_footer();
		return false;
	});		
	
	//Крутилка просмотренных товаров
	if (addLiSize < 4) { addWrapArrow.hide(); }
	addListUl.css({left:'0'});
    jQuery('#carousel_ul li:first').before(jQuery('#carousel_ul li:last')); 
	addWrapBack.click(function(){
		var item_width = addListLi.outerWidth();
		var left_indent = parseInt(addListUl.css('left')) - item_width;
		jQuery('#carousel_ul:not(:animated)').animate({'left' : left_indent},500,function(){    
			jQuery('#carousel_ul li:last').after(jQuery('#carousel_ul li:first'));
			addListUl.css({'left' : '-206px'});
		}); 
		return false;
	});
	addWrapNext.click(function(){
		var item_width = addListLi.outerWidth();
		var left_indent = parseInt(addListUl.css('left')) + item_width;
		jQuery('#carousel_ul:not(:animated)').animate({'left' : left_indent},500,function(){    
			jQuery('#carousel_ul li:first').before(jQuery('#carousel_ul li:last')); 
			addListUl.css({'left' : '-206px'});
		});
		return false;
	});	
	//Коллапсинг описаний
	var collapser = jQuery('.expandable h4');
	var collapse = jQuery('.expandable dl');
	var expander = jQuery('.expander');
	
	//collapse.hide();
	collapser.click(function(){
		jQuery(this).next(".expandable dl").slideToggle(600);
		jQuery(this).toggleClass("opened");
		return false;
	});	
	
	jQuery("div.question h3").click(function(){
		jQuery(this).toggleClass("opened");											 
		jQuery(this).next("div.question .answer").slideToggle(600);		
		return false;
	});												 
 
	
	expander.click(function(){		
		if(expander.hasClass("opened")) {	
			collapse.slideUp(600);
			collapser.removeClass("opened");
			expander.removeClass("opened");
			jQuery(this).empty();
			jQuery(this).append("Развернуть все характеристики");
		}	else {
			collapse.slideDown(600);
			collapser.addClass("opened");
			expander.addClass("opened");
			jQuery(this).empty();
			jQuery(this).append("Закрыть все характеристики");
		}			
		return false;
	});	
	// Где купить
	wtbOpen.click(function(){
		wtb.css({left:"0px"});					   
		return false;
	});	
	wtbClose.click(function(){
		wtb.css({left:"-3000px"});					   
		return false;
	});	
	
	// Галерея
	var Gallery = jQuery('div#gallery_box img');
	var GalleryCaption = jQuery('div#gallery_caption');
	var GalleryFadings = jQuery('div#gallery_box img, div#gallery_caption');
	var GallerySlide = jQuery('div#product_gallery_small');
	var GallerySlideUl = jQuery('div#product_gallery_small ul');
	var GallerySlideLi = jQuery('div#product_gallery_small li');
	var GallerySlideLiSize = GallerySlideLi.size();
	var GalleryArrow = jQuery('div#product_gallery_small .arrow');
	var GalleryBack = jQuery('div#product_gallery_small .back');
	var GalleryNext = jQuery('div#product_gallery_small .next');
	
	if (GallerySlideLiSize == 1)
		GallerySlide.css({left: '94px'});
	else if (GallerySlideLiSize == 2)
		GallerySlide.css({left: '47px'});
	//GallerySlideUl.css({'left' : '-206px'});

	GallerySlideLi.bind("mouseover", function() {	
		jQuery(this).animate({opacity:'0.8'},200);
	});		
	GallerySlideLi.bind("mouseout", function() {	
		jQuery(this).animate({opacity:'1'},200);
	});	
	GallerySlideLi.click(function(){
		var GallerySlideImg = GallerySlideLi.index(this);	
		GalleryFadings.fadeOut(300);
		jQuery(this).oneTime(500, function() {
			Gallery.attr('src', gallery_images[GallerySlideImg]);
			GalleryCaption.html(jQuery('img', jQuery(this)).attr("title"));
			GalleryFadings.fadeIn(300);
		});
		return false;
	});	
	//
	
	if (GallerySlideLiSize < 4) { GalleryArrow.hide(); }
	GallerySlideUl.css({left:'0'});
    jQuery('div#product_gallery_small li:first').before(jQuery('div#product_gallery_small li:last')); 
	GalleryBack.click(function(){
		var item_width = GallerySlideLi.outerWidth();
		var left_indent = parseInt(GallerySlideUl.css('left')) - item_width;
		jQuery('div#product_gallery_small ul:not(:animated)').animate({'left' : left_indent},500,function(){    
			jQuery('div#product_gallery_small li:last').after(jQuery('div#product_gallery_small li:first'));
			GallerySlideUl.css({'left' : parseInt(GallerySlideUl.css('left')) + item_width});
		}); 
		return false;
	});
	GalleryNext.click(function(){
		var item_width = GallerySlideLi.outerWidth();
		var left_indent = parseInt(GallerySlideUl.css('left'));
		jQuery('div#product_gallery_small li:first').before(jQuery('div#product_gallery_small li:last')); 
		GallerySlideUl.css({'left' : parseInt(GallerySlideUl.css('left')) - item_width});
		jQuery('div#product_gallery_small ul:not(:animated)').animate({'left' : left_indent},500,function(){    
		});
		return false;
	});	
	//
	jQuery('.alphabet a').click(function(){
		if (jQuery(this).hasClass("notempty")) {								 
			var letter = jQuery(this).text();								
			var dest = jQuery(".term").find("h3:contains('"+letter+"')").offset().top;
			jQuery("html:not(:animated),body:not(:animated)").animate({ scrollTop: dest}, 500);
		}
		return false;
	});	
	jQuery('.slidetop').click(function(){
		jQuery("html:not(:animated),body:not(:animated)").animate({ scrollTop: 0}, 500);							   
		return false;									   	
	});
	jQuery(".scroll-to").click(function() {
			jQuery("html:not(:animated),body:not(:animated)").animate({ scrollTop: jQuery("#" + jQuery(this).attr("name")).offset().top }, 500);
			return false;
		});
	
	// selects
	jQuery(".lg-select .lg-options").before("<div class=\"lg-select-ending\"></div>");
	jQuery(".lg-select").prepend("<div class=\"lg-select-beginning\"></div>")
						.click(function() {
			var id = this.getAttribute('id').replace('_select', '');
			jQuery(this).addClass("expanded");
			jQuery("#" + id + "_select .lg-options").css({'display': 'block', 'margin-bottom': -jQuery("#" + id + "_select .lg-options").height() + 'px'});
			jQuery("#" + id + "_select .lg-option").click(function() {
					jQuery("lg-select").removeClass("expanded");
					jQuery(".lg-options", jQuery(".lg-select")).css({'display': 'none'});
					jQuery("#" + id + "_input").val(jQuery(this).attr("name"));
					jQuery("#" + id + "_select .lg-select-value").html(jQuery(this).html());
					return false;
				});
			return false;
		});
	jQuery(".lg-select").each(function() {
			var id = this.getAttribute('id').replace('_select', '');
			var first = jQuery("#" + id + "_select .lg-option.lg-selected");
			if (first.size() < 1)
				var first = jQuery("#" + id + "_select .lg-option[name=0]");
			if (first.size() < 1)
				first = jQuery("#" + id + "_select .lg-option");
			if (first.size() > 0)
			{
				jQuery("#" + id + "_select .lg-select-value").html(first.html());
				jQuery("#" + id + "_input").val(first.attr("name"));
			}
			else
			{
				jQuery("#" + id + "_select .lg-select-value").html("");
				jQuery("#" + id + "_input").val("");
			}
		}).after(jQuery("<div class=\"lg-select-after\"></div>"));
	jQuery("body").click(function() {
				jQuery(".lg-select").removeClass("expanded");
				jQuery(".lg-options", jQuery(".lg-select")).css({'display': 'none'});
			});
	jQuery(".lg-options").append("<div class=\"lg-options-ending\"><div class=\"lg-options-ending-left\"></div><div class=\"lg-options-ending-right\"></div></div>").css({'display': 'none'});
});	

jQuery(document).bind("ready resize", function() {

		// footer placement
		
	
	});