// JavaScript Document

function setSectionHeight(){
	var main = document.getElementById('main');
	var footer = document.getElementById('footer');
	main.style.height = footer.offsetTop - main.offsetTop - 30 + 'px';
}


//JavaScript Document
var tagToAdd = '';
function reportComment($id,$commentID,$type,$gotoURL) {
	//voteComment('plus','song',{$songID})
	$.get("/ajax/a_reportComment.php", { linkID: $id, typeLink: $type, commentID: $commentID},
	  function(data) {
	  	jQuery.facebox(data);
	  	
	  	if (data == "4") {
	  		jQuery.facebox("You need to <a href='/login.php?gotourl="+$gotoURL+"'>login</a> or <a href='/register.php'>register</a> in order to report comments.");
	  	} else if (data == "1") {
	  		jQuery.facebox('We received your report! Thank you.');
	  		//$currentValue = parseInt($("#commentCounter_"+$commentID).html());
	  		//$("#commentCounter_"+$commentID).html($sign == 'plus' ? $currentValue+1 : $currentValue-1);   
	  	}
	  });
}
function voteComment($sign,$type,$id,$commentID,$gotoURL) {
	//voteComment('plus','song',{$songID})
	$.get("/ajax/a_voteComment.php", { signX: $sign, linkID: $id, typeLink: $type, commentID: $commentID},
	  function(data){
	  	jQuery.facebox(data);
	  	
	  	if (data == "4") {
	  		jQuery.facebox("You need to <a href='/login.php?gotourl="+$gotoURL+"'>login</a> or <a href='/register.php'>register</a> in order to rate comments.");
	  	} else if (data == "1") {
	  		jQuery.facebox('Comment received your vote!');
	  		$currentValue = parseInt($("#commentCounter_"+$commentID).html());
	  		$("#commentCounter_"+$commentID).html($sign == 'plus' ? $currentValue+1 : $currentValue-1);   
	  	} else {
	  		jQuery.facebox('You rated already this comment!');
	  	}
	  	
	  });
}

function addTag($songID,$type) {
	var addTag;
	jQuery.facebox('Please write your tag (1-2 words)<br /><form name="tagForm"><input type="text" id="newTagToAdd" name="newTagToAdd"><br /><input type="button" value="Add Tag" onClick="javascript:addTagProc(\''+$songID+'\',\''+$type+'\',$(\'#newTagToAdd\').val());"></form>');
	//addTag = prompt('Please write your tag (1-2 words)');
	
}

function addTagProc($songID,$type,addTag) {		
	if (addTag!='' &&  addTag != null) {
		
	$.get("/ajax/a_addTag.php", { tag: addTag , linkID: $songID, typeLink: $type},
	  function(data){
	  	jQuery.facebox('Your tag has been added.');	    
	  });
	}
}


function pollVote($pollID) {	
	var $voteOption = $('[@name=pollQ]').fieldValue();
	
	$.get("/ajax/a_addVote.php", { poll: $pollID, vOption: $voteOption},
	  function(data){	  	
	  	var $data = data.split("|");
	    if (parseInt($data[0]) == 1) {
	    	$("#pollVotesNo").html(parseInt($("#pollVotesNo").html()) + 1);
	    	$questionsAndAnswers = $data[1].split("*");
	    	for ($i=0;$i<$questionsAndAnswers.length;$i++) {
	    		$currQandA = $questionsAndAnswers[$i].split("?");
	    		$("#pollQ_"+$currQandA[1]).hide();
	    		$("#pollQa_"+$currQandA[1]).html(" - "+$currQandA[0]);
	    	}
	    } else {
	    	jQuery.facebox('You can not vote more than one time a day.');	    	
	    }
	  });
	
}

function showPollVotes($pollID) {	
	var $voteOption = $('[@name=pollQ]').fieldValue();
	
	$.get("/ajax/a_addVote.php?results=1", { poll: $pollID, vOption: $voteOption},
	  function(data){	  	
	  	var $data = data.split("|");
	    	$questionsAndAnswers = $data[1].split("*");
	    	for ($i=0;$i<$questionsAndAnswers.length;$i++) {
	    		$currQandA = $questionsAndAnswers[$i].split("?");
	    		$("#pollQ_"+$currQandA[1]).hide();
	    		$("#pollQa_"+$currQandA[1]).html(" - "+$currQandA[0]);
	    	}
	  });
	
}

function addComment($songID,$mode,$gotoUrl) {	
	//if ($loginNeeded == true) {
		//jQuery.facebox('Warning: you are not logged in. You can still send comments, but you must <a href="/login.php">Log in</a> or <a href="/register.php">register</a> before commenting in order to get credit for your contribution.');
	//}
	
	var $commentContent = $('[@name=commTxt]').fieldValue();
	var $captchaCode = $('[@name=captcha]').fieldValue();
	
	$.post("/ajax/a_addComment.php", { whatSubmited: $mode, songID: $songID, commentContent: $commentContent, captchaCode: $captchaCode, gotourl: $gotoUrl},
	  function(data){	  	
	  	var $data = data.split("|");
	    	var $data = data.split("|");
	    	//jQuery.facebox('Msg received:' + data);
	    	$("#captcha").val("");
	    	if (parseInt($data[0]) == 3) { //need login	    			
	    		jQuery.facebox($data[1]);
	    	} else if (parseInt($data[0]) == 1) { //all went fine;need approval	    		
	    		jQuery.facebox($data[1]);
	    		$("#commTxt").val("");
	    		$("#captcha").val("");
	    	} else if (parseInt($data[0]) == 2) { //all went fine;approved
	    		//get details
	    		$commentElements = $data[1].split('+==*=#*=#***');
	    		
	    		$modeTxt = $mode == 'lyrics'?'song':'album';
	    		
	    		
	    		$newComment = '<div class="commentlinks">'+
					'<div class="cmt_tools">'+
						'<div class="thumbsup">'+
						'<a title="Vote This Comment Up" href="javascript:voteComment(\'plus\',\''+$modeTxt+'\','+$commentElements[4]+','+$commentElements[3]+',\'\');">'+
						'</div>'+
						'<div class="thumbsdown">'+
						'<a title="Vote This Comment Down" href="javascript:voteComment(\'minus\',\''+$modeTxt+'\','+$commentElements[4]+','+$commentElements[3]+',\'\');">'+
						'</div>'+
						'<div class="flag">'+
							'<a href="javascript:reportComment('+$commentElements[4]+','+$commentElements[3]+',\''+$modeTxt+'\',\'\');" title="Flag This Comment"></a>'+
						'</div>'+
					'</div>'+
					'<div class="cmt_label">'+
						'<a href="/"><b id="commentCounter_'+$commentElements[3]+'">0</b></a>'+					
					'</div>'+
					'<div class="avatar">'+
						'<a href="/">'+
						'<img alt="avatar" src="images/avatar.jpg"/>'+
						'</a>'+
					'</div>'+
					'<h4>'+
						'by <a href="/">'+$commentElements[1]+'</a> on '+$commentElements[2]+
					'</h4>'+
					'<p class="cmt_contents">'+$commentElements[0].replace("\n","<br />")+'</p>'+
				'</div>';
				    		
	    		$("#commentsList").prepend($newComment);
	    		jQuery.facebox('Comment added.');
	    		$("#commTxt").val("");
	    		$("#commentsListNoComments").hide();
	    	} else {//failed for a reason
	    		jQuery.facebox($data[1]);
	    	}
	  });
	
}

