var currentProduct = null;
var currentRider = null;
var showProductTimer = null;
var lock = false;
var lastShown = false;
var mainPhoto = null;
var currentImage = 0;

function showProduct(id) {
	// We are going to show the product (id) in 400ms
	currentProduct = id;
	showProductTimer = setTimeout('showProductHelper()', 400);
}

function dontShowProduct(id) {
	if(currentProduct == id) {
		if(showProductTimer != null) {
			clearTimeout(showProductTimer);
			showProductTimer = null;
		}
		currentProduct = null;
	}
}

function showProductHelper() {
	if(!lock) {
		lock = true;
		if(currentProduct != null) {
			getDiv("productDiv").innerHTML = '<table width=100% height=100%><td align=middle><img src="GTTemplates/images/loading.gif"></img></td></table>';
			lastShown = currentProduct;
			url = document.getElementsByTagName('base')[0].href + 'default.aspx?fuseaction=product.ajax&id=' + currentProduct;
			ajaxPage(url, 'showResults');
			
		}
	}
}

function showProductNow(id) {
	window.location.hash = id;
	currentProduct = id;
	showProductHelper();
}

function showRiderNow(id) {
	window.location.hash = id;
	currentRider = id;
	showRiderHelper();
}

function showRiderHelper() {
	if(!lock) {
		lock = true;
		if(currentRider != null) {
			getDiv("productDiv").innerHTML = '<table width=100% height=100%><td align=middle><img src="GTTemplates/images/loading.gif"></img></td></table>';
			lastShown = currentRider;
			ajaxPage('default.aspx?fuseaction=team.ajaxRider&id=' + currentRider, 'showResults');
		}
	}
}

function showResults(data) {
	lock = false;
	getDiv("productDiv").innerHTML = data;
	mainPhoto = getDiv("myphotoDiv");
	
	
	setTimeout('YAHOO.lightBox.start.init();', 1000);
	//YAHOO.lightBox.start();
	//YAHOO.lightBox.start);
	setTimeout('addReflections()', 1000);

	//CSBfleXcroll("mainProductDiv");
	
}

function showPrevProduct() {
	var index = getCurrentProduct();
	if(index > 0) {
		showProduct(productIDs[index-1]);
	}
}

function showNextProduct() {
	var index = getCurrentProduct();
	if(index < productIDs.length-1) {
		showProduct(productIDs[index+1]);
	}
}

function getCurrentProduct() {
	for(var i = 0; i < productIDs.length; i++) {
		if(productIDs[i] == lastShown) {
			return i;
		}
	}
	
	return -1;
}

function showPrevRider() {
	var index = getCurrentRider();
	if(index > 0) {
		showRiderNow(riderIDs[index-1]);
	}
}

function showNextRider() {
	var index = getCurrentRider();
	if(index < riderIDs.length-1) {
		showRiderNow(riderIDs[index+1]);
	}
}

function getCurrentRider() {
	for(var i = 0; i < riderIDs.length; i++) {
		if(riderIDs[i] == lastShown) {
			return i;
		}
	}
	
	return -1;
}


function addReflection(element) {
	element.className = "reflect rheight8";
	Reflection.addReflection(element);
	//setTimeout("addReflections();", 200);
}

function showPopupImage() {
	// Show the current image in the lightbox
	YAHOO.lightBox.start.startIt(currentImage);
}

function removeReflection(element) {
	Reflection.remove(element);
}

var scroller = false;

function startScrolling(e) {
	scroller = true;
	getDiv("mainScrollerProductDiv2").scrollTop = findPos(getDiv("parentScrollerProductDiv"))[1] - findPos(getDiv("mainScrollerProductDiv"))[1];
}

function finishScrolling(e) {
	scroller = false;
	getDiv("mainScrollerProductDiv2").scrollTop = findPos(getDiv("parentScrollerProductDiv"))[1] - findPos(getDiv("mainScrollerProductDiv"))[1];
}

function scroll(e) {
	if(scroller) {
		getDiv("mainScrollerProductDiv2").scrollTop = findPos(getDiv("parentScrollerProductDiv"))[1] - findPos(getDiv("mainScrollerProductDiv"))[1];
	}
}

function forcescroll(e) {
	getDiv("mainScrollerProductDiv2").scrollTop = getDiv("parentScrollerProductDiv").scrollTop;
	//getDiv("mainScrollerProductDiv2").scrollTop = findPos(getDiv("parentScrollerProductDiv"))[1] - findPos(getDiv("mainScrollerProductDiv"))[1];
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function showCaption(element) {
	caption.style.visibility = 'visible';
	caption.style.position = 'absolute';
	
	position = findPos(element);
	
	caption.style.left = '200';
	caption.style.top = position[1]+20;
}

function hideCaption() {
	caption.style.visibility = 'hidden';
}

function myprint() {
	try {
		document.myframe.focus();
		document.myframe.print(); 
	}
	catch(e) {
		window.parent.myframe.focus();
		window.print();
	}
}

function showPrinterFriendly(id) {
	
	var html = '<table width=100%><tr><td align=middle><table width="850"><td align=right><span class="fakelink_white" onclick="myprint();">Print</span> | <span class="fakelink_white" onclick="closeEmailPopup();">Close</span></td></table></td></tr><tr><td align=middle><iframe id="myframe" name="myframe" src="default.aspx?fuseaction=product.printerFriendly&id=' + id + '" width=850px height=400px></iframe></table>';
	setPopupFrame(html);
}

function showRiderBlog(id) {
	
    var html = '<table width=100%>' +
                '<tr><td align=middle>' +
                '<table width=680px>' +
                '<td align=right>' +
                '<span class="fakelink_white" onclick="closeEmailPopup();">Close</span>' +
                '</td>' +
                '</table>' +
                '</td></tr>' +
                '<tr><td align=middle >' +
                '<iframe id="myframe" name="myframe" src="riderblog&id=' + id + '" width=680px height=400px></iframe>' +
                '</td></tr></table>';
	setPopupFrame(html);
}

function setPopupFrame(innerhtml)
{
	hideSelectBoxes();
	hideFlash();
	
	var arrayPageSize = getPageSize();
	Element.setWidth('overlay', arrayPageSize[0]);
	Element.setHeight('overlay', arrayPageSize[1]);
	
	// Popup a dialog to get the where from
	new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.8 });

	// calculate top and left offset for the lightbox 
	var arrayPageScroll = getPageScroll();
	var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
	var lightboxLeft = arrayPageScroll[0];

	getDiv("popupFrame").innerHTML = innerhtml;
	
	Element.setTop('popupFrame', lightboxTop);
	Element.setLeft('popupFrame', lightboxLeft);
	Element.show('popupFrame');	
}


function closeEmailPopup() {
	getDiv("popupFrame").innerHTML = "";
	
	Element.hide('popupFrame');
	new Effect.Fade('overlay', { duration: overlayDuration});
	showSelectBoxes();
	showFlash();
}