/************************************
/*General functions
/*for Garage Countdown Backstage
/*
/*Author: Antto Perttola, 2009
************************************/

//Declare chat-variables
var chatUpdater = false; //Object that stores the periodical updater
var chatUpdater_running = true; //Status of the periodical updater

//Things to be loaded
Event.observe(window, 'load', function() {
	
	chatUpdater = new Ajax.PeriodicalUpdater('chat_message_area', './inc/ajax_chat.php', {
		
		method: 'post', 
		parameters: {
			
			bid: $('bs_chat_bid').value
			
		},
		frequency: 3,
		onSuccess: function(){		
			//alert("Chat updated"); //for debugging
		}
		
	});
	
	//chatUpdater.stop(); //not in use currently, because the chat-area doesn't load up properly.
	
	new Ajax.PeriodicalUpdater('bs_present_users', './inc/ajax_bs_present_users.php', {
		
		method: 'post', 
		parameters: {
			
			bid: $('bs_chat_bid').value
			
		},
		frequency: 60,
		onSuccess: function(){		
			
		}
		
	});
  
});

//Show the necessary application with a little fade-effect
function showApplication(app, type){
	
	Effect.Appear('application_'+app, { 
		
		duration: 0.5,
		afterFinish: function() { 
		
			if(app == 'chat'){
				$('chat_message_area').scrollTop = $('chat_message_area').scrollHeight;	
				if(!chatUpdater_running){
					chatUpdater.start();
					chatUpdater_running = true;	
				}
			}
		}
		
	});
	
	return false;	
	
}

//Hide the necessary application with a little fade-effect
function hideApplication(app, type){
	
	if(app == 'chat'){
		if(chatUpdater_running){
			chatUpdater.stop();
			chatUpdater_running = false;
		}
	}

	Effect.Fade('application_'+app, { duration: 0.5 });
	
	if(type == 0 || type == 2)
		showApplication('video');
	else if(type == 3)
		showApplication('radio');

	return false;	
	
}

//Show highlight, if mouse is over the secondary link to an application
function showHighlight(linkname) {
	
	$(linkname+'_link').addClassName(linkname+'_link');	
	
}

//Hide highlight, if mouse is out the secondary link to an application
function hideHighlight(linkname) {
	
	$(linkname+'_link').removeClassName(linkname+'_link');	
	
}

//Send application to get a backstage pass (note: the word application is used in two different meanings here.
//Sorry about that.)
function sendApplication(bid){
	
	new Ajax.Request('./inc/ajax_backstage.php', {
		
		method: 'post',
		parameters: {

			bid: bid,
			message: $('application_note').value,
			submit_send_application: true
			
		},
		 onSuccess: function(transport){
			 
			// $('application_form').innerHTML = "Application sent.";
			$('application_form').innerHTML = transport.responseText;
		 }
					 
	});
	
	return false;
	
}

//Handle the application
function handleApplication(pass_id, status){
	
	new Ajax.Request('./inc/ajax_backstage.php', {
		
		method: 'post',
		parameters: {

			pass_id: pass_id,
			status: status,
			submit_handle_application: true
			
		},
		 onSuccess: function(transport){

			//check if there are any pending applications
			var pends = $('bs_pending_applications_table').getElementsByTagName('tr');
			
			if(pends.length == 1)
					$('no_pending_applications').style.display = "block";
				
			//remove the changed element
			$('bs_pass_application_'+pass_id).remove();
						
			//create a new one, hide the no applications-message
			//if the removed element was the only one of it's kind, show the no applications-message
			//for that type
			if(status == '1'){
				if($('no_accepted_applications'))
					$('no_accepted_applications').style.display = "none";
					
				var decs = $('bs_declined_applications_table').getElementsByTagName('tr');
				if(decs.length == 0)
					$('no_declined_applications').style.display = "block";

				$('bs_accepted_applications_table').insert(transport.responseText);
			}else{
				if($('no_declined_applications'))
					$('no_declined_applications').style.display = "none";
				
				var accs = $('bs_accepted_applications_table').getElementsByTagName('tr');
				if(accs.length == 0)
					$('no_accepted_applications').style.display = "block";

				$('bs_declined_applications_table').insert(transport.responseText); 
			}
			
		 }
					 
	});
	
	return false;
	
}

//Show the message included in the application for a backstage pass
function showApplicationMessage(object){

	var firstChild = object.childElements();
	var secondChild = firstChild[0].childElements();
	
	secondChild[0].style.display = "block";
	
}

//Hide the message included in the application for a backstage pass
function hideApplicationMessage(object){

	var firstChild = object.childElements();
	var secondChild = firstChild[0].childElements();
	
	secondChild[0].style.display = "none";
	
}

//Change band's status about free entrance to Backstage
function changeFreeEntranceStatus(){
	
	if($('bs_free_entrance').checked){
		
		$('bs_pending_applications_div').style.display = "none";
		$('bs_free_entrance_info').style.display = "block";
		$('bs_restricted_entrance_info').style.display = "none";
		
		
		if($('bs_accepted_applications_div'))
			$('bs_accepted_applications_div').style.display = "none";
		if($('bs_declined_applications_div'))	
			$('bs_declined_applications_div').style.display = "none";
		
	}else{
	
		$('bs_pending_applications_div').style.display = "block";
		$('bs_free_entrance_info').style.display = "none";
		$('bs_restricted_entrance_info').style.display = "block";
		
		if($('bs_accepted_applications_div'))
			$('bs_accepted_applications_div').style.display = "block";
		if($('bs_declined_applications_div'))	
			$('bs_declined_applications_div').style.display = "block";
		
	}
	
	new Ajax.Request("./inc/ajax_backstage.php", {
	
		method: 'post',
		parameters: {
			entrance_status: $('bs_free_entrance').checked,
			bid: $('bs_free_entrance').value,
			submit_change_entrance_status: true			
		}
					 
	});
	
}

//Show the upper frame message editor
function showMessageEditor(bid){
	
	if($('bs_status_txtarea') == undefined){
	
		var message = $('bs_bandstatus').innerHTML;
		
		$('bs_bandstatus').innerHTML = "<textarea id='bs_status_txtarea' onBlur='saveMessage("+bid+")'>"+message+"</textarea>";
		$('bs_status_txtarea').focus();
		
	}
	
	return false;	
	
}

//Save the current message
function saveMessage(bid){
	
	new Ajax.Request('./inc/ajax_backstage.php', {
		
		method: 'post',
		parameters: {
		
			message: $('bs_status_txtarea').value,
			bid: bid,
			submit_save_message: true
			
		},
		 onSuccess: function(transport){

			$('bs_bandstatus').innerHTML = transport.responseText;
	 
		 }
					 
	});
			
}

//Show comments of the item
function showComments(id,type){
		
	//get the comments
	new Ajax.Request('./inc/ajax_bs_video_comments.php', {
					 
		method: 'post',
		parameters: {
		
			id: id,
			type: type
			
		},
		onSuccess: function(transport){
			
			var response_arr = transport.responseText.split("|");
			
			//print the basics
			$('application_comments').innerHTML = response_arr[0];
			
			//run some javascript to actually print the comments
			eval(response_arr[1]);
			eval(response_arr[2]);
			
		}
					 
	});
	
	//check where we are so we know which div to hide
	switch(type){
		case 0:
		case 2:
			hideApplication("video");
			break;
		case 3:
			hideApplication("radio");
			break;
	}
	
	//open the comment-div
	showApplication('comments');

	return false;	
	
}

//Set a backstage-video or a song as default
//@type = is this video or song
//@id = id of the video/song

function setAsDefault(type,id){
	
	//Make an ajax call
	new Ajax.Updater('application_radio_left','./inc/ajax_backstage.php', {
		
		method: 'post',
		parameters: {

			type: type,
			id: id,
			submit_set_as_default: true
			
		},
		 onSuccess: function(transport){
			/*linktexts = transport.responseText.split("|");
			
			links = $('application_radio_left').getElementsByTagName('a');
			
			for(i=0;i<links.length;i++){
				if(links[i].className == "bs_default_link")
					links[i].innerHTML = linktexts[1];
					links[i].onclick = function(){
						setAsDefault('song',id);	
						return false;
					}
			}			
			
			$('bs_default_link_'+id).innerHTML = linktexts[0];*/
		 }
					 
	});
	
	return false;
	
	
}

function removeFromDefault(type,id){
	
	//Make an ajax call
	new Ajax.Updater('application_radio_left','./inc/ajax_backstage.php', {
		
		method: 'post',
		parameters: {

			type: type,
			id: id,
			submit_remove_from_default: true
			
		},
		 onSuccess: function(transport){
			$('bs_default_link_'+id).innerHTML = transport.responseText;
		 }
					 
	});
	
	return false;
	
	
}

//Change the wallpaper
//@band_id = id of the band
//@wallpaper_id = id of the wallpaper

function changeWallpaper(band_id,wallpaper_id){
	
	//Make an ajax call
	new Ajax.Request('./inc/ajax_backstage.php', {
		
		method: 'post',
		parameters: {

			band_id: band_id,
			wp_id: wallpaper_id,
			submit_change_wallpaper: true
			
		},
		 onSuccess: function(transport){
			$('bs_wall').style.backgroundImage = "url('img/neutral/backstage/wallpapers/wallpaper_" + transport.responseText + ".jpg')";
		 }
					 
	});
	
	return false;
	
	
}

//Show the wallpaper-menu

function wallpaperMenu(){
		
	if($('bs_wallpapermenu').offsetHeight <= "16"){
	
		new Effect.Morph('bs_wallpapermenu', {
		  style: 'height:90px;', // CSS Properties
		  duration: 0.8 // Core Effect properties
		});
		
	}else if($('bs_wallpapermenu').offsetHeight >= "90"){
		
		new Effect.Morph('bs_wallpapermenu', {
		  style: 'height:14px;', // CSS Properties
		  duration: 0.8 // Core Effect properties
		});		
		
	}
	//alert($('bs_wallpapermenu').offsetHeight);

	
	return false;
	
}

//Add user as a fan
function addAsFan(bid){
	
	new Ajax.Updater('band_fans','./inc/ajax_add_as_fan.php',{
					 
		method: 'post',
		parameters: {
			bid: bid	
		}
	
	});
	
	return false;	
	
}
