//Javascript used for Squareleg, aided by Mootools Library

function tabHandler(tablist,objectHandler) {		
	this.list			=	$$(tablist);
	this.activetab		=	null;
	this.tabs			=	new Array();
	this.tabsrota_count	=	0
	this.tabsrota_timer	=	null;
	this.launch			= function(obj){
		var currentActiveTab = Cookie.get("activetab");
		parentObj = this;
		parentObj.list.each(function(element,index) {				
			parentObj.tabs.push(element);
			if(index < 1){
				parentObj.activetab = element;
			}
			if(!window.ie6){
				//Don't make them move up and down for ie6 as it's buggy
				element.addEvents({
					'mouseenter' :  function(){
						eval(objectHandler+".mouseenter(element);");
					},	 
					'mouseleave' : function(){
						eval(objectHandler+".mouseleave(element);");
					}
				});
			}
			element.addEvents({
				'mousedown' : function(){
					eval(objectHandler+".mousedown(element);");
				}
			});

			element.getParent().setProperty("href","Javascript:void(0)");
		});
		// Set Active Tab
		if(currentActiveTab && currentActiveTab != "ptab01"){

			//Inactivate old tab
			this.disableTab($("ptab01").getFirst());
			
			//Activate new tab
			this.enableTab($(currentActiveTab).getFirst());
			this.activetab=$(currentActiveTab).getFirst();
			
		}
	}
	this.mouseenter = function(obj){
		var fx	= new Fx.Styles(obj, {duration:150, wait:false});
		fx.start({
			'margin-top' : 0,
			'padding-bottom' : 10
		});
	}
	this.mouseleave = function(obj){
		var fx	= new Fx.Styles(obj, {duration:150, wait:false});
		if(this.activetab != obj){
			fx.start({
				'margin-top' : 6,
				'padding-bottom' : 4
			});
		}
	}
	this.mousedown = function(obj,automated){
		if(this.activetab != obj){			
			var fx2					= new Fx.Styles(this.activetab, {duration:150, wait:false});
			var objectHandler		= this;
			var oldobj				= objectHandler.activetab;
			objectHandler.activetab = obj;
			fx2.start({
				'margin-top' : 6,
				'padding-bottom' : 4
			}).addEvent("onComplete",function(){

				//Inactivate old tab
				objectHandler.disableTab(oldobj);

				//Activate new tab
				objectHandler.enableTab(obj);

				if(!automated){
					$clear(objectHandler.tabrota_timer);
				}
			});
		}
	}

	this.disableTab = function(oldobj){
		objectHandlerP = oldobj.getParent();
		objectHandlerP.removeClass('selected');
		objectHandlerC = objectHandlerP.getProperty("id")+"c";
		$(objectHandlerC).addClass("hidden");
	}

	this.enableTab = function(obj){
		objP	= obj.getParent();
		objP.addClass("selected");
		objC	= objP.getProperty("id")+"c";
		$(objC).removeClass("hidden");

		// Setting Cookie
		Cookie.set('activetab', objP.getProperty("id"));

		if(window.ie6){
			obj.setStyles({
				'margin-top' : 0,
				'padding-bottom' : 10
			});
		}
	}

	this.startRotate = function(obj,milsec){
		this.doRotate();
		this.tabrota_timer = this.doRotate.periodical(milsec,this);
	}
	this.doRotate = function(){
		this.tabsrota_count += 1;
		this.mouseenter(this.tabs[this.tabsrota_count-1]);
		this.mousedown(this.tabs[this.tabsrota_count-1],true);
		if(this.tabsrota_count == this.tabs.length){
			this.tabsrota_count=0;
		}			
	}

	//Launch Object
	this.launch(this);
}

function slideShowHandler(id,imagesList,altsList){

	this.id					= id;
	this.imgid				= $(this.id).getElementsBySelector("img")[0];
	this.imagesList			= imagesList;
	this.altsList			= altsList;
	this.sliderota_count	= 1;
	this.sliderota_timer	= null;

	$(this.id).setStyles({
		"background-image"	: "url("+this.imgid.getProperty("src")+")",
		"height"			: this.imgid.getProperty("height")+"px",
		"width"				: this.imgid.getProperty("width")+"px"
	});
	this.imgid.setOpacity(0);			

	this.launch = function(milsec) {
		this.tabrota_timer = this.doRotate.periodical(milsec,this);
	}

	this.doRotate = function(){
		this.sliderota_count += 1;		
		var ni		= this.imagesList[this.sliderota_count-1];
		var na          = this.altsList[this.sliderota_count-1];
		var fx		= new Fx.Styles(this.imgid, {duration:500, wait:false});
		var id		= this.id;
		var imgid	= this.imgid;

		this.imgid.setProperty("src",ni);
		fx.start({
			"opacity" : 1
		}).addEvent("onComplete",function(){
				$(id).setStyle("background-image","url("+imgid.getProperty("src")+")");
				imgid.setOpacity(0);
		});
		imgid.setProperty("alt",na);
		imgid.setProperty("title",na);
		if(this.sliderota_count == this.imagesList.length){
			this.sliderota_count=0;
		}			
	}
}