/**
 * @author aprian
 */

function createSlug(title){
	$.getJSON(
		"/framework/library/cfc/baseFunction.cfc?title=" + title,
		{ method : 'createSlug', returnFormat : 'json'},
		function(data){
			$("#pagecode").val(data);
		}
	);
}


function limitChars(textid, limit, infodiv)
{
	var text = $('#'+textid).val();	
	var textlength = text.length;
	if(textlength > limit)
	{
		alert('You cannot write more then '+limit+' characters!');
		$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else
	{
		$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
		return true;
	}
}

/*
 * ALERT MESSAGE
 * Create a alert message
 * Required : <p class="ajax_warn"></p>
 * Position of .ajax_warn defined by css
 */
function alertMessage(msg){
	$messageBox = $("p.ajax_warn");
	$messageBox.text(msg);
	$messageBox.fadeIn(1000).fadeOut(1000);
}



/*
 * CLOSE BUTTON
 * Bind close function for element with ID btn_close (#btn_close)
 * ID : #btn_close
*/

function closePopup() {
	$("input#btn_close").each( function() {
		$(this).click(function() {
			self.close();
		});
	});
}

/*
 * POPUP WINDOW
 * Bind open new window function for element with class popup_[width]x[height]
 * Class: popup_[Width]x[Height]
 * To create popup 800x600, use class: popup_800x600
*/

function openPopup(href) {
	$("a[class^=popup_]").each( function() {
		$(this).click(function() {
			
			var winpop;
			var xurl = href || $(this).attr('href');
			var xtarget = '_blank';
			var thisClass = $(this).attr('class').split(' ');
			var re = new RegExp('^(popup_)');
			for(i=0;i<thisClass.length;i++) {
				if(thisClass[i].match(re)) {
					popSizes = thisClass[i].split('_');
					popSize = popSizes[1].split('x');
					popWidth = popSize[0];
					popHeight = popSize[1];
				}
			}
			
			openWindow(xurl, popWidth, popHeight);
			return false;
		});
	});
}

function openWindow(url, width, height) {
	var thisWindow;
 	if(thisWindow!=null && !thisWindow.closed){
		 thisWindow.close() }
		 thisWindow = window.open(url, '_blank','width=' + width + ',height=' + height + ',resizable=0,scrollbars=yes,menubar=no,status=no' );
		 thisWindow.focus();
}

