/*
var Popup = Class.create();
Popup.prototype = {
    initialize: function(options) {
	this.options = {
	    url: '#',
	    width: 300,
	    height: 300
	}
	Object.extend(this.options, options || {});
	window.open(this.options.url, '', 'width='+this.options.width+',height='+this.options.height);
    }
}
new Popup({url:'http://www.yahoo.com/'});
*/


var WipInline = {
    wi_queryString: function () {
	var params = {};
	try {
	    var query_string = window.location.search.split("?")[1];
	    if (query_string) {
		params = query_string.toQueryParams();
	    }
	} catch (err) {	}

	return params;
    },
    wi_readCookie: function (c_name) {
	if (document.cookie.length > 0) {
	    var c_start = document.cookie.indexOf(c_name + "=");
	    if (c_start != -1) {
		c_start = c_start + c_name.length + 1;
		c_end = document.cookie.indexOf(";", c_start);
		if (c_end == -1) {
		    c_end=document.cookie.length;
		}
		return unescape(document.cookie.substring(c_start,c_end));
	    } 
	}
    },
    wi_popup: function () {
	document.write('<link rel="stylesheet" href="/tools/wip_v4/inline/css/popup.css" type="text/css" />');
        document.write('<div class="wipinline_css_admin" id="wipinline_css_admin"></div>');
        document.write('<div class="wipinline_status" id="wipinline_status" style="display:none;"></div>');
    },
    wi_setup: function (objid, script_name, func) {
	var params  = this.wi_queryString();
	var cookie  = this.wi_readCookie('WiP_Admin_Ticket');
	if (!cookie) {
	    cookie  = this.wi_readCookie('Ticket');
	}
	if (cookie) {
	    var array   = cookie.split("&");
	    var user    = array[1];
	    var myenv   = {};
	    try {
		if (env) {
		    myenv = env;
		}
	    } catch (err) { }

	    var inline = {
		css:      '<link rel="stylesheet" href="/tools/wip_v4/inline/css/default.css" type="text/css" />'
	    };    
	    if (user) {
		this.wi_edit();
                $('wipinline_css_admin').update(inline.css);

	    }
	}
    },
    wi_edit: function () {
	// WiP::Article::title
	$$('._wtt').each( function(el) {
		//el.addClassName('wip_article_title');
		new Ajax.InPlaceEditor(
				       el,
				       '/tools/wip_v4/admin/inline/edit.epl', {
					   callback: function(form, value) { return 'action=save&id='+el.id+'&f=teaser_title&value='+encodeURIComponent(value) },
					   okText: 'Save',
					   cancelControl: 'button',
					   cancelText: 'Cancel',
                                           highlightColor: '',
                                           highlightEndColor: '',
				           formClassName: '_wtt edit',
					   rows: 1,
					   loadTextURL: '/tools/wip_v4/admin/inline/load.epl'
				       }
				      );
	    });
	$$('._wlt').each( function(el) {
		//el.addClassName('wip_article_title');
		new Ajax.InPlaceEditor(
				       el,
				       '/tools/wip_v4/admin/inline/edit.epl', {
					   callback: function(form, value) { return 'action=save&id='+el.id+'&f=lead_title&value='+encodeURIComponent(value) },
					   okText: 'Save',
					   cancelControl: 'button',
					   cancelText: 'Cancel',
                                           highlightColor: '',
                                           highlightEndColor: '',
				           formClassName: '_wlt edit',
					   rows: 1,
					   loadTextURL: '/tools/wip_v4/admin/inline/load.epl'
				       }
				      );
	    });
	$$('._wt').each( function(el) {
		//el.addClassName('wip_article_title');
		new Ajax.InPlaceEditor(
				       el,
				       '/tools/wip_v4/admin/inline/edit.epl', {
					   callback: function(form, value) { return 'action=save&id='+el.id+'&f=title&value='+encodeURIComponent(value) },
					   okText: 'Save',
					   cancelControl: 'button',
					   cancelText: 'Cancel',
                                           highlightColor: '',
                                           highlightEndColor: '',
				           formClassName: '_wt edit',
					   rows: 1,
					   loadTextURL: '/tools/wip_v4/admin/inline/load.epl'
				       }
				      );
	    });
	$$('._wrm').each( function(el) {
		//el.addClassName('wip_article_title');
		new Ajax.InPlaceEditor(
				       el,
				       '/tools/wip_v4/admin/inline/edit.epl', {
					   callback: function(form, value) { return 'action=save&id='+el.id+'&f=read_more&value='+encodeURIComponent(value) },
					   okText: 'Save',
					   cancelControl: 'button',
					   cancelText: 'Cancel',
                                           highlightColor: '',
                                           highlightEndColor: '',
				           formClassName: '_wrm edit',
					   rows: 1,
					   loadTextURL: '/tools/wip_v4/admin/inline/load.epl'
				       }
				      );
	    });
	$$('._wi').each( function(el) {
		//el.addClassName('wip_article_ingress');
		new Ajax.InPlaceEditor(
				       el,
				       '/tools/wip_v4/admin/inline/edit.epl', {
					   callback: function(form, value) { return 'action=save&id='+el.id+'&f=ingress&value='+encodeURIComponent(value) },
					   okText: 'Save',
					   cancelControl: 'button',
					   cancelText: 'Cancel',
                                           highlightColor: '',
                                           highlightEndColor: '',
					   formClassName: '_wi edit',
					   rows: 8,
					   //onEnterEditMode: function (form, value) { alert(form.id + ':' + $(form)); /*.insert({before : '<div>foobar</div>'})*/ },
					   loadTextURL: '/tools/wip_v4/admin/inline/load.epl'
				       }
				      );
	    });
	$$('._wti').each( function(el) {
		//el.addClassName('wip_article_ingress');
		new Ajax.InPlaceEditor(
				       el,
				       '/tools/wip_v4/admin/inline/edit.epl', {
					   callback: function(form, value) { return 'action=save&id='+el.id+'&f=teaser_ingress&value='+encodeURIComponent(value) },
					   okText: 'Save',
					   cancelControl: 'button',
					   cancelText: 'Cancel',
                                           highlightColor: '',
                                           highlightEndColor: '',
					   formClassName: '_wti edit',
					   rows: 8,
					   loadTextURL: '/tools/wip_v4/admin/inline/load.epl'
				       }
				      );
	    });
	$$('._wb').each( function(el) {
		//el.addClassName('wip_article_body');
		new Ajax.InPlaceEditor(
				       el,
				       '/tools/wip_v4/admin/inline/edit.epl', {
					   callback: function(form, value) { return 'action=save&id='+el.id+'&f=body&value='+encodeURIComponent(value) },
					   okText: 'Save',
					   cancelControl: 'button',
					   cancelText: 'Cancel',
                                           highlightColor: '',
                                           highlightEndColor: '',
					   formClassName: '_wb edit',
					   rows: 20,
					   loadTextURL: '/tools/wip_v4/admin/inline/load.epl'
				       }
				      );
	    });



    }
}


// Extend document object with new functions.
Object.extend(document, WipInline);


//------------------------------------------------------------------------------------------
// Init functions we think you need after dom is loaded
// Event.observe(window, 'load', function() {
document.observe("dom:loaded", function(){
	//wipInline.popup();
	document.wi_setup();
	
	var keydown;
	// Observe key down.
	//Event.observe(document, 'keydown', function(e){
	//	var code;
	//	if (!e) var e = window.event;
	//	if (e.keyCode) code = e.keyCode;
	//	else if (e.which) code = e.which;
	//	keydown = String.fromCharCode(code);
	//	//var character = String.fromCharCode(code);
	//	//# $('wipinline_status').update('<div id="wipinline_popup"  class="popup">' + 'Keydown: Character was "' + character + '", code is "' + code + '"' + '</div>');
	//	//alert('Character was "' + character + '", code is "' + code + '"');
	//    });

	// Observe key press. When a user press a key and releases it.
	Event.observe(document, 'keydown', function(e){
		var altDown  = e.altKey;
		var ctrlDown = e.ctrlKey;
		var timer;
		var code;
		if (altDown && ctrlDown) {
		    if (!e) var e = window.event;
		    if (e.keyCode) code = e.keyCode;
		    else if (e.which) code = e.which;
		    var character = String.fromCharCode(code);
		    var action = '';
		    e.stop();
		    clearTimeout(timer);
		    switch (code) {
		        case 76: // Alt+Ctrl+L - login
			    action = 'login';
			    var fdat = document.wi_queryString();
			    Effect.Appear($('wipinline_status'))
			    new Ajax.Updater($('wipinline_status'), '/tools/wip_v4/inline/ajax/wip_admin_login.epl', {
				    parameters: { div: 'wipinline_status', id: fdat.id, cid: fdat.cid, kid: fdat.kid, q: fdat.q, md5:fdat.md5 },
				    evalScripts: true
			    });

			    //$('wipinline_status').update('<div style="display: block;" id="wipinline_box" class="wipinline_box">' + 'Keypress: Character was "' + character + '", code is "' + code + '"' + ', action is: "' + action + '" alt: '+altDown+' ctrl: '+ctrlDown+'</div>');
			    break;
  		        case 68: // Alt+Ctrl+D - logout
			    action = 'logout';
			    break;
  		        case 69: // Alt+Ctrl+E - edit
			    action = 'edit';
			    break;
  		        case 78: // Alt+Ctrl+N - New
			    action = 'new';
			    break;
  		        case 84: // Alt+Ctrl+T - Twitter
			    action = 'twitter';
			    var fdat = document.wi_queryString();
			    Effect.Appear($('wipinline_status'))
			    new Ajax.Updater($('wipinline_status'), '/tools/wip_v4/inline/ajax/wip_tweet_this.epl', {
				    parameters: { div: 'wipinline_status', id: fdat.id, cid: fdat.cid, kid: fdat.kid, q: fdat.q, md5:fdat.md5 },
				    evalScripts: true
			    });
			    break;
		        case 87: // Alt+Ctrl+W - Close popup
                            action = 'close';
			    timer = setTimeout("Effect.Fade($('wipinline_status'))", 2000);
			    break;
  		        default:
			    //$('wipinline_status').update('<div style="display: block;" id="wipinline_popup" class="wipinline_popup">' + 'Keypress: Character was "' + character + '", code is "' + code + '"' + ', action is: "' + action + '" alt: '+altDown+' ctrl: '+ctrlDown+'</div>');
			    //$('wipinline_popup').fade.delay(3, {duration: 3});
			    //action = 'unknown';
			    break;
		    }

		}
	    });

    });
