var menuflag = 0;
var subnav_height=0;
$(document).ready(function () {
	if ($("#subnav").length > 0 && $(".context_menu").length > 0) {
		var mw = $("#subnav").width();
		$("div.context_menu").wrapInner('<div style="width:160px; margin-top:8px"></div>');
		switchMenuStyle(mw);
		if ($("#mainright").length > 0) {
			$(document).bind('mousemove',function(e){
			var mx = e.pageX;
			var my = e.pageY;
			var d = (menuflag == 0) ? 0 : 162;
			ppos = $("#mainright").position();
			mx = mx-ppos.left + d;
			my = my-ppos.top;
//			alert(mx)
			if (mx > 0)
                	{
                		if (menuflag == 0) {
		               		menuflag = 1;
					$("div.context_menu").stop();
					$('div.subcontainer').css({display:'block'});
					$("div.context_menu").animate({width: "160px"},500,function(){overflow:"visible"; });
				}
			} else {
//			alert(mx)
				mw = $("#subnav").width();
				if (menuflag == 1 && mw < 161) {
				$("div.context_menu").stop();
				$("div.context_menu").css({overflow:'visible'}).animate({width: "1px"},300,function(){menuflag = 0; $('div.subcontainer').css({display:'none'});});
				}
			}

	});
	}

	/*

	if ($("#mainright").length > 0) {
		    $("#mainright").hover(
		      function () {
			if (mw < 160 && menuflag == 0) {
				$("#mainright").stop();
				$("div.context_menu").animate({opacity: 1},100);
				menuflag = 1;
			}
		      },
		      function () {
			if (mw < 160) {
				window.setTimeout(function(){
					if (menuflag != 0) {
					$("div.context_menu").animate({opacity: 0},800);
					$("#mainright").stop();
					menuflag = 0;
					}
				},1000);
			}
		      }
		    );
		    $("div.context_menu").hover(
		      function () {
			$("#mainright").stop();
			menuflag = 1;
		      },
		      function () {
			menuflag = 0;
			if (mw < 160 && menuflag == 0) {
				$("div.context_menu").animate({opacity: 0},1000);
			}
		      }
		    );

	}
	*/
	}

});

function switchMenuStyle(mw){
	if (mw < 161) {
		$("div.context_menu").css({overflow:'visible'}).animate({width: "1px"},300,function(){menuflag = 0; $('div.subcontainer').css({display:'none'});});
		if ($('div.context_menu').height() > 300) {
			if (subnav_height == 0) {
                           $("#subnav").css({height:$('div.context_menu').height()});
                        }
		} else {
			if (subnav_height == 0) {
				$("#subnav").css({height:"300px"});
				$("div.context_menu").css({height:"300px"});
			}
		}
			subnav_height = 1;
	} else {
		menuflag = 1;
		$('div.subcontainer').css({display:'block'});
		$("div.context_menu").animate({width: "160px"},800,
		function(){ overflow:"visible";
		if ($('div.context_menu').height() > 300) {
			if (subnav_height == 0) {
                           $("#subnav").css({height:$('div.context_menu').height()});
                        }
		} else {
			if (subnav_height == 0) {
				$("#subnav").css({height:"300px"});
				$("div.context_menu").css({height:"300px"});
			}
		}
			subnav_height = 1;

                });
	}
}

$(window).resize(function(e) {
	if ($("#subnav").length > 0 && $("div.context_menu").length > 0) {
		$("div.context_menu").stop();
		var mw = $("#subnav").width();
		switchMenuStyle(mw);
	}
});




(function($) { //create closure
//
$.fn.itemsnavigator = function(options){
	var defaults = {
		ulwidth: 480,
		ulheight: 52,
		prev: '/bitrix/templates/.default/i/items_prev.gif',
		next: '/bitrix/templates/.default/i/items_next.gif',
		prevwidth: 17,
		nextwidth: 17
	};
	var opts = $.extend(defaults, options);
	var ul = $(this);
	var div = $("<div></div>");	
	div.css({
		position: 'relative',
		height: defaults.ulheight,
		width: defaults.ulwidth,
		cursor: 'pointer',
		overflow: 'hidden'
		});

	ul.wrap(div);


	var sumwidth = 0;
	$("li",ul).each(function(i){
		var li = $(this);
		li.css({position:'absolute',border:'solid 1px #e1e1e1', left: (sumwidth), top: 0});
		if (sumwidth == 0) li.css({border:'solid 1px #E88B1A'});
		sumwidth += li.width();
        	li.mouseover(function(){
              		li.css({border:'solid 1px #E88B1A'});
		});
		li.mouseout(function(){
			if (!li.hasClass('curr')) li.css({border:'solid 1px #e1e1e1'});
		});
		li.click(function(){
			$("li.curr",ul).css({border:'solid 1px #e1e1e1'}).removeClass('curr');
              		li.css({border:'solid 1px #E88B1A'}).addClass('curr');
			$("#bigimg").html('<img src="'+$("img",li).attr("src")+'" alt="" />');
		});
	});


	if (sumwidth <= defaults.ulwidth) {
		ul.css({position:'absolute', left: 0, top: 0 });
	} else {
		ul.css({position:'absolute', left: defaults.prevwidth, top: 0 });
		var prev = $('<img src="'+defaults.prev+'" alt="&laquo;" />').css({
			position:'absolute',
			zIndex: 999,
			left: 0
			}).insertBefore(ul);
		var next = $('<img src="'+defaults.next+'" alt="&raquo;" />').css({
			position:'absolute',
			zIndex: 999,
			right: 0
			}).insertAfter(ul);

		next.mouseover(function(){
			var pos = ul.position();
			if (Math.abs(pos.left) < sumwidth - defaults.ulwidth) {
				ul.animate({left: - (sumwidth - defaults.ulwidth + defaults.prevwidth) }, 3000);
			}
		});
		next.mouseout(function(){
			ul.stop();
		});


		prev.mouseover(function(){
			var pos = ul.position();
			if (Math.abs(pos.left) > 0) {
				ul.animate({left: defaults.prevwidth }, 3000);
			}
		});
		prev.mouseout(function(){
			ul.stop();
		});

	}
}
//end of closure
})(jQuery);





(function($) { //create closure
// gallery
$.fn.audigallery = function(options){
	var defaults = {
	};
	var opts = $.extend(defaults, options);

	var ul = $(this);
	var bp = $('#galbigimg');
	if (bp.length == 0) {
		return false;
	}
	bp.css({position:"relative", background: "url("+$('img',bp).attr('src')+") no-repeat 50% 50%"});
	var cnt = 0;
	$("a",ul).each(function(i){
		var aa = $(this);
		aa.click(function(){
			$("a",ul).removeClass('current');
			aa.addClass('current');
			var oi = $('img', bp);
			var ni = new Image();
			ni.src = aa.attr('href');
			oi.css({position:'absolute'}).animate({left: -500 }, 1000);
			bp.html('<img src="'+ni.src+'" width="480" alt="" />');

			//$('#galhq').html('<li><a href="'+ni.src.replace(/(\.(?:jpg|png))/, '_1024x819$1')+'" target="bigimg">1024 x 768</a></li><li><a href="'+ni.src.replace(/(\.(?:jpg|png))/, '_1024$1')+'" target="bigimg">1280 x 1024</a></li>');
			$('#galhq').html('<li><a href="'+ni.src.replace(/4\d\dx3\d\d/, '1024x819')+'" target="bigimg">1024 x 768</a></li><li><a href="'+ni.src.replace(/4\d\dx3\d\d/, '1280x1024')+'" target="bigimg">1280 x 1024</a></li>');
			return false;
		});


		if (cnt == 0) {
			var aa = $(this);
			aa.addClass('current');
			var oi = $('img', bp);
			var ni = new Image();
			ni.src = aa.attr('href');
			oi.css({position:'absolute'}).animate({left: -500 }, 1000);
			bp.html('<img src="'+ni.src+'" width="480" alt="" />');
			//$('#galhq').html('<li><a href="'+ni.src.replace(/(\.(?:jpg|png))/, '_1024x819$1')+'" target="bigimg">1024 x 768</a></li><li><a href="'+ni.src.replace(/(\.(?:jpg|png))/, '_1024$1')+'" target="bigimg">1280 x 1024</a></li>');
			$('#galhq').html('<li><a href="'+ni.src.replace(/4\d\dx3\d\d/, '1024x819')+'" target="bigimg">1024 x 768</a></li><li><a href="'+ni.src.replace(/4\d\dx3\d\d/, '1280x1024')+'" target="bigimg">1280 x 1024</a></li>');
		}
		cnt++;

	});
	}
//end of closure
})(jQuery);













function showItemInfo(url, w, h) {
	if (!url) return;
	if (!w || !h) {
		w = 1000;
		h = 600;
	}
	var left, top;
	left = screen.width / 2 - (w / 2);
	top = screen.height / 3 - (h / 3);
	left = (left > 0)? left : 0;
	top = (top > 0)? top : 0;
	window.open(url, "item", "width=" + w + ", height=" + h + ", scrollbars=y, status=0, left=" + left + ", top=" + top );
	//var wo = window.open(url, "item", "width=" + w + ", height=" + h + ", scrollbars=0, status=0, left=" + left + ", top=" + top );
	//wo.document.open();
	//wo.window.focus();
}
