$(document).ready(function() {
	$(".img_zoom").fancybox();

	$(".img_zoom1").hover(
		function(){},
		function(){}
	);

	$(".img_zoom2").click(function(){$(".jxScroller_selected").trigger("click")});

	$(".link_year").hover(
		function(){showListYear();},
		function(){hideListYear();}
	);
	$(".list_years").hover(
		function(){showListYear();},
		function(){hideListYear();}
	);
	$(".item", $(".mmenu_items")).hover(
		function(){$(".smenu", this).animate({"opacity":"show", "height":"toggle", "width":"toggle"}, {duration:200, queue:true});},
		function(){$(".smenu", this).animate({"opacity":"hide", "height":"toggle", "width":"toggle"}, {duration:200, queue:true});}
	);
	
	scroller_init();

	plist_init();
	
	jxScroller.init();
	
	if($(".lbar_imgs").attr("class")) {
		$(".lbar_imgs").css({width:$(".photo_list_body").width()+"px"});
		$(".rbar").css({paddingTop:$(".photo_list_body").height()+20+"px"});
	}
	
	$(".submitVote").click(function(){
		$(".radioVote").each(function(){
			if($(this).attr("checked")==true) {
				$(".vote_body").html('<img src="/i/progress32.gif" align="absmiddle" width="32" height="32" /> &nbsp; Идет обработка запроса...');
				$.ajax({
					type:"GET", url:"/inquiry/?vote="+$(this).val(), dataType:"text", timeout:180*1000, cache: false,
					success:function(result) {
						$(".vote_body").html(result);
					}
				});
			}
		});
	});
	
	$(".catalog_sec_menu").hover(
		function(){
			$(this).attr("btext", $(this).text());
			$(this).text("K-FLEX "+$(this).text());
		},
		function(){
			$(this).text($(this).attr("btext"));
		}
	);
	
	$(".cat_cft_item").hover(
		function() {
			$(this).css({zIndex:1000});
			$("img", this).css({opacity:"0.5"});
			$(".cat_cft_body", this).show(200);
		},
		function() {
			$(this).css({zIndex:1});
			$("img", this).css({opacity:"1"});
			$(".cat_cft_body", this).hide(200);
		}
	);
	
	$(".header2_subitems").click(function(){
		nobj = $(this).next();
		if($(nobj).css("display")=="none") {
			$(".header2_subitems").css({backgroundImage:"url(/i/list_2.gif)"});
			$(".block").hide(0);
			$(this).css({backgroundImage:"url(/i/list_1.gif)"});
			$(nobj).show(500);
		}
	});
	
	$("td", $(".block2")).hover(function(){
		$(".header2", this).css({backgroundImage:"url(/i/list_1.gif)"});
	},
	function(){
		$(".header2", this).css({backgroundImage:"url(/i/list_2.gif)"});
	});
});

function showListYear() {
	$(".list_years").show(0);
}
function hideListYear() {
	$(".list_years").hide(0);
}

function showListYear() {
	$(".list_years").show(0);
}
function hideListYear() {
	$(".list_years").hide(0);
}

/* Scroller plugin */
function scroller_init() {
	var parentObj = $(".scroller_body").parent();
	var scWidthLeft = $(".scroller_left", parentObj).width();
	var scWidthRight = $(".scroller_right", parentObj).width();
	$(".scroller_body", parentObj).width($(parentObj).width() - (scWidthLeft + scWidthRight));
	$("span", $(".scroller_left")).bind("click", scroller_click_left)
	$("span", $(".scroller_right")).bind("click", scroller_click_right)
	$("span", $(".scroller_left")).trigger("click");
	$(".scroller").css({visibility:"visible"});
	$(".img_zoom_cnt", $(".scroller_list")).hover(function(){
		$(this).animate({opacity:"0.7"}, 200);
	},
	function(){
		$(this).animate({opacity:"1"}, 200);
	});
}
function scroller_click_left() { scroller_photo("left", this); }
function scroller_click_right() { scroller_photo("right", this); }
function scroller_photo(scroll, e) {
	var parentObj = $(e).parent().parent();
	var scrollerImgWidth = $(parentObj).attr("scrollerWidth");
	var scrollerLeft = $(".scroller_body", parentObj).scrollLeft();
	
	if (scroll=='left') {
		scrollerLeft-= parseInt(scrollerImgWidth);
		$(".scroller_body", parentObj).animate({scrollLeft: '-='+scrollerImgWidth+'px'}, 600);
	} else if (scroll=='right') {
		scrollerLeft+= parseInt(scrollerImgWidth);
		$(".scroller_body", parentObj).animate({scrollLeft: '+='+scrollerImgWidth+'px'}, 600);
	}
	if(scrollerLeft<0) scrollerLeft = 0;
	
	objScrollerLeft = $("span", $(".scroller_left", parentObj));
	objScrollerRight = $("span", $(".scroller_right", parentObj));
	if (parseInt($(".scroller_body", parentObj).width())+scrollerLeft >= parseInt($('.scroller_list_tbl', parentObj).width())) { $(objScrollerRight).css({backgroundPosition:"-17px 0"}); $(objScrollerRight).unbind("click", scroller_click_right); } 	
	else { $(objScrollerRight).css({backgroundPosition:"0 0"}); $(objScrollerRight).bind("click", scroller_click_right); }
	
	if (scrollerLeft == 0) { $(objScrollerLeft).css({backgroundPosition:"-17px -26px"}); $(objScrollerLeft).unbind("click", scroller_click_left); } 	
	else { $(objScrollerLeft).css({backgroundPosition:"0 -26px"}); $(objScrollerLeft).bind("click", scroller_click_left); }
	
	return false;
}

var jxScroller = {
	thisIe: false,
	thisIe6: false,
	thisOpera: false,
	thisApple: false,
	thisFF: false,
	height: 320,

	init: function() {
		if(/MSIE/.test(navigator.userAgent)) jxScroller.thisIe = true;
		if(/MSIE (5\.5|6)/.test(navigator.userAgent)) jxScroller.thisIe6 = true;
		if(/Opera/.test(navigator.userAgent)) jxScroller.thisOpera = true;
		if(/AppleWebKit/.test(navigator.userAgent)) jxScroller.thisApple = true;
		if(/Firefox/.test(navigator.userAgent)) jxScroller.thisFF = true;
		
		$(".jxScroller").each(function(){
			if($(this).attr("jxScrollerDir")=="v") {
				var newContent = '<table cellpadding="0" cellspacing="0" border="0">';
				$(".jxScroller_item", this).each(function(){
					newContent+= '<tr class="asdasd"><td><div class="jxScroller_item jxScroller_itemV"><div class="jxScroller_img" selected="0">'+$(this).html()+'</div></div></td></tr>';
				});
				newContent+= '</table>';
				$(".jxScroller_body", this).html(newContent);
				var posTop = 0;
				$(".jxScroller_item", this).each(function(){
					$(this).attr("jxTop", posTop);
					posTop+= $(this).outerHeight();
				});
				$(".jxScroller_selected").each(function(){
					var pObj = jxScroller.getParentObj($(this), "jxScroller_img");
					$(pObj).attr("class", "jxScroller_img_s");
					$(pObj).parent().attr("selected", "1");
					var pObj2 = jxScroller.getParentObj($(pObj), "jxScroller");
					$(".jxScroller_body", pObj2).animate({scrollTop: $(pObj).parent().attr("jxTop")-40+'px'}, 600, false, function(){ jxScroller.update($(".jxScroller_body", pObj2)); });
				});
			}
			else {
				
			}
			$(".jxScroller_body", this).css("height", jxScroller.height+"px");
		});
		$(".jxScroller_top").bind("click", jxScroller.scrollTop);
		$(".jxScroller_bottom").bind("click", jxScroller.scrollBottom);
		$(".img_zoom3").click(function(){$("body").hide()});
		$(".img_zoom").fancybox();
	},
	update: function(obj) {
		var pObj = jxScroller.getParentObj($(obj), "jxScroller");
		if($(obj).scrollTop()==0) {
			$(".jxScroller_top>span", pObj).css({backgroundPosition: "-34px 0px"});
			$(".jxScroller_top").unbind("click", jxScroller.scrollTop);
		}
		else {
			$(".jxScroller_top>span", pObj).css({backgroundPosition: "-34px -17px"});
			$(".jxScroller_top").bind("click", jxScroller.scrollTop);
		}
		
		if($(obj).scrollTop()+jxScroller.height>=$("table", pObj).height()) {
			$(".jxScroller_bottom>span", pObj).css({backgroundPosition: "-60px 0px"});
			$(".jxScroller_bottom").unbind("click", jxScroller.scrollBottom);
		}
		else {
			$(".jxScroller_bottom>span", pObj).css({backgroundPosition: "-60px -17px"});
			$(".jxScroller_bottom").bind("click", jxScroller.scrollBottom);
		}
	},
	scrollTop: function() {
		var pObj = jxScroller.getParentObj($(this), "jxScroller");
		var obj = $("div[selected*='1']", pObj);
		$(obj).attr("selected", "0");
		nObj = $(".jxScroller_item", $(obj).parent().parent().prev());
		$(nObj).attr("selected", "1");
		$(".jxScroller_body", pObj).animate({scrollTop: $(nObj).attr("jxTop")+'px'}, 600, false, function(){ jxScroller.update($(".jxScroller_body", pObj)); });
	},
	scrollBottom: function() {
		var pObj = jxScroller.getParentObj($(this), "jxScroller");
		var obj = $("div[selected*='1']", pObj);
		$(obj).attr("selected", "0");
		nObj = $(".jxScroller_item", $(obj).parent().parent().next());
		$(nObj).attr("selected", "1");
		$(".jxScroller_body", pObj).animate({scrollTop: $(nObj).attr("jxTop")+'px'}, 600, false, function(){ jxScroller.update($(".jxScroller_body", pObj)); });
	},
	imgSelectedV: function() {
		var pObj = jxScroller.getParentObj($(this), "jxScroller");
		$(".jxScroller_img_s", pObj).attr("class", "jxScroller_img");
		$(".jxScroller_img", this).attr("class", "jxScroller_img_s");
		$(".jxScroller_body", pObj).animate({scrollTop: $(this).attr("jxTop")-40+'px'}, 600);
	},
	getParentObj: function(objName, className) {
		var pObj = objName.parent();
		if($(pObj).attr("class")==className) { return pObj; }
		else { return jxScroller.getParentObj(pObj, className); }
	}
};
/* End scroller plugin */

/* Photo list plugin */
var plist_timer = "";
function plist_init() {
	$(".item:first", $(".photo_list_body")).fadeIn(200);
	$(".num:first", $(".photo_list_body")).attr("class", "num_s");
	$(".next", $(".photo_list_body")).click(plist_next);
	$(".prev", $(".photo_list_body")).click(plist_prev);
	$(".num", $(".photo_list_body")).live("click", plist_num_click);
	plist_update_block();
	if($(".item", $(".photo_list_body")).length>1) plist_timer = setInterval(plist_next, plist_interval);
}
function plist_update_block() {
	$(".photo_list_body").css({height:$(".item:visible", $(".photo_list_body")).height()+"px", width:$(".item:visible", $(".photo_list_body")).width()+"px"});
}
function plist_next() {
	if($(".item:last", $(".photo_list_body")).css("display")=="inline") {
		nobj = $(".item:first", $(".photo_list_body"));
	} else {
		nobj = $(".item:visible", $(".photo_list_body")).next();
	}
	$(".item", $(".photo_list_body")).fadeOut(190);
	nobj.fadeIn(200, plist_update_block);
	$("a[class*='num']", $(".photo_list_body")).attr("class", "num");
	$("a[num='"+nobj.attr("num")+"']", $(".photo_list_body")).attr("class", "num_s");
}
function plist_prev() {
	nobj = $(".item:visible", $(".photo_list_body")).prev();
	$(".item", $(".photo_list_body")).fadeOut(190);
	nobj.fadeIn(200, plist_update_block);
	$("a[class*='num']", $(".photo_list_body")).attr("class", "num");
	$("a[num='"+nobj.attr("num")+"']", $(".photo_list_body")).attr("class", "num_s");
}
function plist_num_click() {
	clearInterval(plist_timer);
	$(".item", $(".photo_list_body")).fadeOut(190);
	$(".item[num='"+$(this).attr("num")+"']", $(".photo_list_body")).fadeIn(200, plist_update_block);
	$("a[class*='num']", $(".photo_list_body")).attr("class", "num");
	$("a[num='"+$(this).attr("num")+"']", $(".photo_list_body")).attr("class", "num_s");
}
/* End photo list plugin */