//
//	Configuration
//
var fileLoadingImage = "./templates/default/images/loading.gif";
// -----------------------------------------------------------------------------------

//
//	Global Variables
//

var contentLinks = new Array;
var itemArray = new Array;

var target;
var myContent;

// -----------------------------------------------------------------------------------

//
//	Additional methods for Element added by SU, Couloir
//	- further additions by Lokesh Dhakar (huddletogether.com)
//
Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setLeft: function(element,l) {
	   	element = $(element);
    	element.style.left = l +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	},
	setClass: function(element,cssClass) {
		element = $(element);
		element.className = cssClass;
	}
});

// -----------------------------------------------------------------------------------

//
//	Extending built-in Array object
//	- array.removeDuplicates()
//	- array.empty()
//
Array.prototype.removeDuplicates = function () {
    for(i = 0; i < this.length; i++){
        for(j = this.length-1; j>i; j--){        
            if(this[i][0] == this[j][0]){
                this.splice(j,1);
            }
        }
    }
}

// -----------------------------------------------------------------------------------

Array.prototype.empty = function () {
	for(i = 0; i <= this.length; i++){
		this.shift();
	}
}

// -----------------------------------------------------------------------------------

//
//	Content Class Declaration
//	- initialize()
//	- start()
//  - updateMenuItems
//  - changeContent
//  - ajaxHistoryRequest
//  - showContent
//
var Content = Class.create();

Content.prototype = {
	
	// initialize()
	// Constructor runs on completion of the DOM loading. Calls updateImageList and then
	// the function inserts html at the bottom of the page which is used to display the shadow 
	// overlay and the image container.
	//
	initialize: function() {
		target = $('content');
		
		var objStartLink = $('home');
		
		this.start(objStartLink);
		this.updateMenuItems();
	},
	
	//
	// updateEditButtons()
	// Loops through anchor tags looking for 'lightbox' references and applies onclick
	// events to appropriate links. You can rerun after dynamically adding images w/ajax.
	//
	updateMenuItems: function() {	
		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName('a');

		// loop through all anchor tags
		for (var i=0; i < anchors.length; i++) {
			var anchor = anchors[i];
			
			var relAttribute = String(anchor.getAttribute('rel'));
			
			// use the string.match() method to catch 'lightbox' references in the rel attribute
			if (anchor.getAttribute('title') && (relAttribute.toLowerCase().match('content'))) {
				anchor.onclick = function () { myContent.start(this); return false; }
			}
		}
	},
	
	//
	//	start()
	//	Display overlay and lightbox. If image is part of a set, add siblings to imageArray.
	//
	start: function(itemLink) {	
		itemArray = [];		
				
		// if image is NOT part of a set..
		if((itemLink.getAttribute('rel') == 'content') && (itemLink.getAttribute('title') != '')){
			// add single contentItem to itemArray
			itemArray.push(new Array(itemLink.getAttribute('href'), itemLink.getAttribute('title')));			
		}
		
		this.changeContent(itemArray[0][0], itemArray[0][1]);
	},
	
	//
	//	changeImage()
	//	Hide most elements and preload image in preparation for resizing image container.
	//
	changeContent: function(url, title) {		
		//this.ajaxHistoryRequest(url, id);
		var myAjax = new Ajax.Updater(target, url, { method: 'get', encoding: 'windows-1252', onSucces: this.showContent(target, title) });	
		//var myAjax = new Ajax.Request(url, { method: 'get', encoding: 'windows-1252', onSucces: this.showContent(target, title) });
	},
	
	ajaxHistoryRequest: function(url, title) {
    	var myAjax = new Ajax.History.Updater(target, url, {
	        history : {
	            state : title,
	            cache : false,
	            onStateChange: function(state) {
	                History.setTitle(History.getTitle() + ' ' + state);
	            }
	        },
	        onComplete: this.showContent(target, myState)
    	});
	},	
	
	//
	//	showContent()
	//
	showContent: function(target, id) {
		this.updateContentLinks();
		this.setMenu(id);
	},
	
	//
	// updateEditButtons()
	// Loops through anchor tags looking for 'lightbox' references and applies onclick
	// events to appropriate links. You can rerun after dynamically adding images w/ajax.
	//
	updateContentLinks: function() {	
		if (!target.getElementsByTagName){ return; }
		var anchors = target.getElementsByTagName('a');

		// loop through all anchor tags
		for (var i=0; i < anchors.length; i++) {
			var anchor = anchors[i];
			
			var relAttribute = String(anchor.getAttribute('rel'));
			
			// use the string.match() method to catch 'lightbox' references in the rel attribute
			if (anchor.getAttribute('title') && (relAttribute.toLowerCase().match('content'))) {
				anchor.onclick = function () { myContent.start(this); return false; }
			}
		}
	},
	
	setMenu: function(activemenu) {
		if (!document.getElementsByTagName){ return; }
		var listItems = document.getElementsByTagName('li');

		// loop through all anchor tags
		for (var i=0; i < listItems.length; i++) {
			var listItem = listItems[i];
			
			// use the string.match() method to catch 'lightbox' references in the rel attribute
			if (listItem.parentNode.id == 'siteMenu' && (listItem.className != 'bypass')) {
				listItem.className = '';
			}
		}
		Element.setClass($(activemenu).parentNode, 'active');
	}
}

// -----------------------------------------------------------------------------------

Event.observe(window, 'load', init, false);

function init() {
	myContent = new Content();
}