/*
 * Toyota Redesign Gallery
 * By Agus Daud
 * Hothouse 2008
 * Last Modified 22 January 2008
 */


var intervalId = 0;

function ImageGroup()
{
	this.title = '';
	this.xsmallURI = '';
	this.smallURI = '';
	this.mediumURI = '';
	this.largeURI = '';
	this.xlargeURI = '';
}

// Find the height of the viewport
function windowHeight() {
	// A shortcut, in case we're using Internet Explorer 6 in Strict Mode
	var de = document.documentElement;
	// If the innerHeight of the browser is available, use that
	return self.innerHeight ||
	// Otherwise, try to get the height off of the root node
	( de && de.clientHeight ) ||
	// Finally, try to get the height off of the body element
	document.body.clientHeight;
}
		
function adjust() {
	
	var position = "";
	
	if (navigator.appName == "Microsoft Internet Explorer"){
		position = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
	} else {
		position = window.pageYOffset;
	}
	
	winInnerHeight = (document.documentElement.clientHeight - $('#gallery_popup').height())/2;
	
	var leftPopup = Math.floor(getAbsoluteLeft('content_navPage')+17);
	var topPopup = Math.floor(winInnerHeight + position);
	
	$('#gallery_popup').css({left: leftPopup+"px", top: topPopup+"px"});
	
}

function slideshowOff() {

	clearInterval(intervalId);
	$('#popupSlideShow').attr("class","btn-slideshow");	

}
		
function windowPrint() {
	
	slideshowOff();
	window.print();
	
}

function slideShowPopup() {
	
	if (orderNumberPopup >= (imageGroups.length-1)) {
		orderNumberPopup = 0;
	} else {
		orderNumberPopup++;
	}
	
	changePopupImage(orderNumberPopup);
	//interval = Math.floor((imageGroups.length - 7) * 90 / imageGroups.length);
	var widthDiv = orderNumberPopup * interval;
	
	//$('#gp_thumbnails').animate({scrollLeft: widthDiv}, speed);
	
	scrollBar = document.getElementById("gp_thumbnails");
	scrollBar.scrollLeft = widthDiv;
}

function nextImagePopup() {
	
	slideshowOff();
	//alert(imageGroups.length);
	if (orderNumberPopup >= (imageGroups.length-1)) {
		//Do nothing MAX Number reach already
		orderNumberPopup = 0;
	} else {
		orderNumberPopup++;
	}
	changePopupImage(orderNumberPopup);
	
	var widthDiv = orderNumberPopup * interval;
	
	//$('#gp_thumbnails').animate({scrollLeft: widthDiv}, speed);
	
	scrollBar = document.getElementById("gp_thumbnails");
	scrollBar.scrollLeft = widthDiv;

}
		
function prevImagePopup() {
	
	slideshowOff();
	//alert(imageGroups.length);
	if (orderNumberPopup <= 0) {
		//Do nothing MAX Number reach already
		orderNumberPopup = (imageGroups.length-1);
	} else {
		orderNumberPopup--;
	}
	changePopupImage(orderNumberPopup);
	
	var widthDiv = orderNumberPopup * interval;
	
	//$('#gp_thumbnails').animate({scrollLeft: widthDiv}, speed);
	scrollBar = document.getElementById("gp_thumbnails");
	scrollBar.scrollLeft = widthDiv;

}

function nextImage() {

	//alert(imageGroups.length);
	if (orderNumber >= (imageGroups.length-1)) {
		//Do nothing MAX Number reach already
	} else {
		orderNumber++;
	}
	changeMainImage(imageGroups[orderNumber].xlargeURI, imageGroups[orderNumber].largeURI, imageGroups[orderNumber].largeAltText, orderNumber);	
	
}

function prevImage() {

	//alert(imageGroups.length);
	if (orderNumber <= 0) {
		//Do nothing MAX Number reach already
	} else {
		orderNumber--;
	}
	changeMainImage(imageGroups[orderNumber].xlargeURI, imageGroups[orderNumber].largeURI, imageGroups[orderNumber].largeAltText, orderNumber);	
	
}
		
function enlargeImage() {
	
	changePopupImage(orderNumber);
	
	adjust();
	
	var position = "";
	var winInnerHeight ="";
	
	if (navigator.appName == "Microsoft Internet Explorer"){
		position = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
		winInnerHeight = (document.documentElement.clientHeight - $('#gallery_popup').height())/2;
	} else {
		position = window.pageYOffset;
		winInnerHeight = (window.innerHeight - $('#gallery_popup').height())/2;
	}
	
	var leftPopup = Math.floor(getAbsoluteLeft('content_navPage')+17);
	var topPopup = Math.floor(winInnerHeight + position);
	
	$('.black_overlay').fadeIn("slow");
	$('#gallery_popup').css({left: leftPopup+"px", top: topPopup+"px"});
	$('#gallery_popup').fadeIn("slow");
	
	var widthDiv = orderNumberPopup * interval;
	//$('#gp_thumbnails').animate({scrollLeft: widthDiv}, speed);
	
	scrollBar = document.getElementById("gp_thumbnails");
	scrollBar.scrollLeft = widthDiv;
	
}

function closePopup() {
	slideshowOff();
	$('.black_overlay').fadeOut("slow");
	$('.gallery_popup').fadeOut("slow");
} 

function changePopupImage(orderNumPopup) {
	
	orderNumberPopup = orderNumPopup;
	
	$('.thumbnails-list li a').attr("class","linkImg");
	$('.thumbnails-list li a:eq('+orderNumberPopup+')').attr("class","selected");
	
	$('.gp_header_title').html(imageGroups[orderNumberPopup].title);
	$('.gp_main_image img').hide();
	$('.gp_main_image img').attr("src",imageGroups[orderNumberPopup].xlargeURI);
	$('.gp_main_image img').attr("alt",imageGroups[orderNumberPopup].xlargeAltText);
	//$('.gp_main_image img').fadeIn("slow");
	
}

function changeMainImage(linkURI, imageURI, altText, orderNum)
{
	orderNumber = orderNum;
	
	$('.sideInfo').html(imageGroups[orderNum].title);
	$('#divMainImage img').hide();
	$('#divMainImage img').attr("src",imageURI);
	$('#divMainImage img').attr("alt",altText);
	//$('#divMainImage img').fadeIn("slow");
	
	$('#thumbnails a').attr("class","linkImg");
	$('#thumbnails a:eq('+orderNum+')').attr("class","linkImgSelected");
	
}

function fade() {
	$("#picture").fadeIn("600");
} 

function fadePopup() {
	$("#picturePopup").fadeIn("600");
} 


