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

function windowOpen(mypage, myname, w, h) {
	var features = "resizable=no,menubar=no,scrollable=no,location=no";
	if (screen.width) {
		var winl = (screen.width-w)/2;
		var wint = (screen.height-h)/2;
	} else {
		winl = 0;wint =0;
	}
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;
	var settings = 'height=' + h + ',';
	settings += 'width=' + w + ',';
	settings += 'top=' + wint + ',';
	settings += 'left=' + winl + ',';
	settings += features;
	win = window.open(mypage,myname,settings);
	win.window.focus();
}

function blogs_loadMore_start()
{
	new Effect.Fade('last_entries_button')
	new Effect.Appear('last_entries_spinner')
}
function blogs_loadMore_finish(theid)
{
	new Effect.SlideDown('entries_block_'+theid);
	new Effect.Appear('last_entries_button', {delay: 1})
	new Effect.Fade('last_entries_spinner', {delay: 1})
	stripe_recent_blogs()
}

function stripe_recent_blogs()
{
	$$('#recent_blog_entries li').each(function(el, index) {
		Element.addClassName(el, (index%2==1) ? "row1" : "row2");
	});
}

function blogs_loadMore(after_entry_id) {
	//loads next several blog entries
	var element = "recent_blog_entries";
	var url = '/front/get_more_blogs/'+after_entry_id;

	blogs_loadMore_start();
	
	new Ajax.Updater(element, url,
			{
				insertion: Insertion.Bottom,
				evalScripts: true
			}
		);
}

function initTooltip()
{
	Event.observe(window, 'load', buildTooltip);
}

function tooltip_place(el)
{
	var tTip = $('tooltip');
	tTip.style.width = 'auto';
	$("ttip_text").update(el.cachedAlt);
	var tipWidth = tTip.getWidth();

	el.cachedOffset = Position.cumulativeOffset($(el));
	el.cachedTopPos = (el.cachedOffset[1]+$(el).getHeight()-3)+'px';

	tTip.style.left = (el.leftOffFix-tipWidth/2)+'px';
	tTip.style.top = el.cachedTopPos;
	tTip.style.width = tipWidth+'px';
	
	$("tooltip").show();
}
function tooltip_remove()
{
	$("tooltip").hide();
}

function buildTooltip()
{
	var theBody = $('container');		
	var tTip =  $(document.createElement('div'));
	tTip.setAttribute('id', 'tooltip');
	tTip.style.display='block';
	var html = '<div class=\"left\"></div><div id="ttip_text" class=\"text\">ttip</div><div class=\"right\"></div>';
	tTip.update(html);
	tTip.setStyle({opacity:0.9});
	tTip.hide();
	theBody.appendChild(tTip);
	
	//attaching listeners
	$$("#latest_bloggers div.item-mini a").each(function(el) {
		el.cachedOffset = Position.cumulativeOffset($(el));
		el.leftOffFix = el.cachedOffset[0]+$(el).getWidth()/2;
		el.cachedTopPos = (el.cachedOffset[1]+$(el).getHeight()-3)+'px';
		el.cachedAlt = el.down("img").alt;
		
		Event.observe($(el), 'mouseover', function() {
			tooltip_place(el);
		});
		Event.observe($(el), 'mouseout', tooltip_remove);
	});
}

function initPhotofade() {
	document.pfade_on = false;
	Event.observe(window, 'load', photoFadeActivate);
}

var animation_inProgress = false;

function photoFade() {
	//setTimeout("photoFadeUpdater()", 1000);
	new Effect.Fade('photofade-content', {duration : 1,
		afterFinish: photoFadeUpdater
	})
}
function photoFadeUpdater() {
	var url = "/front/photofade"
	new Ajax.Updater('photofade',url, {evalScripts:true})
}
function photoFadeActivate() {
	document.pfade_on = true;
	//new Effect.Opacity('photofade_img', {from:0, to:1, duration: 1.5})
	new Effect.Appear('photofade-content', { duration: 2 });
	setTimeout("photoFade()", 10000);
}


