var ie = document.all ?	true:false;
var ie7 = navigator.userAgent.indexOf('MSIE 7')!=-1;
var	nn4	= document.layers ?	true:false;
var _width, _height;
var initiated = false;

function get_scr_res(){
	_width = (nn4) ? window.innerWidth + 4 : document.body.offsetWidth;
	_height = (nn4) ? window.innerHeight + 4 : document.body.offsetHeight;
}

function pageInit() {
	if (!initiated){
		initiated = true;
	}
	get_scr_res();
	if (document.getElementById("hpPod1")) hpSetup(); // start floating
	if (window.pageSpecificInit) pageSpecificInit();
}

window.onload = pageInit;
window.onresize = pageInit;

function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) curleft += obj.x;
	return curleft;
}
function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) curtop += obj.y;
	return curtop;
}

/************************* jquery ***************************************/
var img = new Image();
var pause = new Image(); pause.src = "img/pause.gif";
var play = new Image(); play.src = "img/play.gif";
var curIndex = 1;
var myWrap = null;

$(document).ready(function(){
	myWrap = $("#main DIV");
	canvasCoords[0] = findPosX($("#canvas")[0]);
	canvasCoords[1] = document.getElementById("canvas").scrollWidth;
	//	wTitle, wCount, wTotal, wPrev, wNext
	$("#wTotal").html(homePics.length);
	$("#wPrev").click(function(){
		$("#pp")[0].src = play.src;
		clearTimeout(window.playTimer);
		curIndex = (curIndex==1) ? homePics.length : curIndex-1;
		updateImage();
		return false;
	});
	$("#wNext").click(function(){
		$("#pp")[0].src = play.src;
		clearTimeout(window.playTimer);
		curIndex = (curIndex==homePics.length) ? 1 : curIndex+1;
		updateImage();
		return false;
	});
	$("#mImage").click(function(){
		$("#wNext").click();
	});
	for(i=0;i<homePics.length;i++){ // start loading in the background;
		newImg = new Image();
		newImg.src = homePics[i][1];
		homePics[i][1] = newImg;
	}
	function updateImage(){
		$("#wCount").html(curIndex);
		img = $("#mImage:first");
		$("#main").addClass("loading");
		$(img).fadeOut(250, function(){
			$(img).attr('width', homePics[curIndex-1][2]);
			$(img).attr('height', homePics[curIndex-1][3]);
//			alert(curIndex+" A "+homePics[curIndex-1][2]);
			myWrap.css({width:homePics[curIndex-1][2]+"px"});
			myWrap.css({height:homePics[curIndex-1][3]+"px",border:"5px solid #FFFFFF"});
//			alert(typeof(homePics[curIndex-1][1])=="string");
			if(typeof(homePics[curIndex-1][1])=="string"){
				$(img).attr('src', homePics[curIndex-1][1]);
				$(img).load(function(){ // image loading
				    $("#main").removeClass("loading");
					$("#wTitle").html(homePics[curIndex-1][0]);
					$(img).fadeIn(500);
					newImg = new Image();
					newImg.src = $(img).attr('src');
					homePics[curIndex-1][1] = newImg;
//					alert()
//					clearTimeout(window.playTimer);
//					window.playTimer = setTimeout("playHP()", 4000);
				})
			}else{
//				$(img).attr("");
				$(img).attr('src', homePics[curIndex-1][1].src);
				$(img).load(function(){ // image loading
				    $("#main").removeClass("loading");
					$("#wTitle").html(homePics[curIndex-1][0]);
					$(img).fadeIn(500);
//					alert(window.playTimer);
//					clearTimeout(window.playTimer);
//					window.playTimer = setTimeout("playHP()", 4000);
				})
			}
//			if(homePics[curIndex-1][3]>452){
//				alert("ddd")
				myMarginLeft = (950-parseInt(homePics[curIndex-1][2]))/2;
				coords[0] = window.findPosX(document.getElementById("main"))+myMarginLeft;
				coords[1] = window.findPosY(document.getElementById("main"));
				coords[2] = coords[0]+parseInt(homePics[curIndex-1][2])+5;
				coords[3] = coords[1]+parseInt(homePics[curIndex-1][3])+20;
				$("#main DIV").css({margin:"50px "+(myMarginLeft-5)+"px 0px "+(myMarginLeft-5)+"px",width:homePics[curIndex-1][2]+"px"})
				$("#pods").css({opacity:"0.3"});
				window.checkForMouse = true;
/*			} else{
				$("#main DIV").css({margin:"0px auto 0px auto"})
				$("#pods").css({opacity:"1"});
				window.checkForMouse = false;
			}*/
		})
	}
	window.playHP = function(){
		curIndex = (curIndex==homePics.length) ? 1 : curIndex+1;
		updateImage();
//		clearTimeout(window.playTimer);
		window.playTimer = setTimeout("playHP()", 5000);
	}
	window.playTimer = setTimeout("playHP()", 5000);
	$(".pp").click(function(){
		ppBtn = $("#pp")[0];
		if(ppBtn.src.indexOf("pause")==-1){
			clearTimeout(window.playTimer);
			playHP();
			ppBtn.src = pause.src;
		} else{
			clearTimeout(window.playTimer);
			ppBtn.src = play.src;
		}
		return false;
	});
	$("#album A.item").click(function(){
//		$("#main DIV").css({margin:"0px auto 0px auto"})
//		alert($(this).attr("title"));
		$("#viewer").show();
		return false;
	});
});

var checkForMouse = false;
var curX = 0;
var curY = 0;
var canvasCoords = new Array(0,0) // x, width
var coords = new Array(0,0,0,0);
function checkMouse(e){
//	alert(checkForMouse)
	if(checkForMouse){
		curX = ie ? (event.x + document.body.scrollLeft) : e.pageX;
		curY = ie ? (event.y + document.body.scrollTop) : e.pageY;
		if(
			(
				((curX<coords[0] && curX>canvasCoords[0] || curX>coords[2] && curX<canvasCoords[0]+canvasCoords[1]) 
					&& curY>coords[1]+472)
					||  
				(curX>canvasCoords[0] && curX<canvasCoords[0]+canvasCoords[1] && curY>coords[3])
			)
			 && myWrap.css("height")!="452px"
		){
		
//			document.getElementById("navigation").innerHTML += myWrap.css("height")+"<br>";
			myWrap.css({height:"452px",border:"5px solid #a3d517"});
			coords[3] = coords[1]+452+20;
			$("#pods").css({opacity:"1"});
		} else if(!(curY>coords[3] || (curX>coords[2] && curY>coords[1]+472 && curX<canvasCoords[0]+canvasCoords[1]) || (curX<coords[0] && curY>coords[1]+472) && curX>canvasCoords[0]) && myWrap.css("height")!=coords[1]+parseInt(homePics[curIndex-1][3])+"px") {
//			document.getElementById("navigation").innerHTML += myWrap.css("height")+"<br>";
			myWrap.css({height:homePics[curIndex-1][3]+"px",border:"5px solid #FFFFFF"});
			coords[3] = coords[1]+parseInt(homePics[curIndex-1][3])+20;
			$("#pods").css({opacity:"0.3"});
		}
	}
}
//window.onmousemove = checkMouse;