var sliders=new Array;

jQuery(document).ready(function(){
	jQuery('.hooolpSliderFrame').mouseenter(
		function(e){
			stopSlide(e.target.id);
		}
	);
	for(var i=0;i<sliders.length;i++){
		startSlide(sliders[i].sliderId);
	}
});

function addRatingForBand(bandId,ratingValue,ratingNum){
	var sliderId='hooolpSlider_'+bandId;
	if(!document.getElementById(sliderId)){
		sliders.push(new Object());
		sliders[sliders.length-1].sliderId=sliderId;
		sliders[sliders.length-1].autoDir='up';
		sliders[sliders.length-1].autoSlide=null;
		document.write('<div class="hooolpRatingFrame">');
		document.write('<div class="hooolpRatingResultFrame"><p class="hooolpRatingText" >hooolp</p><p id="hooolpRatingNumber_'+bandId+'" class="hooolpRatingNumber">'+ratingValue+'</p><p id="hooolpRatingText_'+bandId+'" class="hooolpRatingText">'+ratingNum+'  Ratings</p></div>');
		document.write('<div class="hooolpSliderFrame" id="hooolpSliderFrame_'+bandId+'"><div id="'+sliderId+'" class="hooolpSliderInnerFrame"></div></div>');
		document.write('<div class="hooolpRating_MessageFrame"id="hooolpRating_MessageFrame_'+bandId+'" >');
		document.write('<p class="hooolpRating_MessagePane" id="hooolpRating_MessagePane_'+bandId+'" ></p>');
		document.write('</div>');
		document.write('</div>');
		jQuery('#'+sliderId).slider({ 
			orientation: 'vertical' , 
			max: 10 , 
			min: 0 , 
			step: 1, 
			animate: false, 
			value: 5, 
			change: 
				function(event,ui){
					setHooolpRatingNumber(event,ui);
				} ,
			slide: 
				function(event,ui){
					setHooolpRatingNumber(event,ui);
				} ,
		});
		document.write('<p class="hooolpRating_center" ><input type="button" id="hooolpRating_button_'+bandId+'" class="button" value="Band bewerten" onclick="setBandRating('+bandId+');" </p>');
	}
}

function setHooolpRatingNumber(event,ui,rateId){
	var ratingNumber=ui.value;
	var sliderId=event.target.id.substring(13);
	
	var slidersIndex=0;
	for(var i=0;i<sliders.length;i++){
		if(sliders[i].sliderId==sliderId){
			slidersIndex=i;
			break;
		}
	}
	if(sliders[slidersIndex].autoSlide==null){
		document.getElementById('hooolpRatingNumber_'+sliderId).innerHTML=ratingNumber;
	}
}

function autoSlide(sliderId){
	var slidersIndex=0;
	var sliderVal=jQuery('#'+sliderId).slider('value');
	
	for(var i=0;i<sliders.length;i++){
		if(sliders[i].sliderId==sliderId){
			slidersIndex=i;
			break;
		}
	}
	
	if(sliders[slidersIndex].autoDir=='up' && sliderVal<10){
		jQuery('#'+sliderId).slider('value', sliderVal+1);
	}else if(sliders[slidersIndex].autoDir=='up' && sliderVal==10){
		jQuery('#'+sliderId).slider('value', sliderVal-1);
		sliders[slidersIndex].autoDir='down';
	}else if(sliders[slidersIndex].autoDir=='down' && sliderVal>0){
		jQuery('#'+sliderId).slider('value', sliderVal-1);
	}else if(sliders[slidersIndex].autoDir=='down' && sliderVal==0 ){
		jQuery('#'+sliderId).slider('value', sliderVal+1);
		sliders[slidersIndex].autoDir='up';
	}
	
}

var autoSlides=new Array;

function startSlide(sliderId){
	
	var slidersIndex=0;
	for(var i=0;i<sliders.length;i++){
		if(sliders[i].sliderId==sliderId){
			slidersIndex=i;
			break;
		}
	}
	if(sliders[slidersIndex].autoSlide==null){
		sliders[slidersIndex].autoSlide=window.setInterval('autoSlide("'+sliderId+'")',200);
	}
}


function stopSlide(sliderId){
	var x='hooolpSlider_'+sliderId.substring(18);
	var slidersIndex=0;
	for(var i=0;i<sliders.length;i++){
		if(sliders[i].sliderId==x){
			slidersIndex=i;
			break;
		}
	}
	if(sliders[slidersIndex].autoSlide!=null){
		window.clearInterval(sliders[slidersIndex].autoSlide)
		sliders[slidersIndex].autoSlide=null;
	}

}

function setBandRating(bandId){
	var actRate=jQuery('#hooolpSlider_'+bandId).slider('value');
	jQuery.post('/acp/lib/rating_response.php',{band_id: bandId, band_rating:actRate},function(data){setBandRating_callback(data);},'json');
}

function setBandRating_callback(data){
	if(data.error!=""){
		setHooolpRatingMessage(data.id,data.error);
	}else{
		setHooolpRatingMessage(data.id,data.msg);
		document.getElementById('hooolpRatingNumber_'+data.id).innerHTML=data.rating;
		document.getElementById('hooolpRatingText_'+data.id).innerHTML=data.rating_num+' Ratings';
		document.getElementById('hooolpRating_button_'+data.id).value='hooolp me! ('+data.rating_num+' Ratings)';
	}
	
}

function setHooolpRatingMessage(ratingId,message){
	document.getElementById('hooolpRating_MessagePane_'+ratingId).innerHTML=message;
	jQuery('#hooolpRating_MessageFrame_'+ratingId).show('fast');
	window.setTimeout('jQuery("#hooolpRating_MessageFrame_'+ratingId+'").hide("fast")',4000);
}