/*
* Copyright (c) 2007, Iliyan Gochev
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of Iliyan Gochev nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Iliyan Gochev ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Iliyan Gochev BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
////////////////////////////////////////////////////////////////////////////////

/**
 * @author Iliyan Gochev
 * @about: Simple JS Functions making calls to server scripts for AJAX-style updates
 */
//Gets Clip ID as argument to pass to ajax request, Updates Lyrics Info Block
//Lyrics Requests
function updateLyrics(id) {
	document.getElementById('lyrics').innerHTML = "<div align='center'><img src='img/loading.gif' /></div>"	
	setTimeout('do_nothing()', 1000)	
 	new Ajax.Updater("lyrics","ajax.php?type=lyrics&act=show&id="+id,{asynchronous:true});
}

function showPostLyricsBox(id) {
	document.getElementById('lyrics').innerHTML = "<div align='center'><img src='img/loading.gif' /></div>"	
	setTimeout('do_nothing()', 1000)	
	new Ajax.Updater("lyrics","ajax.php?type=lyrics&act=form&id="+id,{asynchronous:true});
}

function submit_lyrics(id) {	
	new Ajax.Request('ajax.php?type=lyrics&act=post&lyrics='+encodeURI(document.lyrics.lyrics.value)+'&id='+id);
	document.getElementById('lyrics').innerHTML = "<div align='center'><img src='img/loading.gif' /></div>"	 
	setTimeout('updateLyrics(id)', 2000)	
}

//Comments Requests
//Showing Comments for clip
function showComments(clipid) {
	new Ajax.Updater("comments","ajax.php?type=comments&act=show&id="+clipid,{asynchronous:true});
}

// Submiting comment for clip
function submit_comment(clipid) {	
	new Ajax.Request('ajax.php?type=comments&act=post&username='+ encodeURI(document.comment.username.value) + '&comment='+ encodeURI(document.comment.message.value) +'&id='+ clipid);
	 
	document.comment.message.value='';
	document.comment.username.value='';
	
	document.getElementById('comments').innerHTML = "<div align='center'><img src='img/loading.gif' /></div>"	
	setTimeout("showComments(id)",1500);
} 

//Rating Requests
function showRating(id) {	
	new Ajax.Updater("rating-box","ajax.php?type=rating&act=show&id="+id,{asynchronous:true});
}

function Vote(vote,id){
	new Ajax.Request("ajax.php?type=rating&act=post&voted="+vote+"&id="+id,{asynchronous:true})
	
	document.getElementById('rating-box').innerHTML = "<div align='center'><img src='img/loader2.gif' /></div>"
	setTimeout("showRating(id)",1300);
}

function VoteImage(vote, status){
	for(i = 1; i <= vote; i++)
	{
		var image = document.getElementById('vote' + i);
		image.src = 'img/rating' + (status == 1 ? '_g' : '') + '.gif';								
	}					
}
//Other functions
function displayPage(category,page){
	document.getElementById('page').innerHTML = "<div align='center'><img src='img/loading.gif' /></div>"
	new Ajax.Updater("page","ajax.php?type=page&cat="+category+"&page="+page,{asynchronous:true});
}

function doSearch() {
	document.getElementById('search-box').innerHTML = "<div align='center'><img src='img/loading.gif' /></div>"
	for (var i=0; i < document.search.category.length; i++)
    {
    	if (document.search.category[i].checked)
      	{
      		var rad_val = document.search.category[i].value;
      	}
    }    
	new Ajax.Updater("search-box","ajax.php?type=search&keywords="+encodeURI(document.search.keywords.value)+"&cat="+encodeURI(rad_val))
}

function embedCode(id){
	new Ajax.Updater("code","ajax.php?type=code&id="+id,
	{
		asynchronous:true,
		onLoading: function(){
			document.getElementById('code').innerHTML("<img src='img/loading.gif'>")			
		}
	});
}
function do_nothing(){
	//Function that does nothing
}
