//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 == '')
	if (!$('firstfile') || $('firstfile').value  == '' )
	{
		fillPopup("<div class=\"flashmsg error\"><img src=\"/images/ico_error.png\" alt=\"\" align=\"absmiddle\" class=\"pngIco\" />&nbsp;Моля избери файл!</div>");
		showCenteredPopup();
		setTimeout('hidePopup()', 2500);
		return false;
	}

	for (i = 0; i < document.uploadFrm.elements.length; i++) {

		if (document.uploadFrm.elements[i].checked==true ) {
			filled = true;
		}
	}

	if (filesCount==1) {
		word='Снимката се качва'; 
		word2='е тази снимка'; 
	}else {
		word='Снимките се качват'; 
		word2='са тези снимки'; 
	}
	
	if (filled==false) {
		fillPopup("<div class=\"flashmsg error\"><img src=\"/images/ico_error.png\" alt=\"\" align=\"absmiddle\" class=\"pngIco\" />Моля избери на кого "+word2+"!</div>");
		showCenteredPopup();
		setTimeout('hidePopup()', 2500);
		return false;
	}

	hideUserPopup();
	hideOtherPopup();

	showDimmer(true);
	fillPopup("<div style=\"text-align:center;\">"+word+", моля изчакайте...<br /><br /><img src=\"/images/uploadbar.gif\" alt=\"Upload bar...\" width=\"220\" height=\"19\" /></div>");
	showCenteredPopup();

	return true;
}

/* rating */

var myUrl = window.location.href;
imge = myUrl.split("/");
love_id=imge.pop();


function handleRefresh(t) {
	//loadImage(image_src, image_id, image_text);
	//alert (t.responseText);
	//hideModelPopup();
	//hideOtherPopup();
	populatePopup('<span class="success">' + t.responseText + '</span>');
	showCenteredPopup();
	setTimeout('hidePopup()', 2 * 1000);
	showRating(love_id);
}
function handleRefresh2(t) {
	populatePopup('<span class="success">' + t.responseText + '</span>');
	showCenteredPopup();
	setTimeout('hidePopup()', 2 * 1000);
	showMemberRating(love_id);
}

	rate1_text='Много зле...';
	rate2_text='Зле зле...';
	rate3_text='Става...';
	rate4_text='Добре е!';
	rate5_text='Мнооого е добре!';

var setLoveRating = 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="love_vote_1" alt="'+rate1_text+'" title="'+rate1_text+'" onclick="preVoteWithStars(1,\'love_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="love_vote_2" alt="'+rate2_text+'" title="'+rate2_text+'" onclick="preVoteWithStars(2,\'love_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="love_vote_3" alt="'+rate3_text+'" title="'+rate3_text+'" onclick="preVoteWithStars(3,\'love_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="love_vote_4" alt="'+rate4_text+'" title="'+rate4_text+'" onclick="preVoteWithStars(4,\'love_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="love_vote_5" alt="'+rate5_text+'" title="'+rate5_text+'" onclick="preVoteWithStars(5,\'love_vote\');" class="pointer" />';

	//Add voting stars
	//if (users_vote.length > 0) {
	if(  users_vote=="-1" || (users_vote > 0) && (users_vote < 11) ) {
		//if(  users_vote > 0 ) {
		$('love_rating').innerHTML = 'Рейтинг: <span class="rating_num">'+rating+'</span>'+'<br /><img src="/images/ico/rating/gray1.gif" id="love_vote_1" alt="" /><img src="/images/ico/rating/gray2.gif" id="love_vote_2" alt="" /><img src="/images/ico/rating/gray3.gif" id="love_vote_3" alt="" /><img src="/images/ico/rating/gray4.gif" id="love_vote_4" alt="" /><img src="/images/ico/rating/gray5.gif" id="love_vote_5" alt="" />';
		voteWithStars(rating, 'love_vote');
	}
	else {
		$('love_rating').innerHTML = 'Рейтинг: <span class="rating_num">'+rating+'</span>'+rateImages;
	}
	/*}
	else {
	$('love_rating').innerHTML = 'Рейтинг: '+rating+rateImages;
	}*/
}



var setMemberRating = 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="user_vote_1" alt="'+rate1_text+'" title="'+rate1_text+'" onclick="preVoteWithStars(1,\'user_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="user_vote_2" alt="'+rate2_text+'" title="'+rate2_text+'" onclick="preVoteWithStars(2,\'user_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="user_vote_3" alt="'+rate3_text+'" title="'+rate3_text+'" onclick="preVoteWithStars(3,\'user_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="user_vote_4" alt="'+rate4_text+'" title="'+rate4_text+'" onclick="preVoteWithStars(4,\'user_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="user_vote_5" alt="'+rate5_text+'" title="'+rate5_text+'" onclick="preVoteWithStars(5,\'user_vote\');" class="pointer" />';
	
	//Add voting stars
	if(  users_vote=="-1" || (users_vote > 0) && (users_vote < 11) ) {
		$('love_rating').innerHTML = 'Рейтинг: <span class="rating_num">'+rating+'</span>'+'<br /><img src="/images/ico/rating/gray1.gif" id="user_vote_1" alt="" /><img src="/images/ico/rating/gray2.gif" id="user_vote_2" alt="" /><img src="/images/ico/rating/gray3.gif" id="user_vote_3" alt="" /><img src="/images/ico/rating/gray4.gif" id="user_vote_4" alt="" /><img src="/images/ico/rating/gray5.gif" id="user_vote_5" alt="" />';
		voteWithStars(rating, 'user_vote');
	}
	else {
		$('love_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 showUserRating(love_id) {
	new Ajax.Request('/loves/get_user_rating/'+love_id, {method:'get', onSuccess:setLoveRating});		//get photo rating by current user
}
function showRating(love_id) {
	new Ajax.Request('/loves/get_rating/'+love_id, {method:'get', onSuccess:setLoveRating});		//get photo rating by current user
}

function showMemberRating(love_id) {
	new Ajax.Request('/users/get_rating/'+love_id, {method:'get', onSuccess:setMemberRating});		//get photo rating by current user
}


function preVoteWithStars(rating, var_names) {
	voteWithStars(rating, var_names);
	if (var_names == 'love_vote') var url = '/loves/save_rating/'+ love_id;
	if (var_names == 'user_vote') var url = '/users/save_rating/'+ love_id;
	var req_string = 'rating=' + rating;
	if (var_names == 'love_vote') new Ajax.Request(url, {method:'post', postBody:req_string, onSuccess: handleRefresh});
	if (var_names == 'user_vote')  new Ajax.Request(url, {method:'post', postBody:req_string, onSuccess: handleRefresh2});
}

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 verifyDeleteComment() {
    if (confirm("Сигурен/а ли си, че искаш да изтриеш този коментар?")) {
       return true;
    } else {
       return false;
    }
}


/**
	 * Save admin template
	 */
function saveAdminTextPop() {
	//Hide other popup types to avoid bugs;
	hidePopup();
	populatePopup('<div style="positon:relative;"><img src="/images/frame/frame_close.png" alt="close" style="position:absolute; right: 0; top: 0;cursor: pointer" onclick="hideDimmer(true)" /><h6 style="margin: 0;"><span>Enter Title</span></h6><div style="width: 350px; text-align: center;"><form action="" method="GET" onsubmit="saveAdminText()"><input type="text" id="textTitle2" name="title" style="width:200px;" ><br/><img name="send" src="/images/icons/save_off.gif" alt="Save Template" title="Save Template" onmouseover="this.src=getImage(\'save_on\')" onmouseout="this.src=getImage(\'save_off\')" value="Save Text" class="cursor" style="margin-top:10px;" onclick="saveAdminText()" /><img name="cancel" src="/images/icons/cancel_off.gif" alt="Cancel" title="Cancel" onmouseover="this.src=getImage(\'cancel_on\')" onmouseout="this.src=getImage(\'cancel_off\')" value="Cancel" class="cursor" style="margin-top:10px; margin-left:5px;" onclick="hideDimmer(true)" /></form></div>');
	showCenteredPopup(true);
}

function saveAdminText2() {
	$('textTitle').value=$('textTitle2').value;
	hidePopup();
	document.forms.sendFrm.submit();
}

function delAdminText() {
	var ans = confirm('Are You sure You want to delete this template?');
	if (ans == true)
	{
		window.location='/messages/deltext/'+text_id+'/'+$('userId').value+'/';
	}
}
function delAdminText2() {
	var ans = confirm('Are You sure You want to delete this template?');
	if (ans == true)
	{
		window.location='/messages/deltext2/'+text_id+'/'+$('user_email').value+'/';
	}
}

function saveAdminText() {
	$('textTitle').value=$('textTitle2').value;
	document.forms.sendFrm.submit();
}
function setAdminText(num) {
	//Hide other popup types to avoid bugs;
	text_id = num.value;
	if (text_id>-1){
		new Ajax.Request('/messages/gettext/'+text_id, {method: 'get', onSuccess: doLoadText});
	} else {
		$('bookText').value = '';
	}
	if (text_id > 0) $('delBut').style.visibility = 'visible'; else $('delBut').style.visibility = 'hidden';
}

function setAdminTit(num) {
	//Hide other popup types to avoid bugs;
	text_id = num.value;
	if (text_id>-1){
		new Ajax.Request('/messages/gettit/'+text_id, {method: 'get', onSuccess: doLoadTit});
	} else {
		$('textTitle2').value = '';
	}
	if (text_id > 0) $('delBut').style.visibility = 'visible'; else $('delBut').style.visibility = 'hidden';
}

var doLoadText = function (t) {
	$('bookText').value = t.responseText;
}
var doLoadTit = function (t) {
	$('textTitle2').value = t.responseText;
}

function setSelectedTextRange(elm, selectionStart, selectionEnd) {
	if (elm.setSelectionRange) {
		//elm.focus();
		//elm.setSelectionRange(selectionStart, selectionEnd);
	}
	else if (elm.createTextRange) {
		var range = elm.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
	}
}

function setCaretToEnd (elm) {
	setSelectedTextRange(elm, elm.value.length, elm.value.length);
}

function setCaretToStart (elm) {
	setSelectedTextRange(elm, 0, 0);
}

function setCaretToPos (elm, pos) {
	setSelectedTextRange(elm, pos, pos);
}
function swapColor(id, bgcolor, color) {
  if (bgcolor != '') { document.getElementById(id).style.backgroundColor = bgcolor; }
  if (color != '') { document.getElementById(id).style.color = color; }
}

function swapDecoration(id, decoration) {
  document.getElementById(id).style.textDecoration = decoration;
}

