// JavaScript Document
/**************************************************
 *
 *	Objects used to dynamically build the Our Work, 
 *	Testemonials, Case Studies, and Contact Us pages
 *	for use with the Lightbox
 *
 **************************************************/
var LightboxItem = Class.create({
	client : null,				//Client's name
	project : null,				//Type of project
	description : null,			//Description of work done
	website : null,				//URL to related website
	display_heading : true,		//Flag to indicate whether the headings should be displayed or not true/false - Default is true displays headings
	thumbnail_over : null,		//Path to thumbnail over image
	thumbnail_out : null,		//Path to thumbnail out image
	main_image : null,			//Path to the maing image to be displayed
	large_image : null,			//Path to the larger view image
	alternate_images : [],		//Array of alternate images
	related_links : [],			//Array of LinkedItems
	
	initialize: function(options) {
		this.client = (options.client != undefined)?options.client : null;
		this.project = (options.project != undefined)?options.project : null;
		this.description = (options.description != undefined)?options.description : null;
		this.website = (options.website != undefined)?options.website : null;
		this.display_heading = (options.display_heading != undefined)?options.display_heading : true;
		this.thumbnail_over = (options.thumbnail_over != undefined)?options.thumbnail_over : null;
		this.thumbnail_out = (options.thumbnail_out != undefined)?options.thumbnail_out : null;
		this.main_image = (options.main_image != undefined)?options.main_image : null;
		this.large_image = (options.large_image != undefined)?options.large_image : null;
		if(options.alternate_images != undefined) {
			this.alternate_images = options.alternate_images;
		}
		if(options.related_links != undefined) {
			this.related_links = options.related_links;
		}
	},
	
	getCaption: function() {
		var cap = '';
		if(this.client != null && this.client != '') {
			if(this.display_heading == true) cap += '<span class="PicClient">Client: </span>';
			cap += this.client+'<br />';
		}
		if(this.project != null && this.project != '') {
			if(this.display_heading == true) cap += '<span class="PicProject">Project: </span>';
			cap += this.project+'<br />';
		}
		return cap;
	}
});

function initLightboxItems() {
	buildGallery();
	buildThumbnails();
	buildAlternates();
	buildRelated();
	var id = retrieveQSVar("id");
	id = (id == "")? 0 : id;
	overImage(id);
}

function overImage(id) {
	outAll();
	$('image'+id).src = (items[id].thumbnail_over != null)?items[id].thumbnail_over : "images/spacer.gif";
	$('main_img').src = (items[id].main_image != null)?items[id].main_image : "images/spacer.gif";
	
	if(items[id].client == null) $('tr_client').hide();
	else $('tr_client').show();
	$('td_client').innerHTML = items[id].client;
	
	if(items[id].project == null) $('tr_project').hide();
	else $('tr_project').show();
	$('td_project').innerHTML = items[id].project;
	
	if(items[id].description == null) $('tr_description').hide();
	else $('tr_description').show();
	$('td_description').innerHTML = items[id].description;
	
	if(items[id].website == null) $('tr_web_link').hide();
	else $('tr_web_link').show();
	$('td_web_link').innerHTML = '<a href="'+items[id].website+'" target="_blank">'+items[id].website+'</a>';

	if(items[id].display_heading == true) {
		$('th_client').innerHTML = "Client:";
		$('th_project').innerHTML = "Project:";
		$('th_description').innerHTML = "Description:";
		$('th_web_link').innerHTML = "Web Site:";
	}
	else {
		$('th_client').innerHTML = "&nbsp;";
		$('th_project').innerHTML = "&nbsp;";
		$('th_description').innerHTML = "&nbsp;";
		$('th_web_link').innerHTML = "&nbsp;:";
	}
	
	$('a_'+id).show();
	
	if(items[id].alternate_images.length > 0) {
		$('alt_heading').show();
		$('alt_views').show();
		$('alt_'+id).show();
	}

	if(items[id].related_links.length > 0) {
		$('tr_rel_links').show();
		$('rel_links').show();
		$('rel_link_'+id).show();
	}
}

function outAll() {
	$('alt_heading').hide();
	$('alt_views').hide();
	$('tr_rel_links').hide();
	$('rel_links').hide();
	for(var i = 0; i < items.length; i++) {
		$('image'+i).src = items[i].thumbnail_out;
		$('a_'+i).hide();
		if(items[i].alternate_images.length > 0) $('alt_'+i).hide();
		if(items[i].related_links.length > 0) $('rel_link_'+i).hide();
	}
}

function buildGallery() {
	var html = '';
	for(var i = 0; i < items.length; i++) {
		html += '<a href="'+items[i].large_image+'" rel="lightbox[phototour]" id="a_'+i+'" style="display:none">View Larger Image</a>';//
	}
	$('larger').innerHTML = html;
}

function buildThumbnails() {
	var objThumbnails = $('thumbnails');
	for(var i=0; i < items.length; i++) {		
		var objThumbnail = document.createElement('a');
		objThumbnail.setAttribute('id','thumbnail('+i+')');
		objThumbnail.setAttribute('href',items[i].large_image);
		objThumbnail.setAttribute('rel','lightbox[phototour_main]');
		objThumbnail.innerHTML = '<img id="image'+i+'" src="'+items[i].thumbnail_out+'" width="40" height="40" style="border:1px solid #999;"  />';
		objThumbnail.style.paddingRight = '4px';
		objThumbnails.appendChild(objThumbnail);
		Event.observe(objThumbnail,'mouseover',overImage.bind(this,i));
	}
}

function buildAlternates() {
	var html = '';
	for(var i = 0; i < items.length; i++) {
		var j = 0;
		for(j = 0; j < items[i].alternate_images.length; j++) {
			if(j != 0) html += "&nbsp;|&nbsp;";
			else html += '<span id="alt_'+i+'" style="display:none">';
			html += '<a href="'+items[i].alternate_images[j]+'" rel="lightbox[alternate_'+i+']">'+(j+1)+'</a>';
		}
		if(j > 0) html += '</span>';
	}
	$('alt_views').innerHTML = html;
}

function buildRelated() {
	var html = '';
	for(var i = 0; i < items.length; i++) {
		var j = 0
		for(j = 0; j < items[i].related_links.length; j++) {
			if(j != 0) html += "<br />";
			else html += '<span id="rel_link_'+i+'" style="display:none">';
			if(items[i].related_links[j].page == "casestudies.html") 
				html += '<a href="'+items[i].related_links[j].page+'#'+items[i].related_links[j].id+'">+Case Studies</a>';
			else if(items[i].related_links[j].page == "testimonials.html") 
				html += '<a href="'+items[i].related_links[j].page+'#'+items[i].related_links[j].id+'">+Testimonials</a>';
			else
				html += '<a href="'+items[i].related_links[j].page+'?id='+items[i].related_links[j].id+'">+Related Projects: '+items[i].related_links[j].title+'</a>';
		}
		if(j > 0) html += '</span>';
	}
	$('rel_links').innerHTML = html;
}

function retrieveQSVar(qsVar) {
	var qs = window.location.search.substring(1);
	var qsVars;
	var ret = "";
	
	qsVars = qs.split("&");
	for(var i=0; i< qsVars.length; i++)
	{
		keyValue = qsVars[i].split("=");
		if(keyValue[0] == qsVar) {
			ret = keyValue[1];
			break;
		}
	}
	return ret;
}

