
$(document).ready(
	function(){		
		
		if (logged){
			var polling = new Polling();	
			polling.start();
		}
		
		$("#search_input").autocomplete('ajax_search.php', {
			matchContains: true,
			minChars: 2,
			width: 300,		
			multiple: true,	
			selectFirst: false,
			formatItem: function(itemStr) {
				var item = eval("("+itemStr+")");
				
				var html = [];
				
				html.push("<table width='100%'><tr>");
				
				if(item.type == "User") {
					html.push("<td width='50px'><img src='"+item.thumb+"' width='50px' height='50px'/></td>");
				} else {
					html.push("<td width='50px'>");
					html.push(	"<div style='height:50px;width:50px;background-image:url("+item.thumb+");background-repeat:none;'></div>");
					html.push("</td>");
				}
				
				html.push("<td>"+item.text+(item.login?" <span style='font-size: 10px;'>"+item.login+"</span>":"")+" <br/><span style='font-size: 9px;'>"+_LG["lg_"+item.type.toLowerCase()].toLowerCase()+"</span></td>");
				html.push("</tr></table>");
				
				return html.join("");
			}
		}).result(function(event, itemStr) {
			var item = eval("("+itemStr+")");
		
			$("#search_input").attr("value", item.text);
			location.href = item.url;
		});

	
	
		$("#search_input").keypress(function(e) {
											 if(e.which == 13) {
												if($(".ac_over").length == 0) {
													doSearch();														
												}														
											  }
											});
	}
);


function showSignInDialog(relEl) {
	var x, y, w = 238, h = 145;
	
	x = Util.findElemX(relEl) + relEl.offsetWidth - w;
	y = Util.findElemY(relEl) + relEl.offsetHeight + 9;
	
	var html = [];

	html.push('<form action="login.php" method="post">');
	html.push('<table width="220" border="0" align="center" cellpadding="4" cellspacing="0">');
	html.push(  '<tr>');
	html.push(    '<td width="50%" class="text_bold">'+_LG.lg_username+':</td>');
	html.push(    '<td width="50%"><input name="login" type="text" class="login_input regular_text" id="textfield" /></td>');
	html.push(  '</tr>');
	html.push(  '<tr>');
	html.push(    '<td class="text_bold">'+_LG.lg_password+':</td>');
	html.push(    '<td><input name="password" type="password" class="login_input regular_text" id="textfield2" /></td>');
	html.push(  '</tr>');
	html.push(  '<tr>');
	html.push(    '<td></td>');
	html.push(    '<td><input name="doLogin" type="submit" class="btn_1 text_bold" id="button" value="'+_LG.lg_signin+'" />');
	html.push(      '<span class="regular_text" style="margin:0px; vertical-align:bottom; padding-left:6px;">');
	html.push(      '<input type="checkbox" name="remember" value="1" id="checkbox" style="margin-bottom:0px; vertical-align:bottom;"  />'+_LG.lg_keep_logged+'</span></td>');
	html.push(  '</tr>');
	
	html.push(  '<tr>');
	html.push(    '<td></td>');
	html.push(      '<td align="left"><a href="reset.php" class="blue_links">'+_LG.lg_forgot_pass+'</a></td>');
	html.push(  '</tr>');
	
	
	html.push('</table>');
	html.push('</form>');
	
	var params = {
			content: html.join(''),
			x: x, y: y, w: w, h: h,
			opacity: 0.9,
			type: "Standard"			
	};
	
	Dialog.show('sign_in', params);	
}


function showMoreCategoryDialog(relEl) {
	var x, y, w = 200, h = "auto";
	
	x = Util.findElemX(relEl);
	y = Util.findElemY(relEl) + relEl.offsetHeight + 5;
	
	var html = [];
	
	html.push('<table width="100%" border="0" cellspacing="0" cellpadding="0" class="categories" id="categories" style="padding:0px 2px;">');
	
	var cat;
	for(var i=0; i<window.moreCategories.length; i++) {
		cat = window.moreCategories[i];		
		html.push('<tr>');
		html.push(	'<td width="72%" ><a href="bygenre.php?index='+cat.id+'&featured=1" class="blue_links">'+cat.name+'</a></td>');
		html.push(	'<td class="regular_text">('+cat.count+')</td>');
		html.push('</tr>');		
	}
	
	html.push('</table>');
    
	var params = {
		content: html.join(''),
		x: x, y: y, w: w, h: h,
		opacity: 0.9,
		type: "Standard"
	};
	
	Dialog.show('more_cat', params);	
}

function showPhotoInfo(photoEl, authorInfo){
	var x, y, w = 220, h = 70;

	x = Util.findElemX(photoEl) + photoEl.offsetWidth - Math.round(photoEl.offsetWidth*0.4);
	y = Util.findElemY(photoEl) + photoEl.offsetHeight - Math.round(photoEl.offsetHeight*0.2);
	
	var recs = [];
	
	if (authorInfo.pwk=='1'){
		recs.push('<img width="13" src="tpl/img/pwk.gif" title="" />');
	}
	
	if (authorInfo.pmo=='1'){
		recs.push('<img width="13" src="tpl/img/pmo.gif" title="" />');
	}
	
	if (authorInfo.edp=='1'){
		recs.push('<img width="13" src="tpl/img/edp.gif" title="" />');
	}	
	
	var html = [];
	html.push('<table border="0" cellspacing="0" cellpadding="0" class="tooltipTable">');	
	html.push('<tr>');	
	html.push(' <td valign="top"><img src="'+authorInfo.authorPicSmall+'" width="50" height="50" align="left" /></td>');	
	html.push('<td><a href="info.php?user_id='+authorInfo.user_id+'" class="blue_links"><span class="text_bold"> '+authorInfo.name+'</span></a><br />');	
	html.push(recs.join(' '));
	html.push(' &quot;'+authorInfo.photo+'&quot;<br />');		
	html.push('');	
	html.push(''+authorInfo.date+'<br />');	
	html.push('</td></tr>');	
	html.push('</table>');	
	

	
	var params = {
			content: html.join(''), 
			x: x, y: y, w: w, h: h,
			type: "Tooltip",
			opacity: 0.9,
			shadow: false
		}
	
	Dialog.show("tooltip-"+photoEl.id, params);			
}


function showPhotoDetails(photoEl, PhotoInfo){
	var x, y, w = 200, h = 60;

	x = Util.findElemX(photoEl) + photoEl.offsetWidth - Math.round(photoEl.offsetWidth*0.4);
	y = Util.findElemY(photoEl) + photoEl.offsetHeight - Math.round(photoEl.offsetHeight*0.2);
	
	var recs = [];
	
	if (PhotoInfo.pwk=='1'){
		recs.push('<img src="tpl/img/pwk.gif" title="" />');
	}
	
	if (PhotoInfo.pmo=='1'){
		recs.push('<img src="tpl/img/pmo.gif" title="" />');
	}
	
	if (PhotoInfo.edp=='1'){
		recs.push('<img src="tpl/img/edp.gif" title="" />');
	}		
	
	var html = [];
	
	html.push('<table border="0" cellspacing="0" cellpadding="0" class="tooltipTable regular_text">');	
	html.push(' <tr><td valign="top" >');	
	html.push('<div>'+recs.join(" ")+' <b>'+PhotoInfo.photo+'</b></div>');
	html.push('<div style="padding-top:2px;">'+_LG.lg_likes+': <b>'+PhotoInfo.likes+'</b> /');
	html.push(' '+ _LG.lg_comments2+' : <b>'+PhotoInfo.comments+'</b> /');
	html.push(' '+ _LG.lg_views+' : <b>'+PhotoInfo.view+'</b></div>');
	html.push('<div style="padding-top:2px;">'+_LG.lg_posted+': '+PhotoInfo.date+'</div>');
	html.push('</td></tr>');
	html.push('</table>');	
	
	var params = {
			content: html.join(''), 
			x: x, y: y, w: w, h: h,
			type: "Tooltip",
			opacity: 0.9,
			shadow: false
		}
	
	Dialog.show("tooltip-"+photoEl.id, params);	
}

function showPhotoThumb(el, thumbSrc){
	var x, y, w = "auto", h = "auto";

	x = Util.findElemX(el) + el.offsetWidth - Math.round(el.offsetWidth*0.4);
	y = Util.findElemY(el);
	
	var html = [];
	html.push('<table border="0" cellspacing="0" cellpadding="0" class="tooltipTable">');	
	html.push('<tr>');	
	html.push(' <td valign="middle" align="center" style="padding: 5px;">');
	html.push('<img src="'+thumbSrc+'" />');	
	html.push('</td>');	
	html.push('</td></tr>');	
	html.push('</table>');	
	
	var params = {
			content: html.join(''), 
			x: x, y: y, w: w, h: h,
			type: "Tooltip",
			shadow: false
		}
	
	Dialog.show("tooltip-"+el.id, params);			
}


function showPhotoTooltip(el, data,type){
	if (type=='author')
		showPhotoInfo(el, data);
	else 
		showPhotoDetails(el, data);
}

function hidePhotoInfo(evt, photoEl) {
	
	if (!evt){
		evt = window.event;	
	}
	
	var px = Util.mouseX(evt), py = Util.mouseY(evt), imgx = Util.findElemX(photoEl), imgy = Util.findElemY(photoEl);

	if ( px > imgx && px < imgx+ photoEl.offsetWidth && py > imgy && py < imgy+ photoEl.offsetHeight) {
		var tt = document.getElementById("tooltip-"+photoEl.id);
		if(tt != null) {
			tt.onmouseout = function(evt){ 
				if (!evt){
					evt = window.event;	
				}				

				var target = evt.target ? evt.target : evt.srcElement;
				var cx = Util.mouseX(evt), cy = Util.mouseY(evt), ttx = Util.findElemX(tt), tty = Util.findElemY(tt);

				if ( !(cx > ttx && cx < ttx+ tt.offsetWidth && cy > tty && cy < tty+ tt.offsetHeight)) {
					Dialog.hide(tt.id);
				}
			};
		}
		return;
	}
	
	Dialog.hide("tooltip-"+photoEl.id);
}

function toggleEditUserPicMenu(evt) {
	if(!isCurrentUser) {
		return false;
	}
	
	if (!evt){
		evt = window.event;	
	}

	var editUserPic = $id("editUserPic");
	
	if (evt.type == 'mouseover')
		editUserPic.style.display = '';
	else if(evt.type == 'mouseout'){
		var cx = evt.pageX, cy = evt.pageY, ex = Util.findElemX(editUserPic), ey = Util.findElemY(editUserPic);
		if (cx < ex || cx > ex + editUserPic.offsetWidth || cy < ey || cy > ey+ editUserPic.offsetHeight)
			editUserPic.style.display = 'none';
	}
}

function toggleUploadUserPicForm(){
	
	var el = $id('uploadUserPicForm');
	
	if(el.style.display=='none')
		el.style.display='block';
	else
		el.style.display='none';
}

function setInputValue(inpId, value) {
	var inp = $id(inpId);
	if(inp != null) {
		inp.value = value;
	}
}

function showSendMsgDialog(recipInfo) {
		var w = 375, h = 283;		
		var dlgId = 'SendMsg'+recipInfo.id;
		var html = [];

		html.push('<table width="100%" border="0" cellspacing="8" cellpadding="0">	');
		html.push(	'<tr>');
		html.push(		'<td id="'+recipInfo.id+'_body" class="regular_text">');
		html.push(		  _LG.lg_to+': '+recipInfo.name+'<br/>');
		html.push(			'<textarea class="input msgBody" id="'+recipInfo.id+'_msgTxt"></textarea>');
		html.push(		'</td>');				
		html.push(	'</tr>');		
		html.push('</table>');
		
		var params = {
						content: html.join(''),
						w: w, h: h,
						type: "Standard",
						buttons : [
					   		{label: _LG.lg_send, onclick: "processSendMessage(\'"+recipInfo.id+"\')", id: recipInfo.id+'_OK'},
							{label: _LG.lg_cancel, onclick: "Dialog.hide(\'"+dlgId+"\')", id: recipInfo.id+'_Cancel'},
						]};
		


		Dialog.show(dlgId, params);
}

var processSendMessage = function(recipId) {
	var msgBody = $.trim($("#"+recipId+"_msgTxt").attr("value"));
	if(!msgBody) {
		return;
	}
	
	$.ajax({
		  url: 'ajax_messenger.php?recipient='+recipId+"&message="+encodeURIComponent(msgBody),
		  success: function(data) {
			var resp = eval("("+data+")");				  
			if(resp.success) {
				$("#"+recipId+"_Cancel").remove();
				$id(recipId+"_OK").onclick=function(){Dialog.hide("SendMsg"+recipId)};
				$id(recipId+"_OK").value="OK";
				$("#"+recipId+"_body").html(_LG.lg_mail_sent_succ);				
				
				var dlgId = 'SendMsg'+recipId;
				$("#"+dlgId).css("height", "90px");
				
				Dialog.recalcSize(dlgId);				
			}
		  }
		});			
}

function doSearch() {
	var query = $.trim($("#search_input").attr("value"));

	if(query == "") {
		return;
	}
	
	document.location.href = "search.php?q="+query;
}

function resendEmail(email) {
	var url = "ajax_send_mail.php?type=resend";
	$.ajax({
		  "url": url,
		  "success": function(data) {
			var resp = eval("("+data+")");				  
			if(resp.success) {
			var content = '<span style="padding-left: 15px">'+_LG.lg_activation_sent+' <b>'+email+'</b>.</span> <br/><span style="padding-left: 15px">'+_LG.lg_check_spam2+'</span>';

	var params = {
		content: content,
		w:350,
		buttons: [{label:"OK"}]
	}
	
	Dialog.show("resendMail", params)				

			}
		  }
		});		
}

function confirmAddRemoveFollower(flwId, type) {
	var text = _LG.lg_areyou_sure+' '+followText.toLowerCase()+" "+_LG.lg_this_user;
	
	var params = {
		content: text,
		buttons: [{label: "OK", onclick: "Dialog.hide(\'Follow\');processAddRemoveFlw("+flwId+", \'"+type+"\')"},
					{label:_LG.lg_cancel}
				]
	}			
	
	Dialog.show('Follow', params);	
}

function processAddRemoveFlw(flwId, type) {
		$.ajax({
			  url: 'ajax_follow.php?follow='+flwId,
			  success: function(data) {
				var resp = eval("("+data+")");				  
				if(resp.success) {
					if(type == "profile") {
						followText = resp.act > 0 ? _LG.lg_unfollow_author : _LG.lg_follow_author;
						
						var newCnt = eval($("#flwingCount").html()+"+("+resp.act+")");
						$("#flwingCount").html(newCnt);
						$("#followLink").html(followText);
					}
				}
			  }
			});		
}

function showHideNotifications(relEl) {
	var notfDlgEl = document.getElementById('notf');
	if(notfDlgEl != null) {
		Dialog.hide("notf");
		return;
	}
	
	var x, y, w = 288, h = "auto";
	
	x = Util.findElemX(relEl);
	y = Util.findElemY(relEl) + relEl.offsetHeight + 5;
	
	var html = [];
	
	if(window.notifications.length > 0) {	
		html.push('<table width="100%" border="0" cellspacing="0" cellpadding="5" class="categories">');
		
		var notf;
		for(var i=0; i<window.notifications.length; i++) {
			notf = window.notifications[i];		
			html.push('<tr>');
			html.push(	'<td class="regular_text">'+notf.text+'</td>');
			html.push('</tr>');		
		}
		
		html.push('<tr>');
		html.push(	'<td align="center" class="notf_more"><a href="notifications.php" class="blue_links text_bold">'+_LG.lg_see_all_notf+'</td>');
		html.push('</tr>');				
		html.push('</table>');
	} else {
		html.push('<div style="padding: 7px 0px 7px 3px;">'+_LG.lg_no_notf+'</div>');
	}
	
	var params = {
		content: html.join(''), 
		x: x, y: y, w: w, h: h,
		type: "Standard", opacity: 0.9
	}			  
	Dialog.show('notf', params);	
	
	$("#notf_count").html("");	
	$.ajax({"url": "ajax_polling.php?unsetNotf=1","success": function() {}});		
	
}

var Polling = function() {
	var interval = 15*1000;
	var intId = null;
	
	function sendRequest() {
	var url = "ajax_polling.php?getNotf=1";
	$.ajax({
		  "url": url,
		  "success": function(data) {
			var resp = eval("("+data+")");				  
			if(resp.success) {
				var dataNotf = resp.dataNotf;
				
				window.notifications = dataNotf["Notf"];			
				
				var totalNotf = dataNotf["totalUnreadNotf"] != 0 ?  dataNotf["totalUnreadNotf"] : "";
				$("#notf_count").html(totalNotf);
				
				if(resp.dataInbox) {
					$("#inbox_count").html("("+resp.dataInbox+")");
					$("#inbox_count").css("display", "inline");
				} else {
					$("#inbox_count").css("display", "none");
				}
			}
		  }
		});		
	}
	
	this.start = function() {
		intId = window.setInterval(sendRequest, interval);
	}
	
	this.terminate = function() {
		if(intId != null) {
			window.clearInterval(intId);
		}
		
	}
}

function changeCaptcha(){
	$("#signup_captcha").attr("src", "chknum.php?r="+Math.random());
	$("#captcha").attr("value","");
}

window._allInputAreas = {};
var ExpandableInput = function(el, params) {		
	var self = this;	
	
	if(!el) {
		return false;
	}
	
	el = $(el);
	
	var defaultMessage = params.defaultMessage ? params.defaultMessage :_LG.lg_share_text;	
	var openedHeight = params.openedHeight ? params.openedHeight : 75;
	var closedHeight = params.closedHeight ? params.closedHeight : 40;
	var bottomContId = params.bottomContId ? params.bottomContId : "shareBottomCont";
	var  bottomCont = $("#"+bottomContId);			
	
	function boxOnFocus(event) {
		var target = $(event.target);
		
		var cId = el.attr('id').replace(/fc_/gi, "");
		for(var k in _allInputAreas) {
			if(k != cId) {
				_allInputAreas[k].hideWhenOut({target:document.body});
			}
		}
		
		target.css("height", openedHeight+"px");
		
		bottomCont.css("display", "block");		
		
		$(document).bind("click", self.hideWhenOut);	
	
		if(isStatusEmpty()) {
			target.attr("value", "");
			target.css("color", "#000000");
		}
	}

	function isStatusEmpty() {
		var statVal = el.attr("value");
		return ($.trim(statVal) == "") || statVal == defaultMessage;
	}

	this.hideWhenOut = function(evt) {			
		var target = $(evt.target);
		
		if(!isFromBottomCont(target)) {	
			bottomCont.css("display", "none");		
			
			$(document).unbind("click", self.hideWhenOut);	
			
			if(target.attr("id") == el.attr("id")) {
				boxOnFocus(target);
			} else {
				el.css("height", closedHeight+"px");	
	
				if(isStatusEmpty()) {
					el.attr("value", defaultMessage);
					el.css("color", "#777777");	
				}
			}	
		} 
	}

	function isFromBottomCont(el) {
		while(el.parent().length > 0) {
			if(el.attr("id") == bottomContId) {
				return true;
			}
			
			el = el.parent();
			}
	
		return false;		
	}
	
	
	el.bind("focus", boxOnFocus);
}

var showLoading = function(id, msg) {
	var el;
	if(typeof id == "string") {
		el = $("#"+id);
	} else {
		el = $(id);
	}
	
	if(!el) {
		return;
	}
	
	if(!msg) {
		msg = "";
	}
	
	el.html("<img src='tpl/img/ajax-loader-small.gif'> " + msg);
}

var clearLoading = function(id) {
	var el;
	if(typeof id == "string") {
		el = $("#"+id);
	} else {
		el = $(id);
	}
	
	if(!el) {
		return;
	}
	
	el.html("");
}