// 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 changeCommentsPage($comType, $comPageNo, $comID) {
	$.get("/ajax/a_getComments.php", { elemID: $comID, elemType: $comType, elemPage: $comPageNo},
			  function(data){
			  	$('#commentsWithPagination').html(data);			  	
			  });
}

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 = '<li style="border:1px dotted #ccc; background:none; margin-bottom:10px;">'+
								'<div style="float:left;">'+
								    '<span class="commentTxt" style="width:90%; float:left; margin-left:20px;">'+
	                                '<div style="float:left; width:100%;">'+
										'<h4 style="float:left;">Comment by <a href="#">'+$commentElements[1]+'</a> | '+$commentElements[2]+'</h4>'+
	                                 '</div>'+
	                                '</span>'+	                                
	                                '<br />'+
                                    '<div style="float: right;">'+                                    
                                    	'<span style="color: black;">Is this interpretation helpful?</span>'+
										'<a href="javascript:voteComment(\'plus\',\''+$modeTxt+'\','+$commentElements[4]+','+$commentElements[3]+',\'\');">'+
                                        	'<img src="http://static.songlyrics.com/images/thumb_up.gif" align="absmiddle"/>'+
                                        '</a>'+                                        
                                        '<span style="font-size:18px; font-family:Georgia, \'Times New Roman\', Times, serif; color:#0066CC;" id="commentCounter_'+$commentElements[3]+'">0</span>'+                                    
                                        '<a href="javascript:voteComment(\'minus\',\''+$modeTxt+'\','+$commentElements[4]+','+$commentElements[3]+',\'\');">'+
                                        	'<img src="http://static.songlyrics.com/images/thumb_down.gif" align="absmiddle" />'+
                                        '</a>'+                                        
                                    '</div>'+
                                '</div>'+
								'<span class="commentTxt2" style="float:left; margin-left:20px;">'+
                                 '<div style="float:left; width:100%;">'+
									'<p>'+$commentElements[0].replace("\n","<br />")+'</p>'+
                                 '</div>'+
								'</span>'+
								'<span class="clear"></span>'+
							'</li>';	    		
	    		$("#commentsList").prepend($newComment);
	    		jQuery.facebox('Comment added.');
	    		$("#commTxt").val("");
	    		$("#commentsListNoComments").hide();
	    	} else {//failed for a reason
	    		jQuery.facebox($data[1]);
	    	}
	  });
	
}

var adsArray = new Array;

function placeTargetDiv(name, div_style) {
    document.write('<div '+(div_style!=''?'class="'+div_style+'"':'')+' id="'+name+'">&nbsp;</div>'); 
    adsArray[name] = name;
}


function _lateLoadingDivs() {
    for (var divName in adsArray) {
       $("#"+divName).html($("#"+divName + '-Hidden').html());      
       $("#"+divName + '-Hidden').html('');      
    }
}