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');
				}
			}
		}
	});
}


function getAge(today, dateto) {
	var month = dateto.getMonth();
	var date = dateto.getDate();
	var year = dateto.getFullYear();
	
	var dateStr = today.getDate();
	var monthStr = today.getMonth();
	var yearStr = today.getFullYear();
	
	var theYear = yearStr - year;
	var theMonth = monthStr - month;
	var theDate = dateStr - date;
	
	var days = "";
	if (monthStr == 0 || monthStr == 2 || monthStr == 4 || monthStr == 6 || monthStr == 7 || monthStr == 9 || monthStr == 11) days = 31;
	if (monthStr == 3 || monthStr == 5 || monthStr == 8 || monthStr == 10) days = 30;
	if (monthStr == 1) days = 28;
	
	var sumyears = theYear;
	
	if (month < monthStr && date > dateStr) { sumyears = parseInt(sumyears) + 1; summonths = theMonth - 1; }
	if (month < monthStr && date <= dateStr) { summonths = theMonth; }
	else if (month == monthStr && (date < dateStr || date == dateStr)) { summonths = 0; }
	else if (month == monthStr && date > dateStr) { summonths = 11; }
	else if (month > monthStr && date <= dateStr) { sumyears = sumyears - 1;	 summonths = ((12 - -(theMonth)) + 1); }
	else if (month > monthStr && date > dateStr) { summonths = ((12 - -(theMonth))); }
	
	if (date < dateStr) { sumdays = theDate; }
	else if (date == dateStr) { sumdays = 0; }
	else { sumyears = sumyears - 1; sumdays = days - (-(theDate)); }
	
	return sumyears;
}


function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

function htmlspecialchars(str){
	var re=/[(<>"'&]/g
	return str.replace(re, function(m){return replacechar(m)})
}

function replacechar(match){
	if (match=="<")
		return "&lt;"
	else if (match==">")
		return "&gt;"
	else if (match=="\"")
		return "&quot;"
	else if (match=="'")
		return "&#039;"
	else if (match=="&")
		return "&amp;"
}

