/*
	eclipse-creative.com
	20100120	tomc
*/
/*!
	ACTIVITY BASKET CLIENT
*/
var ActivityBasket = new Class({
	Implements		:Options,
	options			:{
		seasons			:['winter','summer'],
		showSeason		:$H({
			"winter"	:true,
			"summer"	:true
		}),
		formEl			:false,
		seasonSwitchEl	:false
	},
	
	/*
		CONSTRUCTOR
	*/
	initialize		:function( options ){
		this.setOptions(options);
		
		if( !this.options.formEl || !this.options.seasonSwitchEl ){
			throw("missing elements");
			return;
		}
		
		// INIT SEASON STATUS //
		this.options.seasons.each(function(seasonName){
			this.options.showSeason.set(seasonName, true);
		},this);
		
		// INIT SEASON SWITCHING TAGS //
		this.options.seasonSwitchEl.getChildren("a").each(function(a){
			
			a.decideCheckbox = function( statusHash ){
				var enabled = true;
				this.getProperty("class").split(" ").each(function(c){
					if(statusHash.has(c)){
						if( !statusHash.get(c) ){
							enabled = false;
						}
					}
					if( img = this.getElement(".checkbox img") ) img.fade( enabled ? 1 : 0 );
					
				},this);
			}
			
			this.options.seasons.each(function(seasonName){
				if(a.hasClass(seasonName)){
					a.addEvent("click", this.toggleSeason.create({
						bind		:this,
						/*event		:true,*/
						arguments	:[seasonName]
					}));
				}
			},this);
			
		},this);
		
		// INIT FORM ELEMENTS //
		this.options.formEl.getElements("[name^=activityIds]").each(function(checkbox){
			//var checked 	= checkbox.getProperty("checked") ? true : false;
			//var activityId	= checkbox.getProperty("value");
			checkbox.setStyle("display","none");
			
			var div = new Element("div",{
				"class"	:"checkbox",
				html	:'<div class="checked" style="width:11px; height:11px; background:url('+rootPath+'grx/rf-layout/check-1-11x11.png) no-repeat;"></div>',
				events	:{
					click	:function(e){
						isChecked = checkbox.getProperty("checked") ? true : false;
						
						checkbox.setProperty("checked", !isChecked);
						this.getElement(".checked").fade( !isChecked ? 1 : 0 );
					}
				}
			}).inject(checkbox,"before");
			
			if( !checkbox.getProperty("checked") ){
				div.getElement(".checked").setStyle("opacity",0);
			}
		});
		
		this.options.formEl.getElements(".submit").addEvents({
			click:function(e){
				this.options.formEl.submit();
			}.bind(this)
		})
	},
	
	
	toggleSeason	:function(seasonName){
		var status = this.options.showSeason.get(seasonName); 
		this.options.showSeason.set(seasonName, !status ); 
		
		this.options.seasonSwitchEl.getChildren("a").each(function(a){
			a.decideCheckbox( this.options.showSeason );
		},this);
		
		this.refreshSeasons();
	},
	
	
	refreshSeasons	:function(){
		// SHOW TAKE PRIORITY OVER NOT-SHOW //
		this.options.showSeason.each(function(show, name){
			if( !show )	this.options.formEl.getElements("." + name).addClass("n");
		},this);
		this.options.showSeason.each(function(show, name){
			if( show )	this.options.formEl.getElements("." + name).removeClass("n");
		},this);
	},
	
	
	toggleActivity	:function(id){
		
	},
	
	
	checkout		:function(){
		// redirect?
	}
});

/*
	ENTRY
*/
window.addEvent("domready",function(){
	var u 		= new URI();
	var uRoot 	= new URI(rootPath);
	var split 	= u.toRelative(uRoot).split("/");
	
	//
	//	DECIDE TO SHOW THE BASKET..
	//
	if( split.length
		&& (
			split[0].toLowerCase() == "summer"
			|| split[0].toLowerCase() == "winter"
			|| split[0].toLowerCase() == "activities"
		)
	){
		$("activity-basket-main").setStyles({display:"block"});
		$("activity-basket-news").setStyles({display:"none"});
	}
	
	
	if($("basket-form") && $("select-season")){
		new ActivityBasket({
			seasons			:["winter","summer"],
			formEl			:$("basket-form"),
			seasonSwitchEl	:$("select-season")
		});
	}
});









