//Defines "Site" variable.
var Site = {
	/* START FUNCTION. SETS UP VARIABLES. LAUNCHES PARSER. */
	start: function(){
		if ($('kwick')){ 	//Code is only run if Kwick elements are present on page.
			var kwicks = $$('#kwick .kwick');	//Kwick elements loaded into "kwicks" array.
			var fx = new Fx.Elements(kwicks, {wait: false, duration: 200, transition: Fx.Transitions.quadOut});	//Effects defined...
			var current = $$('#kwick .current');	//Element marked with "current" class identified.

			fx.start(Site.setCurrent(kwicks, current)); //Sets width of currently displayed Kwick.
			Site.parseKwicks(kwicks, fx, current);	    //Sets up "mousenter" and "mouseleave" behaviour.
		}
	},
	
	/* PARSES KWICKS. SETS UP MOUSE-OVER ACTION. */
	parseKwicks: function(kwicks, fx, current){
		//Iterates through all Kwicks.
		kwicks.each(function(kwick, i){										
			//Sets moused-over Kwick behaviour (increase width).
			kwick.addEvent('mouseenter', function(e){							
				var obj = {}; 						
				obj[i] = {						
					'width': [kwick.getStyle('width').toInt(), 205] 
				};
				
				//Decreases width of unselected Kwicks.
				kwicks.each(function(other, j){
					if (other != kwick){
						var w = other.getStyle('width').toInt();
						if (w != 105) obj[j] = {'width': [w, 105]};
					}
				});
				fx.start(obj);
			});
		});
		
		//CALL TO "setCurrent" WHEN MOUSE INTERACTION CEASES. 
		$('kwick').addEvent('mouseleave', function(e){
			var obj = Site.setCurrent(kwicks, current);
			fx.start(obj);
		});

	},	
	
	/* ENSURES THAT CURRENT LINK IS ALWAYS LEFT ENLARGED POST INTERACTION */
	setCurrent: function(kwicks, current){
		var obj = {};
		if (current != ""){
			kwicks.each(function(kwick, i){
				if (kwick == current[0]){
					obj[i] = {'width': [kwick.getStyle('width').toInt(), 205]};
				}
				else {
					obj[i] = {'width': [kwick.getStyle('width').toInt(), 105]};
				}
			});
		}

		else {
			kwicks.each(function(other, j){
				obj[j] = {'width': [other.getStyle('width').toInt(), 125]};
			});
		}			
		return obj;
	}
};

//When page is loaded, Site "start" function is called.
window.addEvent('load', Site.start);