document.observe("dom:loaded", function() {
   $$('div.tabs').each( function(obj){ new Tabs(obj); });
   $$('div.squadbox').each(function(obj){new Squad(obj); });
});

var ExpandableBox = Class.create({
  initialize: function(config) { 
		this.box = {element: $(config.target), defaultHeight: $(config.target).getHeight(), maxHeight: $(config.target).childElements()[0].getHeight(), padding: 2*config.padding };
  		this.trigger = $(config.trigger);
  		if(this.trigger){ this.trigger.observe('click', this.BoxToggle.bind(this)); } 		
	},

	BoxToggle:function(){
	  	if(this.box.element.getHeight() == this.box.defaultHeight){
	  		this.box.element.morph('height:'+this.box.maxHeight+'px;');
	  		this.trigger.childElements()[0].update('See less');
		}else{
			this.box.element.morph('height:'+(this.box.defaultHeight-this.box.padding)+'px;');
	  		this.trigger.childElements()[0].update('See more'); 					
		} 
	}	  

});

Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

var Tabs = Class.create({
  initialize: function(obj) {
   		this.li = {element: obj.childElements()[0].select('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);
			}.bind(this),false);
	},

	setActiveTab:function(index,e){

		this.li.element[this.li.active].removeClassName('active');
		this.div[this.li.active].hide();
		this.li.active=index;
		this.li.element[this.li.active].addClassName('active');
		this.div[this.li.active].show();
		Event.stop(e);
	}
});


var Squad = Class.create({
	initialize: function(obj){
		this.li = obj.select('ul')[0].childElements();
		this.li.each(function(e, index){
			e.addClassName('collapsed');
			e.select('span')[0].innerHTML='[+]';
			e.select('span')[0].observe('click', function(event){
				this.toggleGroup(index, event);
			}.bind(this),false);
		}.bind(this),false);
	},
	
	toggleGroup:function(index, e){
		this.li[index].toggleClassName('collapsed');
		this.li[index].select('span')[0].innerHTML = (this.li[index].select('span')[0].innerHTML == '[+]' ? '[-]' :'[+]');  
	}

});

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('http://www.thisiscricket.info/'+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://www.thisiscricket.info/profiles/'+mylink+'/index.htm', windowname, 'width='+x+'px,height='+y+'px,toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1');
	return false;
}
