//
function fixPNG(element){
	if (/MSIE.+Win/.test(navigator.userAgent)){
	var src;
	if (element.tagName=='IMG'){
		if (/\.png$/.test(element.src)){
			src = element.src;
			element.src = "/img/0.gif";
		}
	}else{
		src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
		if (src){
			src = src[1];
			element.runtimeStyle.backgroundImage="none";
		}
	}
	if (src){ element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')"; }}
}
function documentFixPNG(){
	var blank = new Image();
	blank.src = "/img/0.gif";
	if (/MSIE.+Win/.test(navigator.userAgent)){
		var images = new Array();
		images = document.getElementsByTagName("IMG");
		for(var i=0; i < images.length; i++){
			if (/\.png$/.test(images[i].src)){
				var src = images[i].src;
				images[i].src = blank.src;
				images[i].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
			}
		}
	}
}

$(function(){
	//$("#form001").submit(function() { send_login(); });
	$("#form001 #token").keyup(function(e){ if(e.keyCode==13){ send_login(); }});
});

var list_tree = new Array();
var list_tree_p = new Array();
var list_tree_pp = new Array();

function open_ls(id){
	if(document.getElementById("pid_" + id)){
		
		if(list_tree_pp[id]==1){
			$("#pid_" + id).hide();
			list_tree_pp[id] = 0;
			return;
		}
		
		var pid = id;
		var arrp = Array();
		
		do{
			arrp[arrp.length] = pid;
			pid = list_tree[pid];
		}while(pid!=0)
		
		var s = 0;
		for(var n=0; n < list_tree_p.length; n++){
			s = 0;
			for(var k=0; k < arrp.length; k++){
				//alert(arrp[k] + " == "+ list_tree_p[n]);
				if(arrp[k]==list_tree_p[n]){
					s=1; break;
				}
			}
			//alert(list_tree_p[n] + " " + s);
			if(s == 1)
				list_tree_pp[list_tree_p[n]] = 1;
			else
				list_tree_pp[list_tree_p[n]] = 0;
		}		
		for(var n=0; n < list_tree_p.length; n++){
			if(list_tree_pp[list_tree_p[n]]==1) $("#pid_" + list_tree_p[n]).show();
			else  $("#pid_" + list_tree_p[n]).hide();
		}
		$('.scrolling').jScrollPane({showArrows:true,scrollbarWidth:2,dragMaxHeight:43,ReinitialiseOnImageLoad:true});
	}
}
function DownloadFile(id){
	var divElement = document.createElement('DIV');
	divElement.innerHTML = '<iframe style="display:none" src="download.php?file_id=' + id + '"></iframe>';
	//divElement.innerHTML = '<iframe width="200" height="100" src="download.php?file_id=' + id + '"></iframe>';
	document.body.appendChild(divElement);
	//alert(id);
}
$(function(){
	$("#form001 #login").val("");
	$("#form001 #passw").val("");
});

MyMenu = function(){
	if (document.getElementById("topmenu")==null) return false;
	var doc_w = $(window).width();
	var obj = document.getElementById('topmenu');
	var jobj = "#topmenu";
	var position = getPosition(obj);
	var margin_top = 15;
	var pid = new Array;
	var timeout = false;
	var curren_id = false;
	
	function next_li(id){
		if(id!=0){
			var ul_pos = getPosition(document.getElementById("menu_ul_" + pid[id]));
			var li_pos = getPosition(document.getElementById("menu_li_" + id));
			var ul_w = doc_w - li_pos.left;

			var ul = document.getElementById("menu_ul_" + id);
			if (ul!=null){
				ul.style.left = "+" + li_pos.left + "px";
				ul.style.width = ul_w + "px";
			};
		}
		$("#menu_ul_" + id + " li").each(function(){
			if(arid = this.id.match(/menu_li_(\d+)/i)){
				pid[arid[1]] = id;
				next_li(arid[1]);
			}
		});
	}
	function init(){
		next_li(0);
		$(jobj + " li").mouseover(function(){
			over(this);
		}).mouseout(function(){
			out(this);
		}).click(function(){
			$(jobj + " li").each(function(){
				if(this.className == "activ"){ $(this).attr('class',''); }
			});
			if(id = this.id.match(/menu_li_(\d+)/)){
				var next = id[1];
				while(next != 0){
					$(jobj + " #menu_li_" + next).attr('class','activ');
					next = pid[next];
				}
			}
		});
	}
	init();
	
	function over(li){
		if(timeout){
			clearTimeout(timeout);
		}
		if(arid = li.id.match(/menu_li_(\d+)/)){
			id = arid[1];
			if(id === curren_id) return true;
			curren_id = id;
			$(jobj + " ul").each(function(){
				if(this.id != "menu_ul_0"){
					this.style.visibility = "hidden";
				}
			});
			$(jobj + " #menu_ul_" + id).css('visibility','visible');
			var next = id;
			while(next != 0){
				$(jobj + " #menu_ul_" + next).css('visibility','visible');
				next = pid[next];
			}
		}
	}

	function out(li){
		timeout = setTimeout(function(){
			outend(li);
		},600);
	}

	function outend(li){
		$(jobj + " ul").each(function(){
			if(this.id != "menu_ul_0"){
				this.style.visibility = "hidden";
			}
		});
		$(jobj + " li").each(function(){
			if(this.className == "activ"){
				if(id = this.id.match(/menu_li_(\d+)/)){
					ul = pid[id[1]];
					if(ul > 0){
						$(jobj + " #menu_ul_" + ul).css('visibility','visible');
					}
				}
			}
		});
	}
}

var BigImagesGallery = new Array();
GalleryImg = function(img_array, but_next_id, but_pre_id, img_id, prevP, nextP, curIn){
	//var images = img_array;
	//var curent_img = images[0];
	var curent_id = 0;
	var but_next = document.getElementById(but_next_id);
	var but_pre  = document.getElementById(but_pre_id);
	var but_img_in = document.getElementById('image_in');
	var img = document.getElementById(img_id);
	var prevPage = "";
	var nextPage = "";
	var BigImages = new Array();
	
	if(typeof prevP != 'undefined') prevPage = prevP;
	if(typeof nextP != 'undefined') nextPage = nextP;
	if(typeof curIn != 'undefined') curent_id = curIn;
	
	if(but_next == null || but_pre == null) return true;
	
	var images = new Array();
	for(var i=0; i < img_array.length; i++){
		images[i] = new Image();
		images[i].src = img_array[i];
	}
	
	var BigOn = false;
	for(var t=0; t<BigImagesGallery.length; t++){
		if(BigImagesGallery[t]!=""){
			BigOn = true;
			break;
		}
	}
	if(BigOn == true && but_img_in !== null){
		but_img_in.style.display = 'block';
		
		$(but_img_in).bind('click', function(){ OpenLightBox(curent_id) });
		$(but_img_in).bind('mouseover', function(){ but_img_in.className = "over" });
		$(but_img_in).bind('mouseout', function(){ but_img_in.className = "" });
		
		$(img).bind('click', function(){ OpenLightBox(curent_id) });
		$(img).bind('mouseover', function(){ but_img_in.className = "over" });
		$(img).bind('mouseout', function(){ but_img_in.className = "" });
		img.style.cursor = 'pointer';
		
	}
	
	ShowHideMenu();
	
	if(but_next && but_pre && img){
		$(but_next).click(function(){
			curent_id++;
			if(curent_id >= images.length){
				if(nextPage != ""){
					window.location = nextPage;
					return;
				}
				curent_id = 0;
			}
			next_img();
		});
		$(but_pre).click(function(){
			curent_id--;
			if(curent_id < 0){
				if(prevPage != ""){
					window.location = prevPage + "?last";
					return;
				}
				curent_id = images.length - 1;
			}
			next_img();
		});
	}
	
	function ShowHideMenu(){
		if(typeof images[curent_id+1] == 'undefined' && nextPage == "")
			but_next.style.visibility = "hidden";
		else
			but_next.style.visibility = "visible";
		
		if(typeof images[curent_id-1] == 'undefined' && prevPage == "")
			but_pre.style.visibility = "hidden";
		else
			but_pre.style.visibility = "visible";
	}
	
	function next_img(){
		$(img).animate({opacity: 0}, 500, 'linear', function(){
			//img.style.visibility = "hidden"; //visible 
			img.src = images[curent_id].src;
			$(img).animate({opacity: 1}, 500 );
			ShowHideMenu();
		});
	}
}


HomeGallery = function(img_array){
	var images = img_array;
	var curent_img = images[0];
	var curent_id = 0;
	var old_id = false;
	var cont = 'html_gallery_main';
	var timeout = false;
	
	if(document.getElementById(cont) != null){
		$("#"+cont + " #next_image").click(function(){
			next_img(+1);
			settime(7000);
		}).hover(
      	function () {
        	$(this).css({'background-image':'url(/img/home_next_image_on.gif)'});
      	}, 
      	function () {
        	$(this).css({'background-image':'url(/img/home_next_image.gif)'});
      	});
		$("#"+cont + " #pre_image").click(function(){
			next_img(-1);
			settime(7000);
		}).hover(
      	function () {
        	$(this).css({'background-image':'url(/img/home_pre_image_on.gif)'});
      	}, 
      	function () {
        	$(this).css({'background-image':'url(/img/home_pre_image.gif)'});
      	});
		settime(5000);
	}
	
	function settime(time){
		if(timeout) window.clearTimeout(timeout);
		timeout = setTimeout(function(){ next_img(+1); },time);
	}
	
	function next_img(step){
		old_id = curent_id;
		curent_id += step;
		if(curent_id >= images.length) curent_id = 0;
		if(curent_id < 0) curent_id = images.length - 1;
		
		curent_img = images[curent_id];
		if(typeof curent_img[3] == 'undefined'){
			curent_img[3] = new Image();
			curent_img[3].src = curent_img[0];
		}
		var left = '970px';
		if(step===-1) left = '-970px';
		var img = '<a href="'+curent_img[1]+'" id="homeImage_'+curent_id+'"><img src="'+curent_img[0]+'" alt="'+curent_img[2]+'" style="left:'+left+'"></a>';
		
		if(step===-1){
			$("#homeImage_"+old_id).after(img);
			$("#homeImage_"+old_id+" img").animate({'left': '970px'}, 500, 'linear', function(){ $("#homeImage_"+old_id).remove(); settime(5000); });
			$("#homeImage_"+curent_id+" img").animate({'left': '0px'}, 500);
		}else{
			$("#homeImage_"+old_id).after(img);
			$("#homeImage_"+old_id+" img").animate({'left': '-970px'}, 500, 'linear', function(){ $("#homeImage_"+old_id).remove(); settime(5000); });
			$("#homeImage_"+curent_id+" img").animate({'left': '0px'}, 500);
		}
	}
}
function preload_images (urls){
	var img = new Array();
	for(var i=0; i<urls.length; i++){
		img[img.length] = new Image();
		img[img.length-1].src = urls[i];
	}
	return img;
}
function getPosition(elem){
	var w = elem.offsetWidth;
   	var h = elem.offsetHeight;	
   	var l = 0;
   	var t = 0;	
   	while (elem){   
		l += elem.offsetLeft;
       	t += elem.offsetTop;
		elem = elem.offsetParent;  }
	return {"left":l, "top":t, "width": w, "height":h};
}
ShowPreImg = function(images_in,ul_id,conteiner_id){
	var images = images_in;
	var conteiner = document.getElementById(conteiner_id);
	var ul = document.getElementById(ul_id);
	var pos_ul;
	var con_w = 71;
	var con_h = 51;
	if (conteiner!=null && ul!=null){
		preload_images(images);
		conteiner.style.width = con_w;
		conteiner.style.height = con_h;
		$("#" + ul_id + " a").mouseover(function(){
			In(this);
		}).mouseout(function(){
			Out(this);
		});
		pos_ul = getPosition(ul);
		//pos_con= getPosition(conteiner);
	}
	function In(li){
		if(arid = li.id.match(/goods_menu_li_(\d+)/)){
			var id = arid[1];
			var pos = getPosition(li);
			if(images[id]!=""){
				conteiner.style.backgroundImage = "url(" + images[id] + ")";
				if((pos.top - con_h) < (pos_ul.top))
				conteiner.style.top =  (pos.top + pos.height + 2) + "px";
				else
				conteiner.style.top =  (pos.top - con_h) + "px";
				conteiner.style.left = (pos_ul.left + pos_ul.width - con_w) + "px";
				$(conteiner).show();
			}
		}
	}
	function Out(li){
		$(conteiner).hide();
	}
}

//var MyLBox_gallery;
function OpenLightBox(cur_img){
	var Bigs = new Array();
	var curI = 0;
	for(var i = 0; i < BigImagesGallery.length; i++){
		if(BigImagesGallery[i] != ""){ 
			Bigs[Bigs.length] = BigImagesGallery[i];
			if(i == cur_img) curI = Bigs.length - 1;
		}
	}
	if(Bigs.length > 0) var MyLBox_gallery = new LightBox(Bigs,1,'#110e15',curI);
	//MyLBox_gallery.start();
}
//var MyLbox;
var SpecImages = new Array();
function loadSpec(id,lg) {	
	var MyLBox = new LightBox(SpecImages,1);
}

function LightBox(images_in, auto_start, bgrnd, start_img){	

	if(typeof bgrnd == 'undefined') bgrnd = '#FFF';
	if(bgrnd === false) bgrnd = '#FFF';
	
	this.prototype = LightBox;
	var initial = typeof this.prototype.constructed === 'undefined' || this.prototype.constructed === false;
	
	var img = new Array();
	img = images_in;
	var curent_img = 0;
	var image;
	var BgColor = bgrnd;
	
	if(typeof start_img !== 'undefined'){
		if(start_img >= 0 && start_img < img.length){
			curent_img = start_img;
		}
	}
	
	var images = {
		loading : "/img/gallery_img/loading.gif",
		blank	: "/img/gallery_img/blank0.gif",
		prev	: "/img/gallery_img/prev_bt.gif",
		next	: "/img/gallery_img/next_bt.gif",
		Print	: "/img/gallery_img/print.gif",
		close	: "/img/gallery_img/close.gif",
		button	: "/img/gallery_img/button.gif"
	};
	
	var conteiner_id = "LightBox_Block";
	var fon_id = "LightBoxBlockFon";
	var inner_block_id = "LightBoxBlockInner";
	var image_cont_id = "LightBoxBlockImg";
	var bt_next_id = "LightBoxBlockNextBt";
	var bt_prev_id = "LightBoxBlockPrevBt";
	var bt_close_id = "LightBoxBlockCloseBt";
	var bt_print_id = "LightBoxBlockPrintBt";
	var min_h = 200;
	var min_w = 300;
	var border = 0;
	var AInterval = 30;
	var Pos = 30;
	var IntervalId = false;
	var Keyboard_Enabled = false;
	var Win_h = $(window).height();
	var Win_w = $(window).width();
	var Doc_h = $(document).height();
	var Doc_w = $(document).width();
	var Doc_Scrol = 0; //$(document).scrollTop();

	if(initial){
		preload_images(images);
		$("body").append('<div id="'+ conteiner_id +'" class="LightBoxBlock"><div id="' + fon_id + '"></div><div id="' + inner_block_id + '"><div id="' + bt_prev_id + '" title="Предыдущая"></div><div id="' + image_cont_id + '"></div><div id="' + bt_next_id + '" title="Следующая"></div><div id="' + bt_print_id + '"><img src="'+ images.Print +'" alt="" border="0">Распечатать</div><div id="' + bt_close_id + '">Закрыть (Esc)<img src="'+ images.close +'" alt="" border="0"></div></div></div>');
		this.prototype.constructed = true;
	}
	
	var menu_swf = document.getElementById('goods_menu_swf');
	var conteiner = document.getElementById(conteiner_id);
	var fon = document.getElementById(fon_id);
	var inner_block = document.getElementById(inner_block_id);
	var imagecont = document.getElementById(image_cont_id);
	var bt_next = document.getElementById(bt_next_id);
	var bt_prev = document.getElementById(bt_prev_id);
	var bt_close = document.getElementById(bt_close_id);
	var bt_print = document.getElementById(bt_print_id);
	
	inner_block.style.backgroundColor = BgColor;
	
	if(initial){
		$(inner_block).click(function(){
			return false;
		});
		$(conteiner).click(function(){
			Close();
		});
		$(bt_close).click(function(){
			Close();
		});
		$(bt_next).mouseover(function(){ this.style.backgroundPosition = "right bottom"; }).mouseout(function(){ this.style.backgroundPosition = "right top"; });
		$(bt_prev).mouseover(function(){ this.style.backgroundPosition = "left bottom"; }).mouseout(function(){ this.style.backgroundPosition = "left top"; });
	}else{
		$(bt_print).unbind('click');	
		$(bt_next).unbind('click');
		$(bt_prev).unbind('click');		
	}
		$(bt_print).bind('click', function(){ Print(); });
		$(bt_next).bind('click', function(){ nextImage(1); });
		$(bt_prev).bind('click', function(){ nextImage(-1); });
	
	function Print(){
		try{
			var MyWin = OpenWin("/" + img[curent_img],"PrintImage", image.width, image.height);
			MyWin.print();
		}catch(e){
			//
		}
	}
	
	function Start(){
		if(img.length < 1 || (img.length == 1 && img[0]=="")) return;
		
		if(menu_swf !== null){
			menu_swf.style.visibility = "hidden";
		}
		
		var Win_h = $(window).height();
		var Win_w = $(window).width();
		
		
		inner_block.style.width = min_w  + "px";
		inner_block.style.height = min_h  + "px";
		inner_block.style.top = Math.round(Doc_Scrol + ((Win_h - min_h)/2)) + "px";
		inner_block.style.left = Math.round((Win_w - min_w)/2) + "px";
		inner_block.style.background = BgColor + " url(" + images.loading + ") center center no-repeat";
		imagecont.style.display = "none";
		inner_block.style.visibility = "hidden";
		$(imagecont).html("");
		
		conteiner.style.height = Doc_h  + "px";
		conteiner.style.display = "block";
		//$(conteiner).show();
		
		nextImage(0);
	}
	
	function nextImage(step){
		
		curent_img += step;
		if(curent_img >= img.length || curent_img < 0){
			Close();
			return;
		}
		imagecont.style.display = "none";
		inner_block.style.visibility = "visible";
		
		inner_block.style.background = BgColor + " url(" + images.loading + ") center center no-repeat";
		var preloader = new Image();
		preloader.onload = function() {
			$(conteiner).stop(true, true);
			inner_block.style.background = BgColor;
			$(imagecont).html('<img src="' + preloader.src + '" alt="" border="0">');
			image = preloader;
			resizeBoxes();
		};	
		preloader.src = img[curent_img];
		
	}
	
	function Close(){
		curent_img = 0;
		conteiner.style.display = "none";
		Keyboard_Disable();
		if(menu_swf !== null){
			menu_swf.style.visibility = "visible";
		}
		return true;
	}
	
	function resizeBoxes(){
		
		if( typeof image.width === 'undefined' || typeof image.height === 'undefined' ) return;
		
		var img_w = image.width;
		var img_h = image.height;
		
		bt_next.style.display = "none";
		bt_prev.style.display = "none";
		
		var Doc_Scrol = $(document).scrollTop();
		
		var end_t = 0;
		var end_l = 0;
		var end_w = img_w + border*2;
		var end_h = img_h + border*2;
		
		if(Win_h <= (img_h + border*2)) end_t = Doc_Scrol;
		else{
			end_t = Math.round(Doc_Scrol + ((Win_h - img_h - border*2)/2));
		}
		
		
		if(Win_w <= (img_w + border*2)) end_l = 0;
		else{
			end_l = Math.round((Win_w - img_w - border*2)/2);
		}
		
		var start = getPosition(inner_block);		
		
		IntervalId = setInterval(function(){
			if(start.height < end_h){
				start.height += Pos;
				start.top += -(Pos/2);
				if(start.height > end_h) start.height = end_h;
				if(start.top < end_t) start.top = end_t;
				inner_block.style.top = start.top + "px";
				inner_block.style.height = start.height + "px";
			}else
			if(start.height > end_h){
				start.height -= Pos;
				start.top += (Pos/2);
				if(start.height < end_h) start.height = end_h;
				if(start.top > end_t) start.top = end_t;
				inner_block.style.top = start.top + "px";
				inner_block.style.height = start.height + "px";
			}else			
			if(start.width < end_w){
				start.width += Pos;
				start.left += -(Pos/2);
				if(start.width > end_w) start.width = end_w;
				if(start.left < end_l) start.left = end_l;
				inner_block.style.left = start.left + "px";
				inner_block.style.width = start.width + "px";
			}else
			if(start.width > end_w){
				start.width -= Pos;
				start.left += (Pos/2);
				if(start.width < end_w) start.width = end_w;
				if(start.left > end_l) start.left = end_l;
				inner_block.style.left = start.left + "px";
				inner_block.style.width = start.width + "px";
			}			
			if(start.height == end_h && start.width == end_w){
				clearInterval(IntervalId);
				bt_next.style.display = "block";
				bt_prev.style.display = "block";
				imagecont.style.display = "block";
				var btTop = Math.round((end_h - 32)/2);
				if(btTop < 0) btTop = 0;
				bt_next.style.top = btTop + "px";
				bt_prev.style.top = btTop + "px";
				Keyboard_Enable();
			}
		},AInterval);
	}
	
	function Keyboard_Enable() {
		if(Keyboard_Enabled === false){
			$(document).keydown(function(objEvent) {
				Keyboard_Action(objEvent);
			});
			Keyboard_Enabled = true;
		}
		if((curent_img + 1)< img.length){
			bt_next.style.display = "block";
			bt_next.style.background = "url(" + images.button + ") right top no-repeat";
		}else{
			bt_next.style.display = "none";
			bt_next.style.background = "none";
		}
		
		if((curent_img - 1) >= 0){
			bt_prev.style.display = "block";
			bt_prev.style.background = "url(" + images.button + ") left top no-repeat";
		}else{
			bt_prev.style.display = "none";
			bt_prev.style.background = "none";
		}
		
		bt_close.style.display = "block";
		//bt_close.style.background = "url(" + images.close + ") right center no-repeat";
		
		bt_print.style.display = "block";
		//bt_print.style.background = "url(" + images.Print + ") center center no-repeat";
		conteiner.focus();
	}

	function Keyboard_Disable() {
		bt_prev.style.display = "none";
		bt_prev.style.background = "none";
		bt_next.style.display = "none";
		bt_next.style.background = "none";
		bt_close.style.display = "none";
		bt_close.style.background = "none";
		bt_print.style.display = "none";
		bt_print.style.background = "none";
		
		if(Keyboard_Enabled === true){
			$(document).unbind();
			Keyboard_Enabled = false;
		}
	}

	function Keyboard_Action( objEvent ) {
		// Prepare
		objEvent = objEvent || window.event;
		
		var keycode = objEvent.keyCode;
		var escapeKey = objEvent.DOM_VK_ESCAPE /* moz */ || 27;

		// Close?
		if ( keycode === escapeKey ) {	return Close();		}

		// Prev?
		if ( keycode === 37 ){
			return nextImage(-1);
		}

		// Next?
		if ( keycode === 39 ){
			return nextImage(1);
		}

		// Unknown
		return true;
	}
	
	this.nextImage = function(s){ nextImage(s); };
	this.start = function(){ Start(); };
	if(typeof auto_start != 'undefined') Start();
}
function OpenWin(url,name,w,h,l,t){
	if(typeof l == 'undefined' || typeof t == 'undefined'){
		var l2 = screen.width/2;
		var l = l2 - w / 2;
		var t2 = screen.height/2;
		var t = t2 - h / 2;
	}
	return myWin = open(url,name,'width='+w+',height='+h+',left='+l+',top='+t+',status=no,toolbar=no,menubar=no,scrollbars=yes,location=no,resizable=yes');	
}

$(function(){
	MyMenu();
	documentFixPNG();
});
