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;

var myTimer = false;
var scrollToY = 0;
var currentYoffset = -1;

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");
	//	wTitle, wCount, wTotal, wPrev, wNext
	$("#wTotal").html(homePics.length);
	$("#wPrev").click(function(){
		curIndex = (curIndex==1) ? homePics.length : curIndex-1;
		updateImage();
		return false;
	});
	$("#wNext").click(function(){
		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");
//		alert(img)
		$("#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;
				})
			}else{
				$(img).attr({"src":""});
//				alert($(img).attr("src"))
				$(img).attr('src', homePics[curIndex-1][1].src);
//				alert($(img).attr("src"))
				$(img).load(function(){ // image loading
				    $("#main").removeClass("loading");
					$("#wTitle").html(homePics[curIndex-1][0]);
					$(img).fadeIn(500);
				})
				window.doubleChcekTimer = setTimeout('$(img).attr("src", homePics[curIndex-1][1].src);', 600)
			}
			doResize();
		})
	}
	$("#album A.item").click(function(){
		if($("#close").length<1) $("BODY").append('<a href="#" id="close" title="Close"><img src="img/close.gif" width="61" height="27" alt="Close" /></a>')
		else $("#close").show();
		$("#close").click(function(){
			$("#viewer").hide();
			$("#close").hide();
			return false;
		})
	$("#viewer").css({"width":$().width()});
	$("#viewer").css({"height":$().height()});

//		alert($(this).attr("title"));
		curIndex = parseInt($(this).attr("id"));
		updateImage();
		
		doResize();
		$("#viewer").show(10,function (){
			window.scrollToY = 0;
			animator(); // and smoothly scrolls
		});
		return false;
	});
});
function doResize(){
	isViewerActive = ($("#viewer").css("display")=="block") ? true : false;
//	alert(isViewerActive);
	if(!document.all){
		document.getElementById("close").style.display = "none";
		document.getElementById("viewer").style.display = "none";
		$("#viewer").css({"width":$().width()});
		$("#viewer").css({"height":$().height()});
		myMarginLeft = ($().width()-890)/2;
		$("#viewer .header").css({"margin":"71px "+myMarginLeft+"px 20px "+myMarginLeft+"px"})
		$("#close").css({"marginLeft":(myMarginLeft+829)+"px"})
		divMarginLeft = ($().width()-parseInt(homePics[curIndex-1][2]))/2;
		$("#main DIV").css({"margin":"0px "+(divMarginLeft-5)+"px 0px "+(divMarginLeft-5)+"px",width:homePics[curIndex-1][2]+"px"})
		if(isViewerActive) {
			document.getElementById("viewer").style.display = "block";
			document.getElementById("close").style.display = "block";
		}
	} else { // stupid IEs
//		alert("dd")
		$("#viewer .header").css({"margin":"71px 0px 20px 0px"})
		$("#main DIV").css({"margin":"0px 0px 0px 0px 0px"})
		$("#viewer").css({"width":"0px"});
		$("#close").css({"marginLeft":"0px"});
		document.getElementById("viewer").style.display = "none";
		document.getElementById("close").style.display = "none";
//			alert("$().width()");
//			$("#viewer").hide();
		$("#viewer").fadeIn(1,function(){
			$("#viewer").css({"width":$().width()});
			$("#viewer").css({"height":$().height()});
			myMarginLeft = ($().width()-890)/2;
			$("#viewer .header").css({"margin":"71px "+myMarginLeft+"px 20px "+myMarginLeft+"px"})
			$("#close").css({"marginLeft":(myMarginLeft+829)+"px"})
			divMarginLeft = ($().width()-parseInt(homePics[curIndex-1][2]))/2;
			$("#main DIV").css({"margin":"0px "+(divMarginLeft-5)+"px 0px "+(divMarginLeft-5)+"px",width:homePics[curIndex-1][2]+"px"})
			if(isViewerActive) {
				$("#viewer").show();
				$("#close").show();
			}
		});
	}
}
window.onresize = doResize;

function animator(){
	viewportH = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
 	myNumber = $().height()-viewportH+10;
//	alert(myNumber+" = "+$().height()+" - "+viewportH);
	myPageYOffset = $.browser.msie ? document.body.scrollTop : pageYOffset;
	if (scrollToY!=myPageYOffset && currentYoffset!=parseInt(scrollToY+myPageYOffset)/2 && currentYoffset!=myPageYOffset){
		if(myNumber<scrollToY) scrollToY = myNumber;
		if(scrollToY<0) scrollToY = 0;
		currentYoffset = myPageYOffset;
		window.scrollTo(0,parseInt(scrollToY+myPageYOffset)/2);
		window.myTimer = setTimeout("animator()", 50);
	} else {
		clearTimeout(window.myTimer);
	}
}
