var Listscroll=function(parentElement,childClass)
{  var parent,childclass,interval,intervalid,displaycount,itemHeight;
	 function buildList(){
	 		var c=displaycount-1;
	 		parent.children('.'+childClass+':lt('+c+')');
	 		parent.children('.'+childClass+':gt('+c+')').css('display','none');
	 		itemHeight=parent.children('.'+childClass).css('height');
	  }
	  
	function rotateList(){
		var next=parent.children('.'+childClass+':hidden:last');
		var tohide=parent.children('.'+childClass+':visible:last');
		next.prependTo(parent);next=parent.children('.'+childClass+':first');next.css('height','0');
		next.animate({height:itemHeight},'slow',function(){tohide.css('display','none').css('height','0');});
	}
	
	function startRotation(){
		intervalid=setInterval(rotateList,interval);
	}

	this.setParent=function(parentElement){
											if(typeof parentElement!==undefined){
												parent=$(parentElement);
								 			}
											else{
												throw new Error('Listscroll: No parent element was provided.');
											}
								}

	this.setChildClass=function(childClass){
		if(typeof childClass!==undefined){
				childclass=childClass;
		}
		else{
				throw new Error('Listscroll: No child class was defined.');
		}
	}
	
	this.setInterval=function(time){
		interval=time;
		clearInterval(intervalid);
		startRotation();
	}

	this.setDisplayCount=function(disp_count){
			if(disp_count===undefined){
					displaycount='3';
			}
			else{
				displaycount=disp_count;buildList();
			}
		}
		
	this.setParent(parentElement);
	this.setChildClass(childClass);
	this.setDisplayCount();buildList();
	this.setInterval(1000);
}

	$(document).ready(function(){
		function isset(e){return!(typeof(e)==='undefined'||e===null);}
		function empty(e){return!isset(e)||e==='';}
		var slide,i;
		$(function(){replaceSelect();startSlide();init_language();
		$('div.user_categories a').click(function(){
				startSlide($(this).attr('id').replace('_link',''));
				return false;
			});
		});
		
		function startSlide(fromKey){
			clearInterval(slide);
			slide=null;
			isset(fromKey)?i=fromKey:i=0;
			displayUserCategory(i);
			slide=setInterval(function(){
															if(i===8)i=0;
															displayUserCategory(i);
															},12000);}
		
		function displayUserCategory(key){
			var uc=$('div.user_category').hide().filter('#cat_'+key);
			if($('#cat_'+key).is(':visible')){
					uc.show();
			}
			else
			{
				uc.fadeIn(300);
			}
			$('div.user_categories a.selected').removeClass('selected');
			$('div.user_categories a#'+key+'_link').addClass('selected');
			i++;
		}

		function replaceSelect(){
			$("select:not('.notreplaced')").each(function(){
																						var e=$(this);
																						if(!e.next().hasClass('select')){
																							list='';
																							name=e.attr('name');
																							id=e.attr('id');
																							options=e.children().filter('option');
																							if(e.val()===''){
																								selected='<div>'+options.filter('option:first').html()+'</div>';
																							}
																							else{
																							selected='<div>'+options.filter('option[value="'+e.val()+'"]').html()+'</div>';
																							}
																							options.each(function(){
																													if($(this).text()!==''){
																														list+='<div id="'+id+'%%'+escape($(this).attr('value'))+'">'+$(this).text()+'</div>';
																													 }
																							});
																				
																							e.after('<div id="fake_'+id+'" class="select">'+selected+'<div class="options">'+list+'</div></div>');
																							if($.browser.msie){
																								e.css({'visibility':'hidden','position':'absolute','left':'-9999px'});
																							}
																							else
																							{
																								e.hide();
																							}
																							var width=e.width()+13;
																							if($.browser.mozilla&&($.browser.version==='1.9'||$.browser.version==='1.9.1'||$.browser.version==='1.9.0.1')){
																								width+=14;
																							}
																							
																							$('div#fake_'+id).css({width:width,'line-height':'18px','height':'18px'}).click(function(){
																									$(this).css('z-index',100).children().show().parent().bind('mouseleave',function(){
																											$(this).children().filter('.options').hide().parent().css('z-index',10);});}).children().filter('.options').children().click(function(){
																													$(this).parent().slideUp(250).prev().text($(this).text());
																													var name_id=$(this).attr('id').split('%%',2);
																													$('select#'+name_id[0]).children().attr('selected',false).filter('option[value="'+unescape(name_id[1])+'"]').attr('selected',true);
																													$('select#'+name_id[0]).change();
																													});
																											}});}
																											
																											
function init_language(){
		$('select#language').unbind().change(function change_language(){
																					var select=$('select#language')[0];
																					var optionSelected=select.options.selectedIndex;
																					var lan=select[optionSelected].value;
																					var loc=document.location.href;
																					if(loc.indexOf('?language')!=-1){
																						loc=loc.substring(0,loc.indexOf('?language'));
																					}
																					seperator=loc.indexOf("?")>0?"&":"?";document.location.href=loc+seperator+"language="+lan
																					});}

var runnerslist=new Listscroll('.spain_athletes','runner_on_strands');
runnerslist.setInterval(10000);runnerslist.setDisplayCount(3);});
