// 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 attachmentLinkToEditor(attachment_id, attachment_title, attachment_ext) {
	var myString = '<a target="_blank" href="/attachment/get/'+attachment_id+attachment_ext+'">'+attachment_title+'</a>';
	sendToEditor("ed", myString);
}

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

function page_templateSelect(parent_id, parent_type, parent_title) {
	//setting the inside elements visibility
	var allTypes = ["page_normal", "page_cat_holder", "page_goods_cat", "page_goods", "page_access_map"];
	var showTypes = allTypes;
		
	for (var i=0; i<allTypes.length; i++) {
		$(allTypes[i]).style.display = "none";
		$("radio_"+allTypes[i]).checked = false;
	}
	$("submit_button").disabled = true;
	$("page_parent_id").value = parent_id;
		
	switch (parent_type) {
		case "page_normal":
		case "page_access_map":
			showTypes = ["page_normal", "page_cat_holder", "page_access_map"];
			break;
		case "page_cat_holder":
			showTypes = ["page_goods_cat", "page_goods"];
			break;
		case "page_goods_cat":
			showTypes = ["page_goods"];
			break;
		case "page_goods":
			alert("This page can not have children");
			return;
			break;
		default:
			//just showing everything.
			break;
	}
		
	//displaying selected.
	for (var i=0; i<showTypes.length; i++) {
		//alert(showTypes[i]);
		$(showTypes[i]).style.display = "block";
	}
	
	//setting the elements position
	var pos = Position.cumulativeOffset($("addchild_"+parent_id))
	$("template_selector").style.left = (pos[0]+10)+"px"
	$("template_selector").style.top = (pos[1]+7)+"px"
	
	//$("template_selector").style.display = "block"
	new Effect.Opacity("template_selector", {from:0.1, to:0.92, duration:0.5})
	new Effect.SlideDown("template_selector", {duration:0.7})
}
function page_templateSelect_hide() {
	Effect.Fade("template_selector");
	Effect.DropOut("template_selector");
}
function page_enableSubmit() {
	$("submit_button").disabled = false;
}

function kurachi_top() {
	var servbuts = $$("#service_buts a");
	var bigbuts = $$("#top_buttons a");
	for (var i=1; i<=4; i++) {
		servbuts[i-1].idx = i;
		bigbuts[i-1].idx = i;
		Event.observe(servbuts[i-1], "mouseover", kurachi_over);
		Event.observe(bigbuts[i-1], "mouseover", kurachi_over);
		
		Event.observe(servbuts[i-1], "mouseout", kurachi_out);
		Event.observe(bigbuts[i-1], "mouseout", kurachi_out);
	}
}
function kurachi_over(evt) {
	var element = Event.element(evt)
	
	kurachi_show_num(element.idx);
	
}
function kurachi_out(evt) {
	var element = Event.element(evt)
	
	kurachi_show_num(-1);
	
}
function kurachi_show_num(num) {	
	var divs = $$("#head_images div");
	for (var i=1; i<=4; i++) {
		if (i==num) {
			divs[i-1].show();
		} else {
			divs[i-1].hide();
		}
	}
}
function kurachi_init() {
	Event.observe(window, "load", kurachi_top);
}