/* lib */

function getObjects(){
scrollerDiv = document.getElementById("scroller");
container = document.getElementById("paintingsList");
paintingsTableObj = document.getElementById("paintingsTable"); 
paintingsDivObj = document.getElementById("paintingsDiv"); 
paintings = container.getElementsByTagName("td");
paintingImages = container.getElementsByTagName("img");


}

function getConstants(){
scrollerWidth = 409;
speed = 2;
scrollerWidthAdjusted = scrollerWidth-8;


}

function setElementLocation(position,element){
paintings[element].style.left = position + "px";
}

function getVariables(){

leftCorner = parseInt("" + paintingsTableObj.style.left);
tableWidth = parseInt("" + paintingsTableObj.offsetWidth);
rightCorner = (tableWidth + leftCorner);

}


function reset (){

getVariables();
/* for some reason Mozilla doesn't read this value from css, only from js */
paintingsTableObj.style.left = "0px";

}


function checkDimensions(){
if (document.all){
		if(document.body.clientHeight < 580){
		document.getElementsByTagName("body").item(0).style.overflow = "scroll";
		}
		else {
		document.getElementsByTagName("body").item(0).style.overflow = "hidden";
		}
		}
}


/* functions */
function checkIframe(){
if(window.parent == window.self){
var path = location.href.split("/");
var slug= path[5];
var photo= path[6];
// alert(slug + " " + photo]);
window.location.href="http://www.ctv.ca/idol/gen/Photo1.html?" + slug + "/" + photo;
}
}
function eightMinsCookie(){
var tmpDate1=new Date();
var tmpDate2=new Date();
var tmpSeconds = tmpDate1.getSeconds() + (5*60);
tmpDate2.setSeconds(tmpSeconds);
var tmpUTC2 = Date.UTC(tmpDate2.getFullYear(),tmpDate2.getMonth(),tmpDate2.getDate(),tmpDate2.getHours(),tmpDate2.getMinutes())
setCookie("TDClickHere", tmpUTC2, "", "/");
}


function playButton(){
var playSplash = true;
var playInterstitial = false;
if(adCookie = /(TDClickHere)=([^;]+)/i.exec(document.cookie)){
	var today = new Date();
	var UTCToday = Date.UTC(today.getFullYear(),today.getMonth(),today.getDate(),today.getHours(),today.getMinutes());
	cookieTime = /(TDClickHere)=([^;]+)/i.exec(document.cookie)[2];
	cookieDate = new Date(cookieTime);

	// alert(cookieTime + " " + UTCToday);
	if(UTCToday >= cookieTime){
	playSplash = false;
	playInterstitial = true;
	}
	else{
	playSplash = false; 
	playInterstitial = false;
	}
}
    if(playSplash==true){
	writeClick();
	eightMinsCookie();
        }
	else if(playInterstitial==true){
//	writeInterstitial();
	eightMinsCookie();
}
}

function writeInterstitial(){
        var fo = new FlashObject("http://images.ctv.ca/mini/idol2006/images/TDInterstitial.swf", "mymovie", "363", "272", "7", "#ffffff");
        fo.addParam("wmode", "transparent");
fo.addParam("swliveconnect", "true");
fo.write("TDClickHere");
}

function writeClick2(){
        var fo = new FlashObject("http://images.ctv.ca/mini/idol2006/images/TDClickHere2.swf", "mymovie", "363", "272", "7", "#ffffff");
        fo.addParam("wmode", "transparent");
fo.addParam("swliveconnect", "true");
fo.write("TDClickHere");
}



function initialize(){
	checkIframe();
	playButton();
	getObjects();
	getConstants();
	reset();
	if (tableWidth > scrollerWidth){
		scrollerDiv.style.display = "block";
		scrollToCurrent();
	}
	checkDimensions();
}

function scrollToCurrent(){
getObjects();
 getVariables();
positionOfCurrent = parseInt("" + document.getElementById("currentPainting").offsetLeft);
if (((rightCorner-(positionOfCurrent-24)) > scrollerWidth) && (positionOfCurrent > 24)){

paintingsTableObj.style.left = - (positionOfCurrent - 10);}
else if(positionOfCurrent > 24) {
paintingsTableObj.style.left = scrollerWidth-(rightCorner - 3);

}

}





function moveNow(dir,speed){

			getVariables();

			if (dir=="left" && (leftCorner < -1)){	
			currentScrollerPos = (paintingsTableObj.offsetLeft + speed) + "px";
				
			paintingsTableObj.style.left = currentScrollerPos;
			}
			else if ((dir=="left") && (leftCorner == -1 || leftCorner == 0 )){
			reset();
			}
			
			else if (dir=="right" && rightCorner > 414) {
				
			currentScrollerPos = (paintingsTableObj.offsetLeft - speed) + "px";
			paintingsTableObj.style.left = currentScrollerPos;
			}
			else if (dir=="right" && rightCorner <= 414) {
			paintingsTableObj.style.left = scrollerWidth-(tableWidth-5);
			// document.getElementById('rightScroller').style.border = "1px solid red";
			}
		
}

function clearScroller(){
if(currentlyMoving){
window.clearTimeout(currentlyMoving);}
}

function moveRight(){
	moveNow("right",speed);
	currentlyMoving = setTimeout("moveRight()",0);

}
function moveLeft(){
	moveNow("left",speed);
	currentlyMoving = setTimeout("moveLeft()",0);

}

function hiliteBorder(thisDiv){
thisDiv.style.borderColor="#0022b7";
}
function unHiliteBorder(thisDiv){
if (thisDiv.id == "currentPainting")
{thisDiv.style.borderColor="#658fcc";}
else
{thisDiv.style.borderColor="#000066";}
}

