// JavaScript Document

//AJAX CONSTRUCTOR FOR ALL FUNCTIONS
function GetXmlHttpObject(){ // open xmlhttp communication /Ajax Constructor
if (window.XMLHttpRequest){ return new XMLHttpRequest();}// code for IE7+, Firefox, Chrome, Opera, Safari
if (window.ActiveXObject){ return new ActiveXObject("Microsoft.XMLHTTP");} // code for IE6, IE5  
return null;}

function ajax_diplay_article(newsID){
	var url = "/tools/ajax.php?function=ajax_display_news&news_page="+newsID;
	create_ajax_request(url,1);}
	
function ajax_diplay_artist(artistID){
	var url = "/tools/ajax.php?function=ajax_display_artist&artist="+artistID;
	create_ajax_request(url,1);}
	
function ajax_display_rel_menu(relID){
	var url = "/tools/ajax.php?function=ajax_display_relmenu&menu_page_id="+relID;
	create_ajax_request(url,0);
}

function ajax_display_release_info(release_nb){
	var url = "/tools/ajax.php?function=ajax_display_release_info&release_id="+release_nb;
	create_ajax_request(url,1);
}

function ajax_change_feedback_info(release){
	var url = "/tools/ajax.php?function=ajax_change_feedback_info&release="+release;
	create_ajax_request(url,0);
}

function ajax_add_feedback_info(formID){
	var url = "/tools/ajax.php?function=ajax_add_feedback_info&EP="+formID.release_selector.value+"&Q1="+getCheckedValue(formID.feedbackQ1)+"&Q2="+getCheckedValue(formID.feedbackQ2)+"&Q3="
	+getCheckedValue(formID.feedbackQ3)+"&Q4="+getCheckedValue(formID.feedbackQ4)+"&Q5="+escape(formID.feedbackQ5.value);
	create_ajax_request(url,0);
}

function add_shopbanner_count(shop,release){
	var url = "/tools/ajax.php?function=add_shop_count&id="+shop+"&cat="+release;
	create_ajax_request(url,999); //999 is used so no functions are called to update the page or msg
}

/*function add_relplay_count(release, track){
	var url = "/tools/ajax.php?function=add_relplay_count&rel="+release+"&track_name="+track;
	create_ajax_request(url,999); //999 is used so no functions are called to update the page or msg
}*/

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function create_ajax_request(url,col_side){ // function creating ajax request using POST method (url encoded form)
	ajax_request = GetXmlHttpObject();
	if (ajax_request==null){ alert ("Your browser does not support Ajax! - Please upgrade to a more recent version!"); return;}
	if (col_side==0) ajax_request.onreadystatechange=updateMenu;
	if (col_side==1) ajax_request.onreadystatechange=updatePage;
	ajax_request.open('GET', url, true); // always call process.php - contains all function to execute commands
	ajax_request.send(null);
}

//UPDATE PAGE CALLED BY AJAX
function updatePage(){ 
if (ajax_request.readyState==4){
var ajax_response = ajax_request.responseText.split('@');
document.getElementById('col-right').innerHTML = ajax_response[0];

AudioPlayer.embed("audioplayer_1", {soundFile: "http://" + document.domain + "/audio/"+ ajax_response[1] +"_a.mp3"});
AudioPlayer.embed("audioplayer_2", {soundFile: "http://" + document.domain + "/audio/"+ ajax_response[1] +"_b.mp3"});

//$.getScript('http://platform.twitter.com/widgets.js%27');                         


/*$('#content').find('a.twitter-share-button').each(function(){
var tweet_button = new twttr.TweetButton( $( this ).get( 0 ) );
tweet_button.render();
});*/
}
}// Ajax Content response return - error or success messages from operation  

function updateMenu(){ 
if (ajax_request.readyState==4){
document.getElementById('col-left').innerHTML = ajax_request.responseText;
}
}// Ajax Menu response return - error or success messages from operation  

//
$(document).ready(function() {	

	//Background color, mouseover and mouseout
	var colorOver = '#48645d';
	var colorOut = '#000';
	var colortext_over = '#fff';
	var colortext_out = '#666';
	var color_span_text_out = '#C30';

	//Padding, mouseover
	var padLeft = '10px';
	var padRight = '10px';
	
	//Default Padding
	var defpadLeft = 10;//$('#artistmenu li a').css('paddingLeft');
	var defpadRight = 10;//$('#artistmenu li a').css('paddingRight');
		
	//Animate the LI on mouse over, mouse out
	$('#artistmenu li').click(function () {	
		//Make LI clickable
		window.location = $(this).find('a').attr('href');
		
	}).mouseover(function (){
		
		//mouse over LI and look for A element for transition
		$(this).find('a')
		.animate( { paddingLeft: padLeft, paddingRight: padRight}, { queue:false, duration:100 } )
		.animate( { backgroundColor: colorOver, color: colortext_over }, { queue:false, duration:200 });
		
		$(this).find('span')
		.animate( { color: colortext_over}, { queue:false, duration:200 });

	}).mouseout(function () {
	
		//mouse oout LI and look for A element and discard the mouse over transition
		$(this).find('a')
		.animate( { paddingLeft: defpadLeft, paddingRight: defpadRight}, { queue:false, duration:100 } )
		.animate( { backgroundColor: colorOut, color: colortext_out }, { queue:false, duration:200 });
		
		$(this).find('span')
		.animate( { color: color_span_text_out}, { queue:false, duration:200 });
	});	
	
	//Scroll the menu on mouse move above the #sidebar layer
	$('#artistsidebar').mousemove(function(e) {

		//Sidebar Offset, Top value
		var s_top = parseInt($('#artistsidebar').offset().top);		
		
		//Sidebar Offset, Bottom value
		var s_bottom = parseInt($('#artistsidebar').height() + s_top);
	
		//Roughly calculate the height of the menu by multiply height of a single LI with the total of LIs
		var mheight = parseInt($('#artistmenu li').height() * $('#artistmenu li').length);
	
		//I used this coordinate and offset values for debuggin
		//$('#debugging_mouse_axis').html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);
		//$('#debugging_status').html(Math.round(((s_top - e.pageY)/100) * mheight / 2));
			
		//Calculate the top value
		//This equation is not the perfect, but it 's very close	
		var top_value = Math.round(( (s_top - e.pageY) /100) * mheight / 2);
		top_value = top_value / 1.5;
		
		//Animate the #menu by chaging the top value
		$('#artistmenu').animate({top: top_value}, { queue:false, duration:500});
	});

	
});
