/*
 * name : mainscript.js
 * desc : contains js controls for sliding panels 
 *	  and image navigation / arrows 
 * by   : willie goosen
 * date : 01/02/2006
 *
 */
 
 
// preload sites structural images
// these need preloading before the site displays
var siteImages = new Array();
function preLoadSiteImages(){
	//site image names
	sin = [	"wg/images/home_nav_button_small.gif",
		"wg/images/content_panel_body.gif",
		"wg/images/content_panel_side.gif",
		"wg/images/SiteLogo.jpg",
		"wg/images/hp_photos/20091029_DSC_6978 - Version 3.jpg",
		"wg/images/next_arrow.gif",
		"wg/images/back_arrow.gif"];
	for (x=0; x < sin.length; x++){
		siteImages[x] = new Image();
		siteImages[x].src = sin[x];
	}
}
 
var panels = new Array();
var exists;

function initPanels(){
	panels[0] = new fx.FadeSize('portfolio', {duration: 400});
	panels[1] = new fx.FadeSize('about', {duration: 400});
	panels[2] = new fx.FadeSize('links', {duration: 400});
	panels[3] = new fx.FadeSize('contact', {duration: 400});

	var divs = document.getElementsByClassName("stretcher"); 	// pannels sliding
	for (var i = 0; i < divs.length; i++){
		div = divs[i];
 
		// if there is a #divname in the url dont hide it
		if (window.location.href.indexOf(div.id +"") < 0) {
			panels[i].hide(div, 'width');
			if (exists != true) exists = false;
		}else{
			exists = true;
		}
	}
 		if (exists == false) panels[0].toggle('width');
}
 
function slidePanels(panel, allPanels){
 
	var mode = "width";
	var delay = 400;
 	for (var i = 0; i < allPanels.length; i++){

		if (allPanels[i].el.offsetWidth > 0 && allPanels[i].el != panel.el && allPanels[i].el.w.timer == null && panel.el.w.timer == null){
			allPanels[i].toggle(mode);
			setTimeout(function(){panel.toggle(mode);}.bind(panel), delay);
		}
	}
}
 
 
var thePhotos = new Array();
var thePhotoUrls= new Array();
var thumbs;
var hpPhoto;
var imgId = 0; // startup img
function initImages(){

	//usefull vars
	thumbs = document.getElementById("thumbs");
	hpPhoto = document.getElementById("hp_photo");
 
	//the actual photos the site
	thePhotoUrls = [
			"wg/images/hp_photos/20091029_DSC_6978 - Version 3.jpg",
//			"wg/images/hp_photos/20100416_DSC_7914.jpg",
			"wg/images/hp_photos/20100416_DSC_7874 - Version 2.jpg",
//			"wg/images/hp_photos/20010101_DSC_4034 - Version 2.jpg",
			"wg/images/hp_photos/HP_DSC_9065.jpg",
			"wg/images/hp_photos/HP_DSC_6669.jpg",
			"wg/images/hp_photos/HP_DSC_4533.jpg",

			"wg/images/hp_photos/HP_DSC_4444.jpg",
			"wg/images/hp_photos/HP_DSC_2637.jpg",
			"wg/images/hp_photos/HP_DSC_2613.jpg",
			"wg/images/hp_photos/HP_DSC_2760.jpg",
			"wg/images/hp_photos/HP_DSC_2699.jpg",

			"wg/images/hp_photos/HP_DSC_2824.jpg",
			"wg/images/hp_photos/HP_DSC_3167.jpg",
			"wg/images/hp_photos/HP_DSC_2147.jpg",
			"wg/images/hp_photos/HP_DSC_1243.jpg",
			"wg/images/hp_photos/HP_DSC_1024.jpg",

			"wg/images/hp_photos/HP_DSC_0794.jpg",
			"wg/images/hp_photos/HP_DSC_2206.jpg",
			"wg/images/hp_photos/HP_DSC_0688.jpg",
			"wg/images/hp_photos/HP_DSC_0685.jpg",
			"wg/images/hp_photos/HP_DSC_0502.jpg"  ];
 
	for (var j=0;j<thePhotoUrls.length;j++){

		thePhotos[j] = new Image();
		thePhotos[j].className = "tn_photo";
		thePhotos[j].src = thePhotoUrls[j];
		thePhotos[j].onclick=function(){selectImage(this)};
 
		thumbs.appendChild(thePhotos[j]);
	}
	//put the first photo on the homepage
	hpPhoto.src = thePhotoUrls[imgId];
 
	//add events to the arrows panels
	$("backArrowDiv").onmouseover=function(){showArrow('back')};
	$("backArrowDiv").onmouseout=function(){hideArrow('back')};
	$("backArrowDiv").onclick=function(){prevImage()};
 
	$("nextArrowDiv").onmouseover=function(){showArrow('next')};
	$("nextArrowDiv").onmouseout=function(){hideArrow('next')};
	$("nextArrowDiv").onclick=function(){nextImage()};
 
	checkArrows();
}
 
 
function showArrow(arrow){
	$(arrow+"ArrowImg").style.visibility = "visible";
	//arrow.firstChild.nextSibling.style.visibility = "visible";
}
 
function hideArrow(arrow){
	$(arrow+"ArrowImg").style.visibility = "hidden";
	//arrow.firstChild.nextSibling.style.visibility = "hidden";
}
 
function nextImage(){
	// find current hpPhoto in thePhotos
	// its id + 1
	// change the homepage image
	// if its the last one in the list remove the arrow
 
	for (var j=0;j<thePhotos.length;j++){
		if (hpPhoto.src == thePhotos[j].src) {
			imgId = j;
		}
	}
	hpPhoto.src = thePhotoUrls[++imgId];
	checkArrows();
}
 
function prevImage(){
	// find current hpPhoto in thePhotos
	// its id - 1
	// change the homepage image
	// if its the first one in the list remove the arrow
 
	for (var j=0;j<thePhotos.length;j++){
		if (hpPhoto.src == thePhotos[j].src) {
			imgId = j;
		}
	}
 
	imgId = imgId-1;
	hpPhoto.src = thePhotoUrls[imgId];
	checkArrows();
 
}
 
function selectImage(img){
 
	// set imgId to that of the img just selected
	hpPhoto.src = img.src;
	checkArrows();
}
 
/*
	function checks to see if the hover over arrow
	should be displayed or not
*/
function checkArrows(){
 
	for (var j=0;j<thePhotos.length;j++){
		if (hpPhoto.src == thePhotos[j].src) {
			imgId = j;
		}
	}

	// this is the last one that was just displayed
	// so remove the next arrow
	if (imgId > thePhotoUrls.length-2){
		$("nextArrowDiv").style.display = "none";
	}else{ //it should be shown
		$("nextArrowDiv").style.display = "block";
		hideArrow('next');
	}
 
 	// this is the first one that was just displayed
 	// so remove the next arrow
 	if (imgId == 0){
 		$("backArrowDiv").style.display = "none";
 	}else{ // it should be shown
 		$("backArrowDiv").style.display = "block";
 		hideArrow('back');
 	}
 }