
////  blogtools.js
////  - modal dialog functions for blogs

// first, some superb utility functions

var blogCacheBustNum = (new Date).getTime();
function getBlogCacheBustNum() { return blogCacheBustNum++; }

function load_style (uri) {    
    uri = uri + '?'+getBlogCacheBustNum();
    if (document.createStyleSheet) {
	this.document.createStyleSheet(uri); 
    } else {
	var link = document.createElement("link");
	link.rel = "stylesheet";
	link.type = "text/css";
	link.href = uri;
	link.media = "screen";
	document.getElementsByTagName('head')[0].appendChild(link);
    }
}  

var loadedScripts = new Array();
function load_script (uri) {	 
    if ( loadedScripts.include(uri) ){
	return;
    }
    n = getScriptCacheBustNum();
    var myAjax = new Ajax.Request(
	uri + '?cachebust=' + n + '&',
	{
        method:'GET',
	asynchronous:false
    });

    try {
 	eval(myAjax.transport.responseText);
	loadedScripts.push(uri);
    } 
    catch (e) {
	alert('JS Error: ' + uri);
	throw e;
    }
}
// cache busting for ie
var scriptCacheBustNum = (new Date).getTime();
getScriptCacheBustNum = function () { return scriptCacheBustNum++; }

// load some libraries with said functions
load_script('/adm/common/js/dialog.js');

var request_in_progress = 0;
function blog_run (q) {
 
    if( request_in_progress  ) {
	return;    
    }
 
    startBusy();
    request_in_progress = 1;
    var myAjax = new Ajax.Request( 'control.ajax',
        { method:'post', 
	  postBody: q,
          onComplete: blog_callback });
}

function blog_callback (r,cmd) {
    request_in_progress = 0;
    stopBusy();
    if( r.status == 200 ) {
	if ( cmd ) { cmd(r) };
    }    
    else {
	var w = window.open("","error","resizable=1,status=0,scrollbars=1,menubar=0,directories=0,width=780,height=650");
	w.document.open('text/html', 'replace');
	w.document.write(r.responseText);
	w.document.close();
	w.focus();
    }
}

// UI Functions
function login_modal () { 
    blog_run('c=login');
}
function add_comment (eid) {
    blog_run('c=comment_add&eid=' + eid);
}
function reply_comment (eid,cid) {
    blog_run('c=comment_add&eid=' + eid + '&cid=' + cid);
}
// 'share this' links
function share_this_page (button, page) {
  var d = elementDimensions(button);
  blog_run('c=share_this_page&sharepage='+page+'&x='+d.cx+"&y="+d.cy);
}
// just in case we get caught with the old-style (deprecated) comments
function commlink(blog_id, entry_id, user, ncomments) {
    if (ncomments) {
	url = 'comment.blog?a=render';
    } else {
	url = 'control.comment?a=add';
    }
    url += '&entry_id=' + entry_id;
    commwin = window.open(url, 'commets', 'resizable,scrollbars');
    commwin.focus();
}

