//var modelProfileVisible = null;
var specailImg = null;


var otherProfileVisible = null;

function showOtherPopup(data, image) {
	hidePopup();
	if (typeof data == 'object') {
		imageCell = '';
		if (typeof image != 'undefined') {
			imageCell = "<td style='width:auto !important;'><img src=\"/files/"+image+"\" /></td>";
		}else{
			image = '';
		}

		setOnScreen();

		otherProfileVisible = true;
		populatePopup('<table class="loveInfo"><tr>'+imageCell+'<td valign=top><table cellpadding=3><tr>'+
		'<th>Име:</th><td class="capitalise">'+data.name+'</td></tr>'+
		'<tr><th>Гадже на:</th><td>'+data.user_name+'</td></tr>'+
		'<tr><th>Град:</th><td class="capitalise">'+data.town+'</td></tr>'+
		'<tr><th>Години:</th><td>'+data.age+'</td></tr></table></td></tr></table>', true);
	}
}

var userProfileVisible = null;
function showUserPopup(data, image) {
	hidePopup();
	if (typeof data == 'object') {
		if (image=="") files='/images/no_img.gif'; else files="/files/";
		imageCell = "<td align=\"center\" style='width:auto !important;'><img src=\"/images/"+data.online+".gif\"/><br/><img class=\"border2\" src=\""+files+image+"\" /></td>";
		setOnScreen();
		userProfileVisible = true;
		populatePopup('<table class="loveInfo"><tr>'+imageCell+'<td valign=top><table cellpadding=3 style="margin-top:13px;"><tr>'+
		'<th>Име:</th><td class="capitalise">'+data.name+'</td></tr>'+
		'<tr><th>Град:</th><td class="capitalise">'+data.town+'</td></tr>'+
		'<tr><th>Години:</th><td>'+data.age+'</td></tr></table></td></tr></table>', true);
	}
}

function hidePops() {
	//hideModelPopup();
	hideUserPopup();
	hideOtherPopup();
}

function hideOtherPopup() {
	otherProfileVisible = false;
	//modelProfileVisible = false;
	hidePopup();
}

function hideUserPopup() {
	userProfileVisible = false;
	//modelProfileVisible = false;
	hidePopup();
}

function showProfileImg(type, image, data) {
	if (type=='love') {
		showLovePopup(data, image);
	}else if (type == 'other') {
		showOtherPopup(data, image);
	}
}
function setErrorMessage(innerText) {
	errorMessage = innerText;
}

function loadRequestPopup(id, user_name) {
	hideOtherPopup();
	hideUserPopup();
	$('iframe').style.display='block';
	populatePopup('<table cellspacing="2" cellpadding="2" border="0"><tr><td align=center>Ако сте сигурен, че това сте вие натиснете ДА,<br/> и ще бъде изпратено съобшение на '+user_name+' за потвърждение. <br /></td></tr><tr><td align="center" class="relative"><input type="button" class="button" onclick="window.location=\'/loves/send_request/'+id+'\'" value="Да, това съм аз!" /> <input type="button" class="button relative" value="Не" onclick="hideDimmer(true);" /></td></tr></table>');
	showCenteredPopup();
}

function loadEditPicPopup(picID) {
	//Hide other popup types to avoid bugs;
	hideModelPopup();
	hideOtherPopup();
	hidePopup();

	populatePopup('<h6 style="margin: 0;"><span>Edit picture</span></h6><div id=\"editPicContent\" style=\"width: 350px; text-align: center;\">Loading picture data...');

	new Ajax.Request('/photos/edit_pic/'+picID, {method: 'get', onSuccess: doLoadEditPic});

	showCenteredPopup(true);
}

var doLoadEditPic = function (t) {
	$('editPicContent').innerHTML = t.responseText;
	reCenterPopup();
	//popupLocked = true;
}

function hideEditPicPopup()
{
	hideDimmer(true);
}



function loadSpecialPicPopup(picurl) {
	//Hide other popup types to avoid bugs;
	//hideModelPopup();
	hideOtherPopup();
	hidePopup();
	populatePopup( '<div id=\"specialPicContent\"><img src=\"/images/preloader.gif\" /></div>' )
	showCenteredPopup(true);

	specailImg = new Image();
	specailImg.onload = loadSpecailPic;
	specailImg.src = picurl;
}

function loadSpecailPic()
{
	$('specialPicContent').innerHTML = '<img src="'+specailImg.src+'"  style="border:3px solid black" />';
	reCenterPopup();
}

function showUploadBar() {

	var pic = document.getElementById('file').value;
	filled = false;
	if(pic == '')
	{
		fillPopup("<div class=\"flashmsg error\"><img src=\"/images/ico_error.png\" alt=\"\" align=\"absmiddle\" class=\"pngIco\" />&nbsp;Choose file!</div>");
		showCenteredPopup();
		setTimeout('hidePopup()', 2500);
		return false;
	}

	hideUserPopup();
	hideOtherPopup();

	showDimmer(true);
	fillPopup("<div style=\"text-align:center;\">Please wait...<br /><br /><img src=\"/images/uploadbar.gif\" alt=\"Upload bar...\" width=\"220\" height=\"19\" /></div>");
	showCenteredPopup();

	return true;
}

/* game rating */
var myUrl = window.location.href;
imge = myUrl.split("/");
nums = imge.length;
game_id=imge[nums-2];

function handleRefresh(t) {
	populatePopup('<span class="success">' + t.responseText + '</span>');
	showCenteredPopup();
	setTimeout('hidePopup()', 2 * 1000);
	showGameRating(game_id);
}
function handleRefresh2(t) {
	populatePopup('<span class="success">' + t.responseText + '</span>');
	showCenteredPopup();
	setTimeout('hidePopup()', 2 * 1000);
	showPicRating(game_id);
}

function handleRefresh3(t) {
	populatePopup('<span class="success">' + t.responseText + '</span>');
	showCenteredPopup();
	setTimeout('hidePopup()', 2 * 1000);
	showVideoRating(game_id);
}


	rate1_text='1';
	rate2_text='2';
	rate3_text='3';
	rate4_text='4';
	rate5_text='5';

var setGameRating = function (t) {

	vars = t.responseText;
	vars = vars.toString();
	var temp = new Array();
	temp = vars.split(',');
	var users_vote;
	var rating;
	rating=temp[0];
	users_vote=temp[1];
	rateImages='<br /><img src="/images/ico/rating/gray1.gif" onmouseover="this.src=&quot;/images/ico/rating/over1.gif&quot;" onmouseout="this.src=&quot;/images/ico/rating/gray1.gif&quot;" id="game_vote_1" alt="'+rate1_text+'" title="'+rate1_text+'" onclick="preVoteWithStars(1,\'game_vote\');" class="pointer" /><img src="/images/ico/rating/gray2.gif" onmouseover="this.src=&quot;/images/ico/rating/over2.gif&quot;" onmouseout="this.src=&quot;/images/ico/rating/gray2.gif&quot;" id="game_vote_2" alt="'+rate2_text+'" title="'+rate2_text+'" onclick="preVoteWithStars(2,\'game_vote\');" class="pointer" /><img src="/images/ico/rating/gray3.gif" onmouseover="this.src=&quot;/images/ico/rating/over3.gif&quot;" onmouseout="this.src=&quot;/images/ico/rating/gray3.gif&quot;" id="game_vote_3" alt="'+rate3_text+'" title="'+rate3_text+'" onclick="preVoteWithStars(3,\'game_vote\');" class="pointer" /><img src="/images/ico/rating/gray4.gif" onmouseover="this.src=&quot;/images/ico/rating/over4.gif&quot;" onmouseout="this.src=&quot;/images/ico/rating/gray4.gif&quot;" id="game_vote_4" alt="'+rate4_text+'" title="'+rate4_text+'" onclick="preVoteWithStars(4,\'game_vote\');" class="pointer" /><img src="/images/ico/rating/gray5.gif" onmouseover="this.src=&quot;/images/ico/rating/over5.gif&quot;" onmouseout="this.src=&quot;/images/ico/rating/gray5.gif&quot;" id="game_vote_5" alt="'+rate5_text+'" title="'+rate5_text+'" onclick="preVoteWithStars(5,\'game_vote\');" class="pointer" />';

	//Add voting stars
	if(  users_vote=="-1" || (users_vote > 0) && (users_vote < 11) ) {
		$('game_rating').innerHTML = 'Рейтинг: <span class="rating_num">'+rating+'</span>'+'<br /><img src="/images/ico/rating/gray1.gif" id="game_vote_1" alt="" /><img src="/images/ico/rating/gray2.gif" id="game_vote_2" alt="" /><img src="/images/ico/rating/gray3.gif" id="game_vote_3" alt="" /><img src="/images/ico/rating/gray4.gif" id="game_vote_4" alt="" /><img src="/images/ico/rating/gray5.gif" id="game_vote_5" alt="" />';
		voteWithStars(rating, 'game_vote');
	}
	else {
		$('game_rating').innerHTML = 'Рейтинг: <span class="rating_num">'+rating+'</span>'+rateImages;
	}
}

function showGameRating(game_id) {
	new Ajax.Request('/games/get_rating/'+game_id, {method:'get', onSuccess:setGameRating});
}


var setPicRating = function (t) {
	vars = t.responseText;
	vars = vars.toString();

	var temp = new Array();
	temp = vars.split(',');

	var users_vote;
	var rating;

	rating=temp[0];
	users_vote=temp[1];
	rateImages='<br /><img src="/images/ico/rating/gray1.gif" onmouseover="this.src=&quot;/images/ico/rating/over1.gif&quot;" onmouseout="this.src=&quot;/images/ico/rating/gray1.gif&quot;" id="photo_vote_1" alt="'+rate1_text+'" title="'+rate1_text+'" onclick="preVoteWithStars(1,\'photo_vote\');" class="pointer" /><img src="/images/ico/rating/gray2.gif" onmouseover="this.src=&quot;/images/ico/rating/over2.gif&quot;" onmouseout="this.src=&quot;/images/ico/rating/gray2.gif&quot;" id="photo_vote_2" alt="'+rate2_text+'" title="'+rate2_text+'" onclick="preVoteWithStars(2,\'photo_vote\');" class="pointer" /><img src="/images/ico/rating/gray3.gif" onmouseover="this.src=&quot;/images/ico/rating/over3.gif&quot;" onmouseout="this.src=&quot;/images/ico/rating/gray3.gif&quot;" id="photo_vote_3" alt="'+rate3_text+'" title="'+rate3_text+'" onclick="preVoteWithStars(3,\'photo_vote\');" class="pointer" /><img src="/images/ico/rating/gray4.gif" onmouseover="this.src=&quot;/images/ico/rating/over4.gif&quot;" onmouseout="this.src=&quot;/images/ico/rating/gray4.gif&quot;" id="photo_vote_4" alt="'+rate4_text+'" title="'+rate4_text+'" onclick="preVoteWithStars(4,\'photo_vote\');" class="pointer" /><img src="/images/ico/rating/gray5.gif" onmouseover="this.src=&quot;/images/ico/rating/over5.gif&quot;" onmouseout="this.src=&quot;/images/ico/rating/gray5.gif&quot;" id="photo_vote_5" alt="'+rate5_text+'" title="'+rate5_text+'" onclick="preVoteWithStars(5,\'photo_vote\');" class="pointer" />';

	//Add voting stars
	if(  users_vote=="-1" || (users_vote > 0) && (users_vote < 11) ) {
		$('game_rating').innerHTML = 'Рейтинг: <span class="rating_num">'+rating+'</span>'+'<br /><img src="/images/ico/rating/gray1.gif" id="photo_vote_1" alt="" /><img src="/images/ico/rating/gray2.gif" id="photo_vote_2" alt="" /><img src="/images/ico/rating/gray3.gif" id="photo_vote_3" alt="" /><img src="/images/ico/rating/gray4.gif" id="photo_vote_4" alt="" /><img src="/images/ico/rating/gray5.gif" id="photo_vote_5" alt="" />';
		voteWithStars(rating, 'photo_vote');
	}
	else {
		$('game_rating').innerHTML = 'Рейтинг: <span class="rating_num">'+rating+'</span>'+rateImages;
	}
}

var setVideoRating = function (t) {
	vars = t.responseText;
	vars = vars.toString();
	var temp = new Array();
	temp = vars.split(',');
	var users_vote;
	var rating;
	rating=temp[0];
	users_vote=temp[1];
	rateImages='<br /><img src="/images/ico/rating/gray1.gif" onmouseover="this.src=&quot;/images/ico/rating/over1.gif&quot;" onmouseout="this.src=&quot;/images/ico/rating/gray1.gif&quot;" id="video_vote_1" alt="'+rate1_text+'" title="'+rate1_text+'" onclick="preVoteWithStars(1,\'video_vote\');" class="pointer" /><img src="/images/ico/rating/gray2.gif" onmouseover="this.src=&quot;/images/ico/rating/over2.gif&quot;" onmouseout="this.src=&quot;/images/ico/rating/gray2.gif&quot;" id="video_vote_2" alt="'+rate2_text+'" title="'+rate2_text+'" onclick="preVoteWithStars(2,\'video_vote\');" class="pointer" /><img src="/images/ico/rating/gray3.gif" onmouseover="this.src=&quot;/images/ico/rating/over3.gif&quot;" onmouseout="this.src=&quot;/images/ico/rating/gray3.gif&quot;" id="video_vote_3" alt="'+rate3_text+'" title="'+rate3_text+'" onclick="preVoteWithStars(3,\'video_vote\');" class="pointer" /><img src="/images/ico/rating/gray4.gif" onmouseover="this.src=&quot;/images/ico/rating/over4.gif&quot;" onmouseout="this.src=&quot;/images/ico/rating/gray4.gif&quot;" id="video_vote_4" alt="'+rate4_text+'" title="'+rate4_text+'" onclick="preVoteWithStars(4,\'video_vote\');" class="pointer" /><img src="/images/ico/rating/gray5.gif" onmouseover="this.src=&quot;/images/ico/rating/over5.gif&quot;" onmouseout="this.src=&quot;/images/ico/rating/gray5.gif&quot;" id="video_vote_5" alt="'+rate5_text+'" title="'+rate5_text+'" onclick="preVoteWithStars(5,\'video_vote\');" class="pointer" />';

	//Add voting stars
	if(  users_vote=="-1" || (users_vote > 0) && (users_vote < 11) ) {
		$('game_rating').innerHTML = 'Рейтинг: <span class="rating_num">'+rating+'</span>'+'<br /><img src="/images/ico/rating/gray1.gif" id="video_vote_1" alt="" /><img src="/images/ico/rating/gray2.gif" id="video_vote_2" alt="" /><img src="/images/ico/rating/gray3.gif" id="video_vote_3" alt="" /><img src="/images/ico/rating/gray4.gif" id="video_vote_4" alt="" /><img src="/images/ico/rating/gray5.gif" id="video_vote_5" alt="" />';
		voteWithStars(rating, 'video_vote');
	}
	else {
		$('game_rating').innerHTML = 'Рейтинг: <span class="rating_num">'+rating+'</span>'+rateImages;
	}
}

function showPicRating(game_id) {
	new Ajax.Request('/pictures/get_rating/'+game_id, {method:'get', onSuccess:setPicRating});
}

function showVideoRating(game_id) {
	new Ajax.Request('/videos/get_rating/'+game_id, {method:'get', onSuccess:setVideoRating});
}

function preVoteWithStars(rating, var_names) {
	voteWithStars(rating, var_names);
	if (var_names == 'game_vote') var url = '/games/save_rating/'+ game_id;
	if (var_names == 'photo_vote') var url = '/pictures/save_rating/'+ game_id;
	if (var_names == 'video_vote') var url = '/videos/save_rating/'+ game_id;
	var req_string = 'rating=' + rating;
	if (var_names == 'game_vote') new Ajax.Request(url, {method:'post', postBody:req_string, onSuccess: handleRefresh});
	if (var_names == 'photo_vote')  new Ajax.Request(url, {method:'post', postBody:req_string, onSuccess: handleRefresh2});
	if (var_names == 'video_vote')  new Ajax.Request(url, {method:'post', postBody:req_string, onSuccess: handleRefresh3});
}

function voteWithStars(rating, var_names) {
	rating=rating/2;
	if ((typeof(rating) == 'undefined') || (rating < 1) || (rating > 5)) rating = 0;
	if (typeof(var_names) == 'undefined') {		//voting from a comment (uses hidden variables)
		var_names = 'star_vote';
		document.getElementById(var_names).value = rating;
	}

	for (i = 1; i < 6; i++) {
		if (rating >= i) {
			$(var_names + '_' + i).src = WEBPATH+'images/ico/rating/normal'+ i +'.gif';
		}
		else {
			$(var_names + '_' + i).src = WEBPATH+'images/ico/rating/gray'+ i +'.gif';
		}
	}
}

function checkAvail(username) {
	new Ajax.Request('/users/checkAvail/'+username, {method:'get', onSuccess:setAvailMsg});		//get photo rating by current user
}

var setAvailMsg = function (t) {

	$('avail').innerHTML = t.responseText;

}

function showPanel(obj) {

	var el = document.getElementById(obj);
	el.style.display = el.style.display ? '':'none';

}


var popImg = null;
function popMinifolioImg(img) {
	//Hide other popup types to avoid bugs;
	hideUserPopup();
	hideOtherPopup();

	showDimmer();
	populatePopup('<img src="'+getImage('progress')+'" />');
	showCenteredPopup();

	popImg = new Image();
	popImg.onload = popMinifolioImgOnload;
	popImg.src = '/files/'+img;
}

function popMinifolioImgOnload() {
	populatePopup('<img src="'+popImg.src+'" alt="big image" style="cursor: pointer; position:relative" onclick="hideDimmer(true)" />');
	showCenteredPopup();
}


function showMenu(obj) {
	document.getElementById(obj).style.display = 'block';
}
function hideMenu(obj) {
	document.getElementById(obj).style.display = 'none';
}


function add_to_favorites(id, type, elem) {
	if (typeof elem != 'undefined') {
		elem.firstChild.title = "This user is in your faves";
		elem.firstChild.alt = "This user is in your faves";
		elem.firstChild.src = '/images/ico/favorites.gif';
	}

	if ($('addtofavs')) {
		$('addtofavs').innerHTML = 'In Your Faves';
	}

	if (type == 'picture') {
		new Ajax.Request('/pictures/add_to_favorites/'+id, {method: "GET", onSuccess: addedToFavorites});
	}
	if (type == 'game') {
		new Ajax.Request('/games/add_to_favorites/'+id, {method: "GET", onSuccess: addedToFavorites});
	}
	if (type == 'video') {
		new Ajax.Request('/videos/add_to_favorites/'+id, {method: "GET", onSuccess: addedToFavorites});
	}
}
var addedToFavorites = function (t) {
		/*hidePops();
		fillPopup(t.responseText);
		showCenteredPopup(true);*/
}

function add_to_turnir(id, elem) {
	if (typeof elem != 'undefined') {
		//elem.firstChild.title = "This user is in your faves";
		//elem.firstChild.alt = "This user is in your faves";
		//elem.firstChild.src = '/images/ico/favorites.gif';
	}

	if ($('addtoturnir')) {
		$('addtoturnir').innerHTML = 'Voted';
	}

		new Ajax.Request('/games/add_to_turnir/'+id, {method: "GET", onSuccess: addedToTurnir});
	

}


var addedToTurnir = function (t) {
		/*hidePops();
		fillPopup(t.responseText);
		showCenteredPopup(true);*/
}

/** Load scores for a game **/
function refreshScores(game_id) {

	$('scorelist').innerHTML = '<div style="padding-top:40px; width:100%; text-align:center; color: #666666; font-size:12px"><img src="/images/loader.gif"><br/>Loading scores...</div>';
	new Ajax.Request('/games/scores/'+game_id, {method:'get', onSuccess:doLoadComments});
}

var doLoadComments = function (t) {
	$('scorelist').innerHTML = t.responseText;
}
/** END **/