var ImageList = new Array(
   "/images/star_big_empty.png",
   "/images/star_big_full.png"
);
    
//PRELOAD
window.onload=function() {
	PreloadedImg = new Array();
	for(var i=0; i < ImageList.length; i++){
		PreloadedImg[i] = new Image();
		PreloadedImg[i].src = ImageList[i];
	}
}


/*
	FUNCTION AJAX: add vote
*/
function addVote (file_external_id, contest_id, rem_old) {
	if (!file_external_id) return false;
	if (!contest_id) return false;
	if (!rem_old) {
		rem_old=0;
		if (!confirm ("Are You sure?")) {
			return false;
		} else {
			$show('vote_error');	
		}
	}	
	SendRequestByAjax("process.php", "process=add_vote&external_id="+file_external_id+"&contest_id="+contest_id+"&rem_old="+rem_old, addVoteCallBack);
	return false;
}

/* 
	FUNCTION CALLBACK: for add vote function 
*/
 function addVoteCallBack (options) {
	var error = options.error;
	var error_desc = options.error_desc;
	var contest_id = options.contest_id;
	var external_id = options.external_id;
	var votes = options.votes;

	if (error) {
		$hide('vote_text'+contest_id);
		$('vote_error').innerHTML = error_desc;
		return false;
	}
	$show('vote_text'+contest_id);
	$('vote_text'+contest_id).innerHTML = '<div>You have voted.</div>';
	$('votes'+contest_id).innerHTML = votes;
	$('vote_error').innerHTML = '';
 }



/*
	FUNCTION AJAX: add vote
*/
/* 
function deleteVote (file_external_id, contest_id) {
	if (!file_external_id) return false;
	if (!contest_id) return false;
	if (!confirm ("Are You sure?")) return false;
	SendRequestByAjax("process.php", "process=delete_vote&external_id="+file_external_id+"&contest_id="+contest_id, deleteVoteCallBack);
}

function deleteVoteCallBack (options) {
	var error = options.error;
	var error_desc = options.error_desc;
	var contest_id = options.contest_id;
	var external_id = options.external_id;
	var votes = options.votes;
	if (error) {
		$('vote_error').innerHTML = error_desc;
		return false;
	}
	$('vote_text'+contest_id).innerHTML = '<a href="#" onclick="addVote(\''+external_id+'\', '+contest_id+');return false;">Add Vote</a>';
	$('votes'+contest_id).innerHTML = votes;
	$('vote_error').innerHTML = '';
}

*/

/*
	FUNCTION AJAX: set comment
*/
function setComment (form_id) 
{
	var form = $(form_id);
	var file_id = form.file_external_id.value;
	var comment = form.comment_text.value;

	patt1 = new RegExp("^[ ]+$");

	if (file_id.length == 0) {
		$('commentsError').innerHTML = "Incorrect File External Id." ;
		return false;
	}

	if (comment.length == 0) {
		$('commentsError').innerHTML = "Please insert your comment.";
		return false;
	}

	if (patt1.test(comment)) {
		$('commentsError').innerHTML = "Please insert correct Comment.";
		return false;
	}

	SendFormByAjax ('comment_add', form_id, commentAddCallback);
	return true;
}



/*
	FUNCTION AJAX CALLBACK: comments add
*/
function commentAddCallback(options) {
	var error = options.error;
	var errorDesc = options.error_desc;

	if (!error) {
		$('commentsError').innerHTML = "";
		$('commentBox').value = "";
		showComments (options.comments, options.navigator, 'comments');
		return true;
	} else {
		$('commentsError').innerHTML = errorDesc;
	}
}


/*
	FUNCTION AJAX: change rating
*/
function set_vote (rating) {
	$('rating').value = rating;
	SendFormByAjax ('set_file_rating', 'fileRatingForm', set_vote_Callback); 
}


/*
	FUNCTION AJAX CALLBACK: modify rating
*/
function set_vote_Callback (options) {

	var error = options.error;
	if (error) {
		$('error').innerHTML = options.error_desc;
	} else {
		$('error').innerHTML = "";
		$('ratingImage').innerHTML = '<b>Rate: </b>' + options.rating;
		$('ratingVotes').innerHTML = '(' + options.votes +' ratings)';
		$('fileRating').innerHTML = "";
		$('fileRatingForm').innerHTML = "";
		$hide('vote_here');
	}
}



/*
	FUNCTION AJAX CALLBACK: comments add
*/
function showComments (comments, navigator, tableId) {
	var content = "<table border=\"0\" cellpadding=\"5\" cellspacing=\"0\" width=\"500\">";

	if (comments) {
		for (var i = 0; i < comments.length; i++) {
			content += '<tr><td>'+comments[i].user_login+'&nbsp;&nbsp;&nbsp;'+comments[i].comment_add_date+'</td></tr><tr><td>'+comments[i].comment_text+'</td></tr><tr><td><hr/></td></tr>';
		}
	}
	content += '<tr><td style="text-align:right;" colspan="2">';

	if (navigator) {
		var first_link = navigator.first_link;
		var prev_link = navigator.prev_link;

		var next_link = navigator.next_link;
		var last_link = navigator.last_link

		/* write first link*/
		if (first_link) {
			content += '<a href="'+first_link+'"><b>&#171;&#171;</b></a>&nbsp;';
		} else {
			content +='<b>&#171;&#171;</b>&nbsp;';
		}
		/* write preview link*/
		if (prev_link) {
			content += '<a href="'+prev_link+'"><b>&#171;</b></a>&nbsp;\n';
		} else {
			content +='<b>&#171;</b>&nbsp;\n';
		}

		/* write pages*/
		for (var i = 0; i < navigator.pages.length; i++) {
			if (navigator.pages[i].page_link) {
				content += '<a href="'+navigator.pages[i].page_link+'">'+navigator.pages[i].page_num+'</a>&nbsp;\n';
			} else {
				content += navigator.pages[i].page_num+'&nbsp;\n';
			}
		}

		/* write next link*/
		if (next_link) {
			content += '<a href="'+next_link+'"><b>&#187;</b></a>&nbsp;';
		} else {
			content +='<b>&#187;&#187;</b>&nbsp;';
		}
		/* write last link*/
		if (last_link) {
		content += '<a href="'+last_link+'"><b>&#187;&#187;</b></a>';
		} else {
			content +='<b>&#187;&#187;</b>';
		}
	}
	content += '</td></tr></table>';

	$(tableId).innerHTML = content;
	return true;
}


/*
	FUNCTION AJAX CALLBACK: login success
*/
function func_login_success(options) {
	var error = options.error;
	var description = options.error_desc;

	if (!error)
		window.location.reload();
	else
		$('error').innerHTML = description;
	return false;
}


/*
	FUNCTION: show big full star
*/
function vote(point){
	for(var i=1; i<=point; i++){
		$("v_"+i).src="./images/star_big_full.png";
	}
}




/*
	FUNCTION: set big empty star
*/
function vote_clear(point){
	for(var i=1; i<=point; i++){
		$("v_"+i).src="./images/star_big_empty.png";
	}
}


/*
	FUNCTION: show form and other
*/
function showRatingFrom (type) {
	idImage = "ratingImage";
	idForm = "fileRating";
	if (type == 1)
		$(idForm).style.display = "block";

	if (type == 0)
		$(idForm).style.display = "none";
}