// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function sendToEditor(n, thestring) {
	//alert("Tring to insert, editor:"+n+'/'+thestring);
	
	var win = window.opener ? window.opener : window.dialogArguments;
	if (!win) win = top;
	tinyMCE = win.tinyMCE;
	/*
	o = document.getElementById(n);
	h = o.innerHTML.replace(new RegExp('^\\s*(.*?)\\s*$', ''), '$1'); // Trim
	h = h.replace(new RegExp(' (class|title|width|height|id|onclick|onmousedown)=([^\'"][^ ]*)( |/|>)', 'g'), ' $1="$2"$3'); // Enclose attribs in quotes
	h = h.replace(new RegExp(' (width|height)=".*?"', 'g'), ''); // Drop size constraints
	h = h.replace(new RegExp(' on(click|mousedown)="[^"]*"', 'g'), ''); // Drop menu events
	h = h.replace(new RegExp('<(/?)A', 'g'), '<$1a'); // Lowercase tagnames
	h = h.replace(new RegExp('<IMG', 'g'), '<img'); // Lowercase again
	h = h.replace(new RegExp('(<img .+?")>', 'g'), '$1 />'); // XHTML
	*/
	win.tinyMCE.execCommand('mceInsertContent', false, thestring);
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function addUnloadEvent(func) {
	var oldonload = window.onbeforeunload;
	if (typeof window.onbeforeunload != 'function') {
		window.onbeforeunload = func;
	} else {
		window.onbeforeunload = function() {
			oldonload();
			func();
		}
	}
}

function highlightComment(comment) {
	if (comment) {
		if (document.getElementById(comment)) {
			new Effect.Pulsate(comment, {duration:2, from:0.4, pulses:3});
			new Effect.Highlight(comment, { duration: 5 });
		}
		return;
	}	
	//highlights comment in case there is a #comment_<id> anchor present in the url
	var href = document.location.href;
	var uriSplit = href.split("#")
	if (uriSplit.length>1 && uriSplit[1].indexOf("comment_")==0) {
		//hightlighting the comment.
		new Effect.Pulsate(uriSplit[1], {duration:2, from:0.4, pulses:3});
		new Effect.Highlight(uriSplit[1], { duration: 5 });
	}
}

function warningOnLeavingUnsaved() {
	return !confirm('You are about to leave unsaved page. Do you wish to continue?')
}
