var CorkSports = {
	HAL:function(id) {
		new Effect.Highlight( $( id ) );
		$( id ).focus();
	},
	Tabs : {
		Sel:function(id) {
			var cTabs = $A( $("tabs").select("li") ).map( Element.extend ).each( function(name,index) {
				$(name).removeClassName("sel");			
				var cIndex = $(name).getAttribute("id").split("_")[1];
				$("TabPage_"+cIndex).addClassName("hidden");			
			} );
			$( "Tab_"+id ).addClassName("sel");
			$( "TabPage_"+id).removeClassName("hidden");
			
			var cInputs = $( "TabPage_"+id ).select("input");
			if( cInputs.length ) {
				$( cInputs[0] ).focus();
			}
			
		}
	},
	Forms : {
		ClubsDisplay:function(obj) {
			if(obj.id == "rbSport") {
				window.location = "../index.cfm/page/viewclubs/sort/s";
			}else if(obj.id == "rbLocation") {
				window.location = "../index.cfm/page/viewclubs/sort/l";
			}else if(obj.id == "rbAlphabetical") {
				window.location = "../index.cfm/page/viewclubs/sort/a";
			}
		},
		RecGroupsDisplay:function(obj) {
			if(obj.id == "rbSport") {
				window.location = "../index.cfm/page/viewrecreationalgroups/sort/s";
			}else if(obj.id == "rbLocation") {
				window.location = "../index.cfm/page/viewrecreationalgroups/sort/l";
			}else if(obj.id == "rbAlphabetical") {
				window.location = "../index.cfm/page/viewrecreationalgroups/sort/a";
			}
		},
		CentresDisplay:function(obj) {
			if(obj.id == "rbLocation") {
				window.location = "../index.cfm/page/viewcentres/sort/l";
			}else{
				window.location = "../index.cfm/page/viewcentres/sort/a";
			}
		},
		Register : {
			Validate:function() {
				if( $("name").value.blank() ) {
					CorkSports.HAL("name");
					return false;
				}
				if( $("address1").value.blank() ) {
					CorkSports.HAL("address1");
					return false;
				}
				if( $("location").value.blank() ) {
					CorkSports.HAL("day");
					return false;
				}
				if( $("county").value.blank() ) {
					CorkSports.HAL("month");
					return false;
				}
				if( $("telephone").value.blank() ) {
					CorkSports.HAL("telephone");
					return false;
				}
				return true;
			}
		}
	},
	HomePageWidget : {		
		Start:function( argSlideSpeed ) {
			new Protofade("bannerSlides" , {Duration:argSlideSpeed});
		}
	}
}

var Protofade = Class.create({

	initialize: function(element, options) {		
		this.options = {
      		Duration: 1,
			Delay: 4.0,
			Random: false,
			Slideshow:true,
			Controls:false
    	}
		Object.extend(this.options, options || {});

    	this.element        = $(element);
		this.slides			= this.element.childElements();
		this.num_slides		= this.slides.length;		
		this.current_slide 	= (this.options.Random) ? (Math.floor(Math.random()*this.num_slides)) : 0;
		this.end_slide		= this.num_slides - 1;
		
		this.slides.invoke('hide');
		this.slides[this.current_slide].show();
				
		if (this.options.Slideshow) { 
			this.startSlideshow();
		}				
		if (this.options.Controls) {
			this.addControls();
		}		
	},
	
	addControls: function() {
		this.wrapper 		= this.element.up();
		this.controls		= new Element('div', { 'class': 'controls' });
		this.wrapper.insert(this.controls);
		
		this.btn_next 		= new Element('a', { 'class': 'next', 'title': 'Next', href: '#' }).update('Next');
		this.btn_previous	= new Element('a', { 'class': 'previous', 'title': 'Previous', href: '#' }).update('Previous');
		this.btn_start		= new Element('a', { 'class': 'start', 'title': 'Start', href: '#' }).update('Start');
		this.btn_stop		= new Element('a', { 'class': 'stop', 'title': 'Stop', href: '#' }).update('Stop');
		
		this.btns = [this.btn_next, this.btn_previous, this.btn_start, this.btn_stop];
		this.btns.each(function(el){
			this.controls.insert(el);
		}.bind(this));
		
		this.btn_previous.observe('click', this.moveToPrevious.bindAsEventListener(this));
		this.btn_next.observe('click', this.moveToNext.bindAsEventListener(this));
		this.btn_start.observe('click', this.startSlideshow.bindAsEventListener(this));
		this.btn_stop.observe('click', this.stopSlideshow.bindAsEventListener(this));
	},

	startSlideshow: function(event) {
		if (event) { Event.stop(event); }
		if (!this.running)	{
			this.executer = new PeriodicalExecuter(function(){
	  			this.updateSlide(this.current_slide+1);
	 		}.bind(this),this.options.Delay);
			this.running=true;
		}
	},
	
	stopSlideshow: function(event) {	
		if (event) { Event.stop(event); } 
		if (this.executer) { 
			this.executer.stop();
			this.running=false;
		}	 
	},

	moveToPrevious: function (event) {
		if (event) { Event.stop(event); }
		this.stopSlideshow();
  		this.updateSlide(this.current_slide-1);
	},

	moveToNext: function (event) {
		if (event) { Event.stop(event); }
		this.stopSlideshow();
  		this.updateSlide(this.current_slide+1);
	},
	
	updateSlide: function(next_slide) {
		
		if (next_slide > this.end_slide) { 
				next_slide = 0; 
		} 
		else if ( next_slide == -1 ) {
				next_slide = this.end_slide;
		}
		
		this.fadeInOut(next_slide, this.current_slide);		
	},

 	fadeInOut: function (next, current) {		
		this.slides[current].setStyle({"zIndex":1});
		this.slides[next].setStyle({"zIndex":50});
		new Effect.Parallel([
			new Effect.Fade(this.slides[current], { sync: true }),
			new Effect.Appear(this.slides[next], { sync: true }) 
  		], { duration: this.options.Duration });
		
		this.current_slide = next;		
	}

});

String.prototype.isEmail = function () { 
	var rx = new RegExp("\\w+([-+.\’]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); 
	var matches = rx.exec(this); 
	return (matches != null && this == matches[0]); 
}

function isNumeric(x) { 
	var y=parseInt(x); 
	if (isNaN(y)) {
	   return false; 
	} else {
		return true;
	}
} 

function LoadingAjax(txt) {
	if(typeof(txt)=="undefined") {
		txt = "Loading...";
	}
	return "<div class='ajaxMsg' id='widgetAjaxMsg'> "+txt+"</div>";
}

function parseJSON( json ){
	var o = eval('(' + json + ')');
	if( o.redirectURL )
	{
		document.location = o.redirectURL;
		return;
	}
	if( $("message") != null ) new Element.remove( "message" );
	if( o.errMsg )
	{
		if( o.errMsgTitle == null ) o.errMsgTitle = "Error";
		if( o.errMsgTimer == null ) o.errMsgTimer = 5000;
		tw.ShowMessage( o.errMsgTitle, o.errMsg, "error", o.errMsgTimer, o.errMsgPosition );
		return o;
	}
	if( o.msgTitle || o.msg )
	{
		if( o.msgTimer == null ) o.msgTimer = 5000;
		tw.ShowMessage( o.msgTitle, o.msg, o.msgClass, o.msgTimer, o.msgPosition );
	}
	return o;
}

var errFunc = function(t) {
	var win = window.open("", "win", "width=1024,height=700,resizable=yes,scrollbars=yes,status=no"); // a window object
	win.document.open("text/html", "replace");
	win.document.write( "<html><body style='margin:0'><div style='border-bottom:1px solid #222;background:#666;padding:10px;'><h1 style='color:#FFF;margin:0;padding:0;'>Digital Crew Ajax Error</h1></div><div style='padding:10px;'>"+t.responseText.replace(/^\s+|\s+$/, '') + "</div></body></html>" );
	win.document.close();
	win.focus();
}