// Script Library 01 for the John Archer Design Website - www.johnarcher.co.uk
// copyright Robert Fisher, 21 Sept 2006.
// You may copy this code but please keep the copyright notice as well.

function populate() {
	//Sets up the all thumbs and images on the page in the startup positions.
	
	
	if (GLOBAL_thispage=="about") {
		swapimage('lightbox', "images/icon_about.gif")
	}
	else if (GLOBAL_thispage=="contact") {
		swapimage('lightbox', "images/icon_contact.gif")
	}
	else if (GLOBAL_thispage=="prices") {
		swapimage('lightbox', "images/icon_prices.gif")
	}
	else {
		image_preloader(thumbimages); // Preload all needed images on page.
		image_preloader(navimages); // Preload all needed images on page.
		var tempimg ='';
		for(i=0; i<=9; i++){
			tempimg = thumbimages[i];
			if (tempimg == undefined) {tempimg='images/blank_thumb.gif'}
			swapimage('thumb'+(i+1), tempimg);
		} //Loops through thumb lists and displays them 1 by 1.
		swapimage('lightbox', images[0]); // Sets the initial focus for the lightbox.
		image_preloader(fullimages); // Preload all needed images on page.
			
		//Set text elements:  - start positions
		set_elementnode_value('desc_box_iname', images_name[0]);
		set_elementnode_value('desc_box_ptype', images_project[0]);
		set_elementnode_value('desc_box_cname', images_client[0]);
		set_elementnode_value('desc_box_notes1', images_notes1[0]);
		set_elementnode_value('desc_box_notes2', images_notes2[0]);			
	}
	
	
	//Setup highlighed title navigation images:
	var navobjID = "";
	
	// First the top nav bar:
	if (GLOBAL_thispage=="about") {navobjID = "nav_about";}
	else if (GLOBAL_thispage=="contact") {navobjID = "nav_contact";}
	else if (GLOBAL_thispage=="prices") {navobjID = "nav_prices";}
	else if (GLOBAL_thispage=="illustration") {navobjID = "nav_port";}
	else if (GLOBAL_thispage=="advertising") {navobjID = "nav_port";}	
	else if (GLOBAL_thispage=="web") {navobjID = "nav_port";}
	else if (GLOBAL_thispage=="print") {navobjID = "nav_port";}	
	else if (GLOBAL_thispage=="logos") {navobjID = "nav_port";}	
	var imgsrc = getimagesrc(navobjID);
	var dotpos = imgsrc.lastIndexOf('.');
	var tempfilename = imgsrc.substring(0,dotpos);
	var tempfiletype = imgsrc.substring(dotpos,imgsrc.length);
	var newimgsrc = '';
	newimgsrc = tempfilename + '_m' + tempfiletype;
	
//	alert(newimgsrc)
	
	swapimage(navobjID, newimgsrc);
	
	//Then the bottom potfolio nav bar:
	if (navobjID == 'nav_port') {
		if (GLOBAL_thispage=="illustration") {navobjID = "nav_port_illustration";}
		else if (GLOBAL_thispage=="advertising") {navobjID = "nav_port_advertising";}	
		else if (GLOBAL_thispage=="web") {navobjID = "nav_port_web";}
		else if (GLOBAL_thispage=="print") {navobjID = "nav_port_print";}	
		else if (GLOBAL_thispage=="logos") {navobjID = "nav_port_logos";}	
		var imgsrc = getimagesrc(navobjID);
		var dotpos = imgsrc.lastIndexOf('.');
		var tempfilename = imgsrc.substring(0,dotpos);
		var tempfiletype = imgsrc.substring(dotpos,imgsrc.length);
		var newimgsrc = '';
		newimgsrc = tempfilename + '_m' + tempfiletype;
		swapimage(navobjID, newimgsrc);
		setObjVis('portnav','visible');
	}
	else { 
		setObjVis('portnav','hidden');
	}
	
}

function getnavimages() {
		temp_array = new Array()
		temp_array[0] = "images/nav_about.gif";
		temp_array[1] = "images/nav_about_m.gif";
		temp_array[2] = "images/nav_contact.gif";
		temp_array[3] = "images/nav_contact_m.gif";
		temp_array[4] = "images/nav_portfolio.gif";
		temp_array[5] = "images/nav_portfolio_m.gif";						
		temp_array[6] = "images/nav_prices.gif";
		temp_array[7] = "images/nav_prices_m.gif";
		temp_array[8] = "images/nav_port_advertising.gif";
		temp_array[8] = "images/nav_port_advertising_m.gif";
		temp_array[9] = "images/nav_port_illustration.gif";
		temp_array[10] = "images/nav_port_illustration_m.gif";
		temp_array[11] = "images/nav_port_logos.gif";
		temp_array[12] = "images/nav_port_logos_m.gif";
		temp_array[13] = "images/nav_port_print.gif";
		temp_array[14] = "images/nav_port_print_m.gif";
		return temp_array;
}

function getthumbname(filename,moused) {
	// This function returns the filename of the thumb version for the current image. (+ _thumb)
	//If the string variable moused = "moused" then it will give the "_thumb_m" version.

	var dotpos = 0;
	var tempref = '';
	var tempfilename = '';
	var tempfiletype = '';
	var new_filename = '';
	tempref=filename;
	dotpos = tempref.indexOf('.');
	tempfilename = tempref.substring(0,dotpos);
	tempfiletype = tempref.substring(dotpos,tempref.length);
	new_filename = tempfilename + '_thumb';
			
	if (moused == 'moused') {
		new_filename = new_filename + '_m';
	}
	new_filename = new_filename + tempfiletype;
	
	return new_filename;
}

function setpageglobals() {
	images = getportdesc_array(GLOBAL_thispage,'filename');
	images_name = getportdesc_array(GLOBAL_thispage,'name');
	images_project = getportdesc_array(GLOBAL_thispage,'project');
	images_client = getportdesc_array(GLOBAL_thispage,'client');
	images_notes1 = getportdesc_array(GLOBAL_thispage,'notes1');
	images_notes2 = getportdesc_array(GLOBAL_thispage,'notes2');

	for(i=0; i<=images.length-1; i++){
		z++;
		if (images[i] == '') {tempref='images/blank.gif'}
		else {tempref = images[i];}
		dotpos = tempref.lastIndexOf('.');
		tempfilename = tempref.substring(0,dotpos);
		tempfiletype = tempref.substring(dotpos,tempref.length);
		fullimages[y] = tempfilename + '' + tempfiletype; y++;
		fullimages[y] = tempfilename + '_thumb' + tempfiletype;	
		thumbimages[t] = fullimages[y];	y++;t++;
		fullimages[y] = tempfilename + '_thumb_m' + tempfiletype;y++;
	}
	if (z<9) {
		for(i=(z+1); i<=9; i++){
			tempref='images/blank.gif';
			dotpos = tempref.indexOf('.');
			tempfilename = tempref.substring(0,dotpos);
			tempfiletype = tempref.substring(dotpos,tempref.length);
			fullimages[y] = tempfilename + '' + tempfiletype; y++;
			fullimages[y] = tempfilename + '_thumb' + tempfiletype;
			thumbimages[t] = fullimages[y];	y++;t++;
			fullimages[y] = tempfilename + '_thumb_m' + tempfiletype; y++;
		}
	}
	//Navigation buttons:
	navimages = new Array();
	navimages = getnavimages();		
}

function getfullname(filename) {
	//Returns the filename of the master/full image for a given main image, thumb or moused thumb.
		
	var tempref = '';
	var new_filename = '';
	var imgsrc = getimagesrc(objID);
	var dotpos = imgsrc.lastIndexOf('.');
	var tempfilename = imgsrc.substring(0,dotpos);
	var tempfiletype = imgsrc.substring(dotpos,imgsrc.length);
	var newimgsrc = '';
	var fullimgsrc = '';
			
	if (imgsrc.lastIndexOf('_m.') == -1) {
		//This image is not a mouseover.
		if (imgsrc.lastIndexOf('_thumb.') == -1) {
			fullimgsrc = tempfilename + '_full' + tempfiletype;				
		}
		else {
			fullimgsrc = tempfilename.substring(0,tempfilename.length-6) + '_full' + tempfiletype;
		}
	}
	else {
		//This image is a mouseover already.
		if (imgsrc.lastIndexOf('_thumb_') == -1) {
			fullimgsrc = tempfilename + '_full' + tempfiletype;
		}
		else {
			fullimgsrc = tempfilename.substring(0,tempfilename.length-8) + '_full' + tempfiletype;
		}
	}
			
	return fullimgsrc;
}

function nav_mo(objID) {
	//runs the mouseover action for the navigation buttons...
	
	if (GLOBAL_thispage == "illustration" && objID == "nav_port") {}       //Do nothing...		
	else if (GLOBAL_thispage == "advertising" && objID == "nav_port") {}  //Do nothing...		
	else if (GLOBAL_thispage == "logos" && objID == "nav_port") {}  //Do nothing...		
	else if (GLOBAL_thispage == "print" && objID == "nav_port") {}  //Do nothing...		
	else if (GLOBAL_thispage == "web" && objID == "nav_port") {}  //Do nothing...		

	else if (GLOBAL_thispage == "illustration" && objID == "nav_port_illustration") {}       //Do nothing...		
	else if (GLOBAL_thispage == "advertising" && objID == "nav_port_advertising") {}  //Do nothing...		
	else if (GLOBAL_thispage == "logos" && objID == "nav_port_logos") {}  //Do nothing...		
	else if (GLOBAL_thispage == "print" && objID == "nav_port_print") {}  //Do nothing...		
	else if (GLOBAL_thispage == "web" && objID == "nav_port_web") {}  //Do nothing...

	else if (GLOBAL_thispage == "about" && objID == "nav_about") {}  //Do nothing...		
	else if (GLOBAL_thispage == "contact" && objID == "nav_contact") {}  //Do nothing...				
	else if (GLOBAL_thispage == "prices" && objID == "nav_prices") {}  //Do nothing...		
	
	else {
		thumb_mo(objID);  //makes use of the thumb mouse over code.
		}
}

function nav_clk(objID) {
	if (objID =='nav_enter') {
		window.location = 'about.html';
	}
	else if (objID =='nav_about') {
		window.location = 'about.html';
	}
	else if (objID =='nav_prices') {
		window.location = 'prices.html';
	}
	else if (objID =='nav_contact') {
		window.location = 'contact.html';
	}
	else if (objID =='nav_port') {
		if (GLOBAL_thispage=='advertising') {
//			window.location = 'port_advertising.html';
		}
		else if (GLOBAL_thispage=='illustration') {
//			window.location = 'port_illustration.html';
		}
		else if (GLOBAL_thispage=='print') {
//			window.location = 'port_print.html';
		}
		else if (GLOBAL_thispage=='logos') {
//			window.location = 'port_logos.html';
		}
		else if (GLOBAL_thispage=='web') {
//			window.location = 'port_web.html';
		}		
		else {
			window.location = 'port_advertising.html';
		}
	}
	else if (objID =='nav_home') {
		window.location = 'index.html';			
	}
	else if (objID =='nav_port_advertising') {
		window.location = 'port_advertising.html';
	}
	else if (objID =='nav_port_web') {
		window.location = 'port_web.html';
	}
	else if (objID =='nav_port_illustration') {
		window.location = 'port_illustration.html';
	}
	else if (objID =='nav_port_print') {
		window.location = 'port_print.html';
	}
	else if (objID =='nav_port_logos') {
		window.location = 'port_logos.html';
	}
}

function thumb_mo(objID) {
	// Runs from the onmouseover event of the thumbs.
	var imgsrc = getimagesrc(objID);
	var dotpos = imgsrc.lastIndexOf('.');
	var tempfilename = imgsrc.substring(0,dotpos);
	var tempfiletype = imgsrc.substring(dotpos,imgsrc.length);
	var newimgsrc = '';
			
	if (imgsrc.lastIndexOf('_m.') == -1) {
		//This image is not a mouseover.
		newimgsrc = tempfilename + '_m' + tempfiletype;
	}
	else {
		//This image is a mouseover already.
		newimgsrc = tempfilename.substring(0,tempfilename.length-2) + tempfiletype;
	}
	
	swapimage(objID, newimgsrc);
}
			
function thumb_clk(objID) {
	// Runs from the onclick event of the thumbs.
	var imgsrc = getimagesrc(objID);
	var dotpos = imgsrc.lastIndexOf('.');
	var tempfilename = imgsrc.substring(0,dotpos);
	var tempfiletype = imgsrc.substring(dotpos,imgsrc.length);
	var newimgsrc = '';
	var fullimgsrc = '';
			
	if (imgsrc.lastIndexOf('_m.') == -1) {
		//This image is not a mouseover.
		newimgsrc = tempfilename.substring(0,tempfilename.length-6) + tempfiletype;
		fullimgsrc = tempfilename.substring(0,tempfilename.length-6) + '_full' + tempfiletype;
	}
	else {
		//This image is a mouseover already.
		newimgsrc = tempfilename.substring(0,tempfilename.length-8) + tempfiletype;
		fullimgsrc = tempfilename.substring(0,tempfilename.length-8) + '_full' + tempfiletype;
	}
			
	swapimage('lightbox', newimgsrc); //Replace the image.
			
	//Set the text:
	var count = 0;
	var brc_pos = 0;
	var tempsrc = '';

	set_elementnode_value('desc_box_iname', '');
	set_elementnode_value('desc_box_ptype', '');
	set_elementnode_value('desc_box_cname', '');
	set_elementnode_value('desc_box_notes1', '');
	set_elementnode_value('desc_box_notes2', '');
			
	for (count = 0; count <= images.length; count++) {
		brc_pos = newimgsrc.lastIndexOf('images/');
		tempsrc = newimgsrc.substring(brc_pos,newimgsrc.length);
		//alert('cur: ' + images[count] +'   new: ' + tempsrc );
		if (images[count] == tempsrc) {
			set_elementnode_value('desc_box_iname', images_name[count]);
			set_elementnode_value('desc_box_ptype', images_project[count]);
			set_elementnode_value('desc_box_cname', images_client[count]);
			set_elementnode_value('desc_box_notes1', images_notes1[count]);
			set_elementnode_value('desc_box_notes2', images_notes2[count]);
			break; //exits the for loop.
		}
	}
			
	imageObj = preloadimage(fullimgsrc);  // finally load the full image to memory ready - this is needed to get the height and width measurements in time.
}

function lightbox_clk(objID) {
	// Runs from the onclick event of the thumbs.
	
	if (GLOBAL_thispage=="about" || GLOBAL_thispage=="contact" || GLOBAL_thispage=="prices") {
		//no click event for non-portfolio pages
	}
	else {
		var imgsrc = getimagesrc(objID);
		var dotpos = imgsrc.lastIndexOf('.');
		var tempfilename = imgsrc.substring(0,dotpos);
		var tempfiletype = imgsrc.substring(dotpos,imgsrc.length);
		imgsrc = tempfilename + '_full' + tempfiletype;
		imageObj = new Image();
		imageObj = preloadimage(imgsrc);
		//alert(imageObj.height);		//DEBUG
			
		var h = 0;
		var w = 0; 
		var browsermod_h = 30;  //Needs modifying to add border height for ie only
		var browsermod_w = 20;  //Needs modifying to add border width for ie only
				
		if (imageObj.height == 0) {
			h = imageObj.height + 900;
			w = imageObj.width + 650;
		}
		else {
			h = imageObj.height + browsermod_h;
			w = imageObj.width + browsermod_w;
		}
	
		newWindow = window.open(imgsrc, "lightboxwindow", 'toolbar=0, menubar=0,resizable=0,height='+ h +',width='+ w +'') 
	}
}


