function urlencode(str) {
return escape(str).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
}

function urldecode(str) {
return unescape(str.replace('+', ' '));
}
function clearcook(link)
{
	$.cookie('ez_page','',{path: "/",expires: -1});
	location.href=link;
}
function setlogin(init,url)
{	
	if (url) page=url;
	else
	page = location.href;
	//cook = urlencode(page);
    var is_ok=page.indexOf('/register/');
    if (is_ok!=-1) page='';

	$.cookie('ez_page',page,{path: "/",expires:30});
	location.href=init;
}

function reload(imageName,path)
{
	var randomnumber=Math.floor(Math.random()*101001); // generate a random number to add to image url to prevent caching
	var src = path+'/captcha.php?num=' + randomnumber;
	$('img[name='+imageName+']').attr('src',src);  // change image src to the same url but with the random number on the end
}

function remove_favour(id)
{
	var ok=confirm('Do you really want to remove this?');
	if (ok) xajax_remove_favour(id); 
	return false;
}
function user_delete(id)
{
	var ok=confirm('Do you really want to remove this?');
	if (ok) xajax_remove_articles(id); 
	return false;
}
function delete_comment(id)
{
	var ok=confirm('Do you really want to remove this?');
	if (ok) xajax_delete_comment(id); 
	return false;
}
function ChangeFont(value)
{
	var currentFontSize = $('div#art_content').css('font-size');
	var currentFontSizeNum = parseFloat(currentFontSize, 10);

	if (value==1)
		var newFontSize = currentFontSizeNum*1.2;
	else
		var newFontSize = currentFontSizeNum*0.8;

	$('div#art_content').css('font-size', newFontSize);
	return false;
}
$(function(){
	$.fn.resizeImg = function(new_width,new_height) {
		this.each(function(){
			var oldwidth = this.width;                   
			var oldheight = this.height;
			if (oldwidth>oldheight) this.height=new_height;
			if (oldheight>oldwidth) this.width=new_width;
			if(oldheight==oldwidth)                    
			{                                
				this.width=new_width;
				this.height=new_height;                          
			}  
			return this;

		});
	};          
});
function tiny_init()
{
	tinyMCE.init({

		mode : "textareas", 
		theme : "advanced",    
		plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager",
		paste_auto_cleanup_on_paste : true,		
		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code",
		theme_advanced_buttons3 : "insertdate,inserttime,preview,|,forecolor,backcolor,|,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,    
		height: "300",
		font_size_style_values : "medium"
	});
}
jQuery.fn.counter = function() {
  // setup initial counter display
  $(this).each(function() {
	var max = $(this).attr('maxlength');
	var val = $(this).attr('value');
	var cur = 0;
	if(val) // value="", or no value at all will cause an error
	  cur = val.length;
	var left = max-cur;
	$(this).after("<span class='counter'>"
	  + left.toString()+"</span> characters remaining");
	// Style as desired
	var c = $(this).next(".counter");
	c.css("margin-left","10px");
	c.css("padding", "0 3px 0 3px")
	c.css("border", "1px solid #ccc")
	if(left <= 10)
		c.css("background","#F4F379");
	else
		c.css("background","none");
 
	// setup counter to change with keystrokes 
	$(this).keyup(function(i) {
	  var max = $(this).attr('maxlength');
	  var val = $(this).attr('value');
	  var cur = 0;
	  if(val)
		cur = val.length;
	  var left = max-cur;
	  var c = $(this).next(".counter");
	  c.text(left.toString());
	  if(left <= 10)
		  c.css("background","#F4F379");
	  else
		  c.css("background","none");
	  return this;
	});
  });
  return this;
}


