var currentHash="";
var currentTitle=[];
var defaultTitle=document.title;
var needle=($.browser.msie)?"http://"+location.hostname+location.pathname.replace("index.php","")+"images":"images";
var _sidebar_border="<div class='sidebar_module_border'><img src='files/images.v3/space.gif' width='1' height='1'></div>";
var _popup_class=($.browser.msie)?"lightbox":"nyroModal";
var _cache_gallery;
var _cache_product_group=[];
var _cache_product_list=[];
var _cache_product_menu=[];

/***************************************
 CONTENT EFFECT
 ***************************************/

// permananet content effect
$(document).ready(function(){
	// table auto resize
	$(window).resize(setTableHeight);

	// nyroModel settings
	$.fn.nyroModal.settings.width=300;
	$.fn.nyroModal.settings.height=150;
	$.fn.nyroModal.settings.minWidth=50;
	$.fn.nyroModal.settings.minHeight=50;

	// create main menu
	for(i=0;i<menu.length;i++){
		$("p#pageMenu").append(menu[i]==''?"<span class='menu_split'>&nbsp;</span> ":"<span class='menu' id='"+menu[i]+"'>"+zstr.menu[menu[i]]+"</span> ");
	}
	
	// sidebar content
	$("div.sidebar_module").filter(".load").each(function(){
		$(this).load("content/_global/sidebar."+this.title+".php");
		this.title="";
	});

	// load content
	load((location.hash)?location.hash.substr(1):"/");

	ID5=setInterval(onHashChange,200);
});

// hash change
function onHashChange(){
	if($.browser.msie){
		var f=$("#history").get(0).src;
		if(f=="about:blank") return;
		f="#"+f.substr(f.indexOf("~")+3);
	 	if(currentHash!=f){
			location.hash=f;
			currentHash=f;
			loadHash(currentHash);
	   }
	}else if(currentHash!=location.hash){
		currentHash=location.hash;
		loadHash(currentHash);
	}
}

// add buttons
function addTitleButton(a,b,e){
	e=(e)?" onclick='"+e+"'":"";
	if(!$("div#mainContent div.title_box").find("div.title_button").length) $("div#mainContent div.title_box").prepend("<div class='right title_button'><p></p></div>");
	var o=$("div#mainContent div.title_box div.title_button p");
	if(!o.eq(0).children("span."+b).length) o.prepend("<span class='"+b+" button'"+e+">"+a+"</span> ");
}

// graphical alphanumerics
function numalpha_getIndex(s){
	var i,t,r;
	var a=[];
	for(i=0;i<s.length;i++){
		t=s.charCodeAt(i);
		if(t==39) r=36;
		else if(t==35) r=38;
		else if(t==46) r=37;
		else if(t>=48&&t<=57) r=26+(t-48);
		else if(t>=65&&t<=90) r=(t-65);
		else if(t>=97&&t<=122) r=(t-97);
		else r=-1;
		a.push(r);
	}
	return a;
}

function gString(s){
	var gCharIndexArray=numalpha_getIndex(s);
	var gCharIndex, i;
	var gCharString="";
		
	for(gCharIndex in gCharIndexArray){
		if((i=gCharIndexArray[gCharIndex])<0) continue;
		gCharString+="<span class='char' style='width: "+(numalpha[i+1]-numalpha[i])+"px; background-position: -"+numalpha[i]+"px -3px'></span>";	
	}
	
	return gCharString;
}

// dynamic content effect after load page
function contentEffect(){
	// * URL CORRECTION *
	// resolve correct thumb div width and relative path
	$("img").not("_done").each(function(){
		$(this).attr("src",$(this).attr("src").replace(/\:8088/,""));
	//	$(this).attr("src",$(this).attr("src").replace(/takeru\.no-ip\.org/,"takeru.no-ip.org:8088"));
		var re=/images\/([\w\d_/.]+)$/i;
    	var arr=re.exec($(this).attr("src"));
		if(arr==null) return;
		
		var p=($(this).hasClass("_popup"));
		var d=$(this).attr("name");
		var src1="include/thumbnail.v4.php?m=c&p="+(d||currentPage)+"&f="+arr[1]+"&d="+$(this).width()+","+$(this).height()+"&t="+(!p?this.alt:"")+"&r=2&s="+(d?4:1);
		var src2="content/"+(d||currentPage)+"/images/"+arr[1];
			
		if(p) $(this).wrap("<a class='"+_popup_class+"' title='"+zstr.prodAddThumbEnlarge+"' name='"+this.title+"' rel='"+this.alt+"' href='"+src2+"'><span class='button button_page product_thumb'></span></a>").attr({alt:"",title:"",src:src1});
		else  $(this).attr("src",$(this).hasClass("_thumb")?src1:src2);
		
		$(this).addClass("_done");
	})

	// * OLD CONTENT CORRECTION * 
	$("a.external").each(function(){$(this).after("<span class='external' title='"+this.href+"'>"+this.innerHTML+"</span>");}).remove();

	// * GENERAL *
	// top image
	$("td.content").css("backgroundImage","url('files/top/"+((currentPage=="top")?topImageSrc:topImageSrcNorm)+"')");
	
	// content table styling
	$("table.inline tr td:last-child").css("border-right-style","none");
	$("table.inline tr").not(".title, #noEffect")
		.mousemove(function(){$(this).addClass("hover");})
		.mouseout(function(){$(this).removeClass("hover");})
	
	// sidebar module
	$("div.sidebar_module").not("._all").hide().filter("._"+currentPage).show();
	$("td.sidebar, td.sidebar_top").width(($("div.sidebar_module:visible").not(":empty").length)?173:0); 
	
	// * GRAPHIC ALPHANUMERICS *
	$("span.locno").each(function(){$(this).html("<span class='numalpha numalpha_locno'>"+$(this).html()+"</span>")});
	$("span.numalpha").each(function(){$(this).html(gString($(this).text()));});
	if($.browser.msie) $("span.char").html("<img src='files/images.v3/space.gif' height='22' width='1'>");
	
	// * EVENTS *
	// title bar button
	$("span.button, span.button_page").not(".button_page_active")
		.unbind()
		.mouseover(function(){$(this).addClass("button_hover");})
		.mouseout(function(){$(this).removeClass("button_hover");});
		
	// links and buttons
	$("span.internal").each(function(){this.title="/"+this.title.replace(/\,/,"/")});
	$("span.internal_product").each(function(){
		if(!$(this).hasClass("_noprepend")) $(this).prepend("<span class='internal_code'>"+this.title+"</span>");
		this.title="/products/"+this.title.substr(0,1)+"/p/"+this.title;
	})
	
	$("span.internal, span.internal_product, span.external")
		.unbind()
		.mousemove(function(){$(this).addClass("link_hover");})
		.mouseout(function(){$(this).removeClass("link_hover");})
		
	$("span.external").click(function(){window.open(this.title)});
	$("span.internal, span.internal_product").click(function(){load(this.title)});
	$("span.internal_reload").click(loadHash);
	$("span.product_group").click(function(){load("/products/"+currentSection+"/g/"+this.id)})

	$("div.thumb").each(function(){$(this).width($(this).children("p.img").children("img").width())});
}

function contentEffectSpecial(){
	var settings = {
		imageLoading: "files/nflightbox/loading.gif",   
		imageBtnPrev: "files/nflightbox/prev.png",   
		imageBtnNext: "files/nflightbox/next.png",   
		imageBtnClose: "files/nflightbox/close.png",    
		imageBlank: "files/nflightbox/lightbox-blank.gif",   
		imageBtnBottomPrev: "files/nflightbox/btm_prev.gif",   
		imageBtnBottomNext: "files/nflightbox/btm_next.gif",   
		imageBtnPlay: "files/nflightbox/start.png",    
		imageBtnStop: "files/nflightbox/pause.png"
	};
	$("img, a.nyroModal, .product_disp").tooltip({track: true, delay: 0, showURL: false});
	$(".nyroModal").nyroModal();
	$(".lightbox").lightBox(settings);
	//$(".thickbox").click(function(){tb_init(this); return false;})
	$(".product_disp").click(function(){$(this).parent().find("p#product_space_dp").toggle(); })
	product_checkEnlarge();
}

function contentEffectBlog(){
	// redirect form post to $.post
	$("form.blogForm, form#search_form").submit(function(){
		setLoading();
		$.post("lifetype/index.php",$(this).serializeArray(),function(data){
			$("div#mainContent").html(data.substring(data.indexOf("<!-- LTFContent"),data.lastIndexOf("<!-- LTFContent")));
			$("div#portalPanel").html(_sidebar_border+data.substring(data.indexOf("<!-- LTFSidebar"),data.lastIndexOf("<!-- LTFSidebar"))+_sidebar_border);
			__page_blog_handler(currentPage);
		});
		return false;
	});
	
	// login hacks
	$("form#loginForm").submit(function(){
		$.post("lifetype/admin.php",$(this).serializeArray(),function(data){
			if(data.indexOf("loginBox")!=-1){
				alert(zstr.adminOnly);
				return;
			}
			$.get("lifetype/admin.php?op=blogSelect&blogId=1",function(){
				alert(zstr.loginSuccess);
				//$("form#loginForm").addClass("prototype").next("#logoutButton").removeClass("prototype");
				loadHash();
			});
		});
		
		return false;
	})
	
	// logout hacks
	$("p#logoutButton").click(function(){
		$.get("lifetype/admin.php?op=Logout",function(){
			alert(zstr.logoutSuccess);
			//$("p#logoutButton").addClass("prototype").prev("form").removeClass("prototype");
			loadHash();
		});
	})
	
	// link hacks
	$("a.internal").add("div.pager a, td.calMonthToday a, td.calMonthDay a, a.showMoreLink, th.calMonthBackward a, th.calMonthForward a, p#pager a").each(function(){
		if($(this).text()=='read_more'){
			if($(this).parent().hasClass("content")) 	$(this).remove();
			else										$(this).parent().remove();
			return;
		}
		var re=/(\w+)\=([^&]+)/ig;
		var arr, op;
		var attr=[];
    	while((arr=re.exec(this.href))!=null){
			if(arr[1]=="op") 			op=arr[2];
			else if(arr[1]=="blogId") 	continue;
			else						attr[attr.length]=arr[0];
		}
		attr=(attr.length)?attr.join("&"):false;
		//alert($(this).html());
		if($(this).parent().hasClass("content_sidebar"))
			$(this).parent().attr({alt: (op?op:""), operation:(attr?attr:"")}).addClass("hasLink").html($(this).html());
		else
			$(this).after("<span class='internal' title='"+currentPage+"/"+(!op?"":op+"/"+(!attr?"":attr))+"'>"+$(this).html()+"</span>");
		//alert($(this).html());
		$(this).remove();
	})
	
	//pager
	var o=$("<p class='content'></p>");
	$("div.pager").children("span").each(function(i){
		if($(this).text().length>1){
			$(this).text((i==0)?zstr.newerComments:zstr.olderComments);
		}
		$(this).appendTo(o);
	}).end().html(o.html()).wrap("<p class='content'></p>");
	
	//reverse comment order
	if($("div.blog_cmodule").length>1){
		var o=$("div.blog_cmodule:eq(0)");
		$("div.blog_cmodule:not(.prototype)").each(function(){
			$(this).insertAfter(o);
		});
	}
	
	//further link hacks
	$("a").not(".external").each(function(){
		$(this).after("<span class='internal' title='"+this.href.substring(this.href.indexOf("#")+2,this.href.length)+"'>"+$(this).html()+"</span>");
		$(this).remove();
	});
	
	// convert menu to conform the styles
	$("div#portalPanel p.content_sidebar")
		.mouseover(function(){$(this).addClass("content_sidebar_active");})
		.mouseout(function(){$(this).removeClass("content_sidebar_active");})
		.filter(".hasLink")
		.click(subMenu_callback)
		.each(function(){
			if(currentSection==$(this).attr("alt")&&currentOperation==$(this).attr("operation"))
				$(this).unbind().addClass("content_sidebar_active");
		});
	
	$("div.blogMain > p").not(".subtitle").addClass("content");
	
	// preorder list hacks
	$("p.finished, p.finished_cancel, p.finished_pend, p.finished_not").each(function(){
		var i,text="";
		var arr=$(this).text().split("\u3000");
		if(arr.length==1) return;
		for(i=0;i<fwidth.length&&i<arr.length;i++)
			text+="<span class='finished_list' style='width: "+fwidth[i]+"px'>"+arr[i]+"</span>";
		$(this).html(text);
	});
}

// reset menu and title when load page
function resetSubMenu(sectionArray,currentSection){
	var i, c;
	if(sectionArray.length>0){
		$("div#menuList").empty().show();
		
		for(i=0;i<sectionArray.length;i++){
			$("div#menuList").append("<p class='"+(sectionArray[i].id==""?(c="line"):c?"content_sidebar content_sidebar_indent":"content_sidebar")+"' alt='"+sectionArray[i].id+"'>"+sectionArray[i].sectionName+"</p>");
			if(sectionArray[i].id!=""){
				$("div#menuList").children("p.content_sidebar:last").attr({operation: sectionArray[i].operation, matches: sectionArray[i].matches});
			}
		}
		
		$("div#menuList p.content_sidebar")
			.mouseover(function(){$(this).addClass("content_sidebar_hover");})
			.mouseout(function(){$(this).removeClass("content_sidebar_hover");})
			.click(subMenu_callback)
			.each(function(){
				if($(this).attr("matches").match("/\|"+currentSection+"|\/")){
					var t=$(this)
						.unbind()
						.addClass("content_sidebar_active")
						.text();
					currentTitle.push(t);
				}
			});
		
		$("div#menuList")
			.prepend(_sidebar_border)
			.append(_sidebar_border);
	}else{
		$("div#menuList").hide();
	}
	
	$("td.sidebar, td.sidebar_top").width(($("div.sidebar_module:visible").not(":empty").length)?173:0); 
}

function subMenu_callback(){
	load($(this).attr("alt").substr(0,1)=="/"?$(this).attr("alt"):"/"+currentPage+"/"+$(this).attr("alt")+"/"+$(this).attr("operation"));
}

function resetTopMenu(){
	$("span.menu_active").removeClass("menu_active");
	currentTitle=[$("span.menu[id='"+currentPage+"']").addClass("menu_active").text()];
	
	$("span.menu")
		.unbind()
		.click(function(){load("/"+this.id)})
		.not(".menu_active")
		.mousemove(function(){$(this).addClass("menu_active");})
		.mouseout(function(){$(this).removeClass("menu_active");})
}

function resetTitle(){
	currentTitle.unshift(defaultTitle);
	document.title=currentTitle.join(" > ");
	if($.browser.msie) $("#history").get(0).contentWindow.document.title=document.title;
}

// loading signature
function setLoading(){
	$("*").mousemove(__loading_callback);
	$("#loadingbar").show();
}

function clearLoading(){
	$("#loadingbar").hide();
	$("*").unbind("mousemove",__loading_callback);	
}

function __loading_callback(e){
	$("#loadingbar").css({
		top: e.pageY+10,
		left: e.pageX+10
	});
}

// auto resize table
function setTableHeight(){
	var h=document.body.clientHeight-254;
	$("table.content_layout").height(h);
	$("td.content_area").height(h);
}

/***************************************
 PAGE LOADING
 ***************************************/

// load page 
function load(hash){
	if($.browser.msie||$.browser.mozilla||$.browser.opera) $("#history").get(0).src="~"+language+hash;
	location.assign("#"+hash.replace(/\/{2}/,"/"));
}

function loadHash(){
	var h =location.hash?location.hash:"#/top";
	var hA=h.substr(2).split("/");
	if(hA[2]=="g")				switchProductPage(hA[3]);
	else if(hA[0]=="portal")	loadBlog(hA[0],hA[1],hA[2]);
	else if(hA[0]=="buy")		loadBlog(hA[0],hA[1],hA[2]);
	else						loadPage(hA[0],hA[1],hA[2],hA[3]);
}

function loadBlog(blog,op,attr){
	var urlop=(!op)?"":"&op="+op;
	var urlattr=(!attr)?"":"&"+attr;
	var d=new Date();
	var blogID=(blog=="portal")?2:1;

	setLoading();

	$.get("lifetype/index.php?blogId="+blogID+urlop+urlattr+"&t="+d.getTime(),function(data){
		if($.browser.msie){
			var o=$("div#mainContent").get(0);
			o.innerHTML=data.substring(data.indexOf("<!-- LTFContent"),data.lastIndexOf("<!-- LTFContent"));
		}else{
			$("div#mainContent").html(data.substring(data.indexOf("<!-- LTFContent"),data.lastIndexOf("<!-- LTFContent")));
		}
		$("div#portalPanel").html(_sidebar_border+data.substring(data.indexOf("<!-- LTFSidebar"),data.lastIndexOf("<!-- LTFSidebar"))+_sidebar_border);
		currentSection=(!op)?"":op;
		currentOperation=(!attr)?"":attr;
		__page_blog_handler(blog);
	})
}

function __page_blog_handler(blog){
	currentPage=blog;
	contentEffectBlog();
	contentEffect();
	resetTopMenu();
	resetSubMenu([],"");
	resetTitle();
	loadPage_finalize();
};

function loadPage(id,isSection,operation,code,page){
	// initialize undefined parameters
	if(!id) id="top";
	if(!isSection) isSection="index";
	if(isSection=="index"&&id=="products") isSection="T";
	
	setLoading();
	
	$.get("include/content.page.php",{page: id, section: isSection, operation: operation, version: "v4"},function(data){
		// insert contents
		$("div#mainContent").html(data);
		
		// re-creating content effect, and reset main menu
		currentPage=id;
		currentSection=isSection;
		contentEffect();
		resetTopMenu();
		
		// special performances for different sections
		if(operation=="*")							loadPage_finalize();
		else if(id=="products"||operation=="p")		getProduct(isSection,code,page);
		else if(id=="book"&&isSection=="index")  	loadPage_finalize();
		else if(id=="book")					 	 	getProductThumb();
		else if(id=="works")					 	getGallery(isSection=="index"?defaultGallery:isSection);
		else 										loadPage_finalize();
	});

	// processing sub-menu
	$.get("include/content.submenu.php",{page: id, version: "v4"},function(data){
		if(data){
			var newArray,i;
			eval(data);
			if(id=="products"){
				for(i=0;i<newArray.length;i++)
					if(newArray[i].id.indexOf(isSection)!=-1)
						isSection=newArray[i].id;
			}
			resetSubMenu(newArray,isSection);
		}else{
			resetSubMenu([],"");
		}
		
		resetTitle();
		setTableHeight();
	});	
}

function loadPage_finalize(){
	contentEffectSpecial();
	setTableHeight();
	clearLoading();
}

/***************************************
 PRODUCTS
 ***************************************/
function switchProductPage(str){
	var b=str.length-4;
	if(currentPage!="products") loadPage("products",str.substr(0,1+b),"p",str.substr(1+b,2),str.substr(3+b,1));
	else						displayProduct(str.substr(0,1+b),str.substr(1+b,2),str.substr(3+b,1));
}

function getProduct(category,group,page){
	if(!group) group=0
	
	if(_cache_product_group[category]&&(!group||(group&&(!parseInt(group[1])&&(!parseInt(group[2])))))){
		if(page!=-1) displayProduct(category,group,page);
		else         displayProductThumb(category);
	}else{
		$.get("include/product.list.php",{category: category, groupx: group},function(data){
			if(data){
				var itemArray,groupArray;
				eval(data);
				
				_cache_product_group[category]=groupArray;
				_cache_product_list[category]=itemArray;
				
				if(page!=-1) displayProduct(category,group,page);
				else         displayProductThumb(category);
			}
		});
	}
}

function getProductThumb(){
	var q=$("#product_space_t");
	var v=q.children("span").attr("id");
	getProduct("DU",v,-1);
}

function displayProductThumb(c){
	var q=$("#product_space_t").children("p");
	var v=q.attr("id");
	var w=_cache_product_list[c][v];
	
	if(w.addthumb.length){
		for(z=0;z<w.addthumb.length;z++)
			q.append("<a class='"+_popup_class+"' title='"+zstr.prodAddThumbTitle+"' name='"+v+" - "+w.name+"' rel='"+v+"' href='content/products/images/"+w.addthumb[z]+"'><span class='button button_page product_thumb'><img src='include/thumbnail.v4.php?m=p&f="+w.addthumb[z]+"'></span></a>");
	}
	
	loadPage_finalize();
	
	$("span.button, span.button_page").not(".button_page_active")
		.unbind()
		.mouseover(function(){$(this).addClass("button_hover");})
		.mouseout(function(){$(this).removeClass("button_hover");})
}

function displayProduct(category,group,page){
	if(!page) page=1;

	// update session info
	$.get("include/product.list.php",{updateGroup: true, groupx: group});
	
	var i,j,hash,menuStr="",spaceStr="";
	var nv=procVCol[category]?procVCol[category]:procVCol.O;
	var nh=procHCol[category]?procHCol[category]:procHCol.O;
	
	if(!_cache_product_menu[category]){
		for(i=0;i<_cache_product_group[category].length;i++){
			var a=_cache_product_group[category][i];
			var b=a.vNumber, v=0; 
			var c=a.hNumber, h=0;
			while(b>0||c>0){
				if(b>0){
					b=b-nv;
					a.page.push("v"+(++v));
				}
				if(c>0){
					c=c-nh;
					a.page.push("h"+(++h));
				}
			}
			
			menuStr+="<span class='button button_page product_group"+(a.id=="xx"?" product_group_xx":"")+"' id='"+category+a.id+"1'>"+(a.id=="xx"?zstr.prodGroupXX:a.id=="nn"?zstr.prodGroupNN:a.name)+(a.page.length>2?" 1":"")+"</span>";
			
			if(a.page.length>2){
				for(j=2;j<=(a.page.length+1)/2;j++){
					menuStr+="<span class='button button_page product_group"+(a.id=="xx"?" product_group_xx":"")+"' id='"+category+a.id+j+"'>"+j+"</span>";
				}
			}
		}
		_cache_product_menu[category]=menuStr;
	}
	
	$("div#product_menu p").html(_cache_product_menu[category]);

	if(!group||group=="0"){
		group=0;
		hash=category+_cache_product_group[category][0].id+page;
	}else{
		hash=category+group+page;
		for(i=0;i<_cache_product_group[category].length;i++){
			if(_cache_product_group[category][i].id==group){
				group=i;
			}
		}
	}
	
	// write the title bar
	$("p#proc_title").html($("div#menuList p.content_sidebar_active").text()+": "+(_cache_product_group[category][group].id=="xx"?zstr.prodGroupXX:_cache_product_group[category][group].id=="nn"?zstr.prodGroupNN:_cache_product_group[category][group].name));
	$("span.product_group[id='"+hash+"']").addClass("button_hover button_page_active");
	$("#product_space").empty();

	if(_cache_product_menu[hash]){
		$("#product_space").html(_cache_product_menu[hash]);
		contentEffect();
	}else{
		var t=$("div#product_space_d");
		var u=$("div#product_space_i");
		
		for(p=0;p<=1;p++){
			if((page-1)*2+p>=_cache_product_group[category][group].page.length) break;
			var j=_cache_product_group[category][group].page[(page-1)*2+p];
			var c=0;
			var b=(j.substr(0,1)=="v"?nv:nh);
			var h=(j.substr(0,1)=="v"?_cache_product_group[category][group].vNumber:_cache_product_group[category][group].hNumber)
			var a=b*(parseInt(j.substr(1,1))-1);
			var y=u.clone().appendTo("#product_space").removeClass("prototype");
			var x=t.clone().appendTo("#product_space").removeClass("prototype");
			var d=false, dd=false;
			
			while(c++<b&&a+c<=h){
				var v=_cache_product_group[category][group]["list"][j.substr(0,1)][a+c-1];
				var w=_cache_product_list[currentSection][v];
				var o=x.children(".prototype").clone().appendTo(x).removeClass("prototype").attr("id",v);
				var i=false;
				
				if(v.substr(0,1)=="D"||v.substr(0,1)=="U"){ o.addClass('item_container_book'); i=true; dd=true; }
				if(w.unavailable){ 	o.addClass('item_container_end'); w.stock=zstr.prodEnd;}
				else if(w.isnew){ 	o.addClass('item_container_new'); w.stock=zstr.prodNew;}
				else if(w.nomuch){	o.addClass('item_container_few'); w.stock=zstr.prodFew;}
				else{o.addClass('item_container_ok'); w.stock=zstr.prodOK;}
				
				o.find("span.psd").each(function(){
					if(!w[this.id])		$(this).addClass("hidden").prev().addClass("hidden");
					else				$(this).html(w[this.id]);
				});
				
				if(!w.nothumb){
					y.find(".prototype").clone().insertBefore(y.find(".prototype"))
						.removeClass("prototype")
						.append("<a class='"+_popup_class+"' onmouseover='product_highlight(this.rel,true)' onmouseout='product_highlight(this.rel,false)' name='"+v+" - "+w.name+"' rel='"+v+"' href='content/products/images/"+v+".jpg'><img src='content/products/images/"+v+".jpg' id='"+v+"' title='"+zstr.prodAddThumbEnlarge+"' class='psd_image"+j.substr(0,1)+"'></a>")
				}
				
				var q=o.find("#product_space_t");
				
				if(w.sp){
					q.append("<span class='internal' title='book,"+w.sp+"'><span class='button button_page product_thumb'>"+zstr.prodAddPage+"</span></span>");	
				}
				
				if(w.addthumb.length){
					for(z=0;z<w.addthumb.length;z++)
						q.append("<a class='"+_popup_class+"' title='"+zstr.prodAddThumbTitle+"' name='"+v+" - "+w.name+"' rel='"+v+"' href='content/products/images/"+w.addthumb[z]+"'><span class='button button_page product_thumb'><img src='include/thumbnail.v4.php?m=p&f="+w.addthumb[z]+"'></span></a>"); //"+zstr.prodAddThumb+"
				}
				
				if(w.disp&&!$.browser.msie) o.children("p#product_space_dn").addClass("etc product_disp pointer").attr("title",zstr.prodDetailDesp);
				if(w.disp&&(w.isnew||$.browser.msie)) o.children("p#product_space_dp").show();
			}
			if(dd) x.find(".item_container").addClass("item_container_double");
		}
		contentEffect();
		_cache_product_menu[hash]=$("#product_space").html();
	}
	loadPage_finalize();
}

function product_checkEnlarge(){
	$("img.psd_imagev, img.psd_imageh").each(function(){product_checkEnlargeCallback($(this),this.id)});
}

function product_checkEnlargeCallback(o,id){
	var i=new Image();
	i.onload=function(){
		if(this.width<o.width()*1.5&&this.height<o.height()*1.5){
			var p=$("div.item_container#"+id+" #product_space_t a."+_popup_class);
			if(p.length) o.attr("title",zstr.prodAddThumbTitle).tooltip({track: true, delay: 0, showURL: false}).parent().unbind("click").click(function(){p.eq(0).click(); return false;});
			else		 o.unbind("mouseover").parent().css("cursor","default").unbind("click").click(function(){return false});
		};
	};
	i.src="content/products/images/"+id+".jpg";
}

function product_highlight(id,isActive){
	if(isActive) $("img#"+id+",div.item_container#"+id).addClass("item_container_active");
	else		 $("img#"+id+",div.item_container#"+id).removeClass("item_container_active");
}

/***************************************
 GALLERY
 ***************************************/
function getGallery(section){
	if(_cache_gallery){
		$("div#works_wrap").html(_cache_gallery);
		contentEffectSpecial();
	}else{
		$.get("include/gallery.list.php",{category: section},function(data){
			if(data){
				var subFolderArray,o,pt,href,content,date,i,j,h;
				var p=$("div#works_category").filter(".prototype");
				
				eval(data);
				
				for(i=0;i<subFolderArray.length;i++){
					h="";
					
					o=p.clone().appendTo("div#works_wrap").removeClass("prototype");
					o.children("p.subtitle").html(subFolderArray[i].sectionName);
					
					if(subFolderArray[i].image.length>0){
						for(j=0;j<subFolderArray[i].image.length;j++){
							// date=subFolderArray[i].image[j].year+"-"+subFolderArray[i].image[j].month+"-"+subFolderArray[i].image[j].day;
							href="item/"+subFolderArray[i].image[j].path+"/"+subFolderArray[i].image[j].filename+".jpg";
							
							var t="<a class='"+_popup_class+" thumbnail_container"+(subFolderArray[i].image[j].isNew?" thumbnail_container_new":"")+"' ";
							t+="href='"+(!subFolderArray[i].image[j].protected?href:"content/_global/gallery.password.html")+"' file='"+href+"' name='"+subFolderArray[i].image[j].title+"' rel='"+subFolderArray[i].id+"'>";
							t+="<img style='background-image: url(include/thumbnail.v4.php?p="+subFolderArray[i].image[j].path+"&y="+subFolderArray[i].image[j].year+"&f="+subFolderArray[i].image[j].filename+"&d=30,50&m=i)' title='"+subFolderArray[i].image[j].title+"' src='files/images.v3/space.gif' height='50' width='30'></a>";
							
							h=t+h;
						}
					}
					
					o.children("p#works").html(h);
				}
				
				$("div#works_wrap").children("p.title").html(zstr.titleGallery+section+zstr.titleGalleryFinish);
				contentEffect();
				_cache_gallery=$("div#works_wrap").html();
				loadPage_finalize();
			}
		});
	}
}