try {document.execCommand("BackgroundImageCache", false, true);} catch(err) {}
function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	

	arrayPageSize = new Array(windowWidth,windowHeight);
	return arrayPageSize;
};

min_random = 1;
max_random = 7;
randomN=Math.floor(Math.random()*(max_random - min_random)) + min_random;

$(document).ready(function () {
	M = $("#main");
	b = $("#big");
	if(M.get(0)) {
		$("#big", M).attr({src: "/i/splash"+randomN+".jpg"});
		typeImg="";
	}
	else {typeImg="site"; b.hide();}
	fi = 0;
	bp = 0;
	fi1 = 0;
	function imagesLoaded()	{
		if(fi1==0) {
			if(bw > arrPageSizes[0]) {
				b.css({height:"auto", width:"80%", marginLeft: "10%", left:0}).show();
			}
			else {				
				b.css({height: "75%", width: "auto", marginLeft: -$(b).width()/2, left:"50%"}).show();
			}
			fi1=1;
		}
		else {b.fadeOut("normal", function() {b.attr("src", href); fi = 1; bresize();});}
	}
	checkLoad = function() {
		bp++;
		t.css({backgroundPosition: bp});
		if(!objImage.complete) {checkLoadT = setTimeout("checkLoad()",20);}
		else {imagesLoaded();}
	}

	function bresize() {
		bw = $(b).width();
		bh = $(b).height();
		arrPageSizes = getPageSize();
		if(typeImg=="site") {
			if(fi1==0) {
				b.css({marginTop: 0, top:"25px"});
				objImage = new Image();
				objImage.src=$(b).attr("src");
				t=$(".active").parent();
				checkLoad();
			}
			else {
				b.hide().css({width:"auto"});
				bw = $(b).width();
				if(bw >= arrPageSizes[0]+1) {
					b.css({height:"auto", width:"80%", marginLeft: "10%", left:0, marginTop: 0, top:"25px"}).show();
				}
				else {
					b.css({height: "75%", width: "auto"});
					bw = $(b).width();
					b.css({marginLeft: -bw/2, left:"50%", marginTop: 0, top:"25px"}).show();
				}
			}
		}
		else {
			var ratio = arrPageSizes[0]/arrPageSizes[1];
			if(ratio >= 1.6) {
				dif = arrPageSizes[0] - bw;
				res = 111+dif/(bw/100);
				bwr = bw*res/100;
				bhr = bh*res/100;
				b.css({height: bhr+"px", width: bwr+"px", marginTop: -bhr/2+"px", marginLeft: -bwr/2+"px", top:"50%", left:"50%"});			
			}
			else if(ratio < 1.6) {
				dif = arrPageSizes[1] - bh;
				res = 100+dif/(bh/100);
				bwr = bw*res/100;
				bhr = bh*res/100;
				b.css({height: bhr+"px", width: bwr+"px", marginTop: -bhr/2+"px", marginLeft: -bwr/2+"px", top:"50%", left:"50%"});			
			}
		}
		if(fi==1) {b.fadeIn("normal"); fi=0;}
	}
	$(window).resize(function() {bresize();});
	bresize();

	$("#wrap").css({width: 88*($("#works li:not(.separator)").length)+451});
	C = $("#current");

	wa = $("#works a");
	wa.add(b).click(function() {
		objImage = new Image();
		i = wa.index(this);
		t = $(this);

		if(this.id=="big") {
			a = $(".active");
			an = a.next("li");
			if(an.hasClass("separator")) {an = an.next("li");}
			if(an.get(0)) {
				t = $("a", an);
				i = wa.index(t);
				ii=i+1;
				if($("body").hasClass("eng")) {location.href = "/en/xhtml/#/"+ii;}
				else {location.href = "/xhtml/#/"+ii;}
			}
			else {
				return false;
			}
		}

		if(i==0) {href = '/i/works/img1.jpg';}
		else {href = '/i/works/img'+t.attr("href").replace('#/', '')+'.jpg';}
		objImage.src=href;

		cur = i+1;
		cur = cur.toString();
		$(C).html("");
		for(j=0; j<cur.length; j++) {
			ca = cur.charAt(j);
			$(C).append("<span class='d"+ca+"'><span>"+ca+"</span></span>");
		}

		$(".active").removeClass("active");
		tp = t.parent();
		tp.addClass("active");
		a = $(".active");
		an = a.next("li");
		if(an.get(0)) {b.css({cursor: "pointer"});}
		else {b.css({cursor: "default"});}

		if(tp.hasClass("site")) {typeImg="site";}
		else {typeImg="";}

		checkLoad();

		if(i==0) {
			if($("body").hasClass("eng")) {location.href = "/en/xhtml/#/1";}
			else {location.href = "/xhtml/#/1";}
			return false;
		}
	});
});