document.observe("dom:loaded", function() {
	$$('div.expandable').each( function(obj){ new ExpandableBox(obj); }); 
	$$('div.tabs').each( function(obj){ new Tabs(obj); });
	$$('div.Rtabs').each(function(obj){ new Tabs(obj); });
	$$('div.scrollist').each(function(obj){ new Scroller(obj); });
	ResetScrollPosition();
   
	$$('div.cms_btn').each(function(target){
		target.observe('mouseover',function(){ this.setStyle({backgroundPosition: '0 -22px'}); });
		target.observe('mouseout',function(){  this.setStyle({backgroundPosition: '0 -3px'}); });
	});
	$$('div.event').each(function(target,index){
		var ep=$('ep_'+index);
		target.observe('mouseover',function(event){  			
			var v_width=document.viewport.getWidth();
			ep.show();
  			var new_pos = $('ed_'+index).positionedOffset();
  			if(new_pos.left+230 < v_width){ ep.setStyle({left: new_pos.left+30+'px', top: new_pos.top+'px'}); }					
  			else ep.setStyle({left: new_pos.left-200+'px', top: new_pos.top+'px'});
  		}.bind(this),false);
  		target.observe('mouseout',function(event){
  			ep.hide();
  		}.bind(this),false);   				
	}.bind(this),false);	
});

function open_window(URL){
	if($('dialog2')) win2.destroy();
	win2 = new Window('dialog2', {className:"alphacube", showEffect: Element.show, hideEffect: Element.hide, zIndex:150, top:100, left:200, width:700, height:500, resizable: true, wiredDrag: true,destroyOnClose:true});
	win2.getContent().innerHTML= '<img src="images/indicator.gif" alt="" />';
	win2.setURL(URL); 
	if(URL!=$('dialog2_content').src)	
		$('dialog2_content').src=URL;
	win2.showCenter(true);
}

var ExpandableBox = Class.create({
  initialize: function(obj) {
   		this.divs=obj.childElements();
		this.box = { element: this.divs[0], defaultheight: this.divs[0].getHeight(), mxheight: this.divs[0].firstDescendant().getHeight() };
		if(this.divs[1]) this.divs[1].observe('click', this.BoxToggle.bind(this));
	},

	BoxToggle:function(){
	  	if(this.box.element.getHeight()==this.box.defaultheight){
	  		this.box.element.morph('height:'+this.box.mxheight+'px;');
		}else{
  			this.box.element.morph('height:'+(this.box.defaultheight-2)+'px;');
		}
	}	  

});

var Tabs = Class.create({
  initialize: function(obj) {
   		this.li = {element: obj.select('.head ul li'), active:0 };
   		this.div = obj.select('div.content')[0].childElements();
   		if(this.li.element[0]) this.li.element[0].addClassName('first active');
   		if(this.div[0]) this.div.without(this.div[0]).invoke('hide');
   		this.li.element.each(function(target,index){
   			target.observe('click',function(event){
   					this.setActiveTab(index,event);
   				}.bind(this),false);
			target.firstDescendant().observe('click',function(event){
   					this.setActiveTab(index,event);
   				}.bind(this)); 
			}.bind(this),false);
	},
	
	setActiveTab:function(index,e){
		this.li.element[this.li.active].removeClassName('active');
		this.div[this.li.active].hide();
		this.li.element[this.li.active=index].addClassName('active');
		this.div[this.li.active].show();
		Event.stop(e);
	}
});


var Scroller = Class.create({
	initialize: function(obj) {
		this.btnLeft= obj.select('.btnleft')[0];
		this.btnLeft.hide();
		this.btnRight= obj.select('.btnright')[0];
		disp=obj.getStyle('display');
		obj.show();
		this.ul = {element: obj.select('.container ul')[0], width: obj.select('.container')[0].getWidth()-4 };
		ul=obj.select('.container ul li');
		ulWidth=ul[0].getWidth();
		this.position ={current:0, limit: ul.size()*(ulWidth)-this.ul.width };
		obj.style.display=disp;
		
		this.btnLeft.observe('click', this.scrollLeft.bind(this) );
		this.btnRight.observe('click', this.scrollRight.bind(this) );
	},
	
	scrollRight: function(){
		if(this.position.current<this.position.limit){
			this.ul.element.morph('left:-'+(this.position.current+this.ul.width)+'px;');
			this.position.current=this.position.current+this.ul.width;
			this.btnLeft.show();
			if(this.position.current==this.position.limit) this.btnRight.style.display='none';
		}
	},
	scrollLeft: function(){
		if(this.position.current>0){
			this.ul.element.morph('left:-'+(this.position.current-this.ul.width)+'px;');
			this.position.current=this.position.current-this.ul.width;
			this.btnRight.show();
			if(this.position.current==0) this.btnLeft.style.display='none';
		}
	}	
});

function SaveScrollXY() {
 $('ScrollY').value='1';
}

function ResetScrollPosition() {
	var hidy;
	hidy = ($('ScrollY') ? $F('ScrollY') : null);
	if (hidy != null && hidy!=0) {
	new Effect.ScrollTo('submit_link',
		{
    	    duration: 0.6,  
            transition: Effect.Transitions.sinoidal
        });
	}
}

function popup(mylink, windowname,x,y){
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
   		href=mylink;
	else
   		href=mylink.href;
	window.open(href, windowname, 'width='+x+'px,height='+y+'px,toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1');
	return false;
}

function profile(mylink, windowname,x,y){
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
   		href=mylink;
	else
   		href=mylink.href;
	window.open('http://yellfootball.co.uk/profiles/'+mylink+'/'+mylink+'.html', windowname, 'width='+x+'px,height='+y+'px,toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1');
	return false;
}