function function_exists (function_name) {
	if (typeof function_name == 'string'){
        return (typeof this.window[function_name] == 'function');
	} else{
	return (function_name instanceof Function);
}}
	
function findPos(obj) {
 var curleft = curtop = 0;
 if (obj.offsetParent) {
	 curleft = obj.offsetLeft
	 curtop = obj.offsetTop
	 while (obj = obj.offsetParent) {
		 curleft += obj.offsetLeft
		 curtop += obj.offsetTop
	 }
 }
 return [curleft,curtop];
}

function toggle_message(id)
{
	jQuery("#message2table"+id).toggle();
	jQuery("#messagetable"+id).toggle();
	jQuery("#messagediv"+id).toggle();
}

function showEditImageMenu(id, button, type){
	var coords = findPos(button);
	var items = '';

	if (type=='article'){
		items = '<div onmouseover=\'this.style.background="#FF2300"\' onmouseout=\'this.style.background="none"\' onclick=\'setMainPhoto('+id+')\'>Pažymeti kaip pagrindinį</div>';
		items += '<div onmouseover=\'this.style.background="#FF2300"\' onmouseout=\'this.style.background="none"\' onclick=\'unsetMainPhoto('+id+')\'>Nužymeti pagrindinį</div>';
		items += '<div onmouseover=\'this.style.background="#FF2300"\' onmouseout=\'this.style.background="none"\' onclick=\'insertPhoto('+id+', "icon")\'>Įdėti mažą vaizdą į tekstą</div>';
		items += '<div onmouseover=\'this.style.background="#FF2300"\' onmouseout=\'this.style.background="none"\' onclick=\'insertPhoto('+id+', "medium")\'>Įdėti vidutinį vaizdą į tekstą</div>';
		items += '<div onmouseover=\'this.style.background="#FF2300"\' onmouseout=\'this.style.background="none"\' onclick=\'insertPhoto('+id+', "mediumbig")\'>Įdėti didelį vaizdą į tekstą</div>';
		items += '<div onmouseover=\'this.style.background="#FF2300"\' onmouseout=\'this.style.background="none"\' onclick=\'removeImage('+id+')\'>Trinti vaizdą</div>';
		items += '<div onmouseover=\'this.style.background="#FF2300"\' onmouseout=\'this.style.background="none"\' onclick="jQuery(\'#image-menu\').hide()" style="text-align:right">[uždaryti]</div>';
	} else if (type=='page'){
		items = '<div onmouseover=\'this.style.background="#FF2300"\' onmouseout=\'this.style.background="none"\' onclick=\'insertPhoto('+id+', "icon")\'>Įdėti mažą vaizdą į tekstą</div>';
		items += '<div onmouseover=\'this.style.background="#FF2300"\' onmouseout=\'this.style.background="none"\' onclick=\'insertPhoto('+id+', "medium")\'>Įdėti vidutinį vaizdą į tekstą</div>';
		items += '<div onmouseover=\'this.style.background="#FF2300"\' onmouseout=\'this.style.background="none"\' onclick=\'insertPhoto('+id+', "mediumbig")\'>Įdėti didelį vaizdą į tekstą</div>';
		items += '<div onmouseover=\'this.style.background="#FF2300"\' onmouseout=\'this.style.background="none"\' onclick=\'removeImage('+id+')\'>Trinti vaizdą</div>';
		items += '<div onmouseover=\'this.style.background="#FF2300"\' onmouseout=\'this.style.background="none"\' onclick="jQuery(\'#image-menu\').hide()" style="text-align:right">[uždaryti]</div>';
	} else if (type=='profile'){
		items = '<div onmouseover=\'this.style.background="#FF2300"\' onmouseout=\'this.style.background="none"\' onclick=\'setMainPhoto('+id+')\'>Pažymeti kaip pagrindinį</div>';
		items += '<div onmouseover=\'this.style.background="#FF2300"\' onmouseout=\'this.style.background="none"\' onclick=\'removeImage('+id+')\'>Trinti vaizdą</div>';
		items += '<div onmouseover=\'this.style.background="#FF2300"\' onmouseout=\'this.style.background="none"\' onclick="jQuery(\'#image-menu\').hide()" style="text-align:right">[uždaryti]</div>';
	}

	jQuery('#image-menu').html(items);
	jQuery('#image-menu').children('div').css({
			padding:"5px"
	});
	jQuery('#image-menu').css({
			position: "absolute", 
			color: "white",
			cursor: "pointer", 
			background: "#282828",
			padding:"0 0 0 0",
			top: coords[1]+30, 
			left: coords[0]+30, 
			width:"200px" 
	});
	jQuery('#image-menu').show();
}


function delete_mail()
{
	var selected=new Array();
	
	$("input.mailcheck:checkbox:checked").each(function()
	{
		conversation = $(this).attr("id");
		selected.push(conversation.replace(/mailcheck/, ''));
	});
	
	if (selected.length > 0)
	{
		if (!confirm('Really remove checked (' + selected.length + ') emails?' ))
		{
			return false;
		}
		
		label = $('#mail_label').val();
		page = $('#mail_page').val();
		
		jQuery.ajax({
		  type: "POST",
		  url: "/mail/ac/remove/",
		  data: "mailaction=trash&label="+label+"&conversations="+encodeURIComponent(selected),
		  success: function(msg){
			  if (msg && msg > 0)
			  {
					  loadmaillist(label,page);
			  }
		  }
		});
	}	else	{
		alert ('Please, check some messages for removing.');
	}
}

function loadmaillist(label,page)
{
	jQuery('#load').html('Loading ...');
	jQuery.ajax({
		type: "POST",
		url: "/mail/ac/loadmaillist/",
		data: "label="+label+"&page="+page,
		success: function(msg){
		 jQuery("#load").html(msg);
		}
		});
	if (window.event) event.returnValue = false;
}

function redirect(url)
{
	window.location = url;
}

function redirectblank(url)
{
	//window.parent.location = url;
	window.open(url,'_blank');
}


function clear_to_value(sid,oid)
{
	jQuery.ajax({
		type: "POST",
		url: "/mail/ac/clear_to_value/",
		data: "sid="+sid+"&oid="+oid,
		success: function(msg){
			$("#owner"+oid).remove();
		}
	});
}


function mail_check_all(obj)
{
	var checked_status = obj.checked;
	jQuery("input.mailcheck").each(function()
	{
		this.checked = checked_status;
	});
}


function vote(id, mode, opt){
	var url = '/votes/vote/'+id+'/'+mode+'/'+opt;
	jQuery.ajax({
		url: url,
		cache: false,
		success: function(resp){
			if (resp=="-1"){
				alert ('Please log in first to vote.');
			} else if (resp=="0")
				alert ('You have already voted or you do not have permission to vote for this comment.');
			else {
				if (opt=="1"){
					c = parseInt($("#voting-"+mode+"-"+id).find(".up").html())+1;
					$("#voting-"+mode+"-"+id).find(".up").html(c);
					$("#voting-"+mode+"-"+id).find(".up").addClass('myup');
				} else {
					c = parseInt($("#voting-"+mode+"-"+id).find(".down").html())+1;
					$("#voting-"+mode+"-"+id).find(".down").html(c);
					$("#voting-"+mode+"-"+id).find(".down").addClass('mydown');
				}
			}
		}
	});
}
