/* Copyright IBM Corp. 2006, 2007  All Rights Reserved.              */

var Dogear = {
	version: "1.0",
	path: "",
	requires: [],
	count: 0,
	reqElems: new Array(5),
			
	init: function() {
		var included_libs,head,scripts,s,src,num,lib,param;
		included_libs = new Array();
		head = document.getElementsByTagName("head");
		scripts = head[0].getElementsByTagName('*');
		for (var i=0; i<scripts.length; i++) {
			s = scripts[i];
			src = s.src;
			if (src) {
				num = src.lastIndexOf("\/");
				lib = src.slice(num+1);
				included_libs[lib] = true;
				if (src.match(/script\/dogear\.js(\?.*)?$/)) {
					Dogear.path = src.replace(/script\/dogear\.js(\?.*)?$/,'');
				}
			}
		}
		for (i=0; i<Dogear.requires.length; i++) {
			lib = Dogear.requires[i];
			if (!included_libs[lib]) {
				var script=document.createElement('script');
				script.charset='UTF-8';
				script.src=Dogear.path+ "/" + lib;
				head[0].appendChild(script);
			}
		}
	},
	
	Util: {
			
		constructUrl: function(url, params)
		{
			var query = '';
			for (var p in params) {
				if (query.length > 0)
					query += "&";
				query += encodeURIComponent(p);
				query += "=";
				query += encodeURIComponent(params[p]);
			}
		
			if (query.length > 0)
				url += "?" + query;
			
			return url;
		},
		
		getCookie: function(name)
		{
		    key = name + "=";
		    c = document.cookie;
		    
		    // set start of cookie
		    start = c.indexOf("; " + key) + 2 + key.length;
		    start = start > -1 + 2 + key.length ? start : c.indexOf(name + "=") + key.length;	
		    
		    // set end of cookie if start is found
		    if (start > -1 + key.length) {
		       end = c.indexOf(";", start);
		       end = end > -1 ? end : c.length;
		       
		       // return cookie value
		       return unescape(c.substring(start , end));
		    } else {
		       // no cookie
		       return null;
		    }
		},
		
		setCookie: function(name, value)
		{
		    document.cookie = name + "=" + escape(value) +	"; expires=" + 
			    new Date(new Date().getTime() + 365*24*60*60*1000).toGMTString() + "; path=/";
		},
		
		setPreference: function(name, value)
		{
			Dogear.Util.setCookie(name, value);
			document.reload();
		},
		
		// Prefs
		setDescriptionVisibility: function(hide)
		{
			var divs = document.getElementsByTagName("div");
			for (var i = 0; i < divs.length; i++)
			{
				if (divs[i].className == 'verbiage' || divs[i].className == 'author')
					divs[i].style.display = hide ? 'none' : 'block';
			}
		},
		
		setHideDescription: function(hide)
		{
			Dogear.Util.setCookie('hv', hide ? 'true' : 'false');
			Dogear.Util.setDescriptionVisibility(hide);
		},
		
		addEngine: function()
		{
		  if ((typeof window.sidebar == "object") && (typeof window.sidebar.addSearchEngine == "function"))
		  {
			window.sidebar.addSearchEngine(
			  Dogear.path+"tools/dogear.src",
			  Dogear.path+"tools/dogear.png",
			  "dogear",
			  "Web" );
		  }
		  else
		  {
			alert(dojo.i18n.getLocalization("lconn.dogear","strings").dogearsearchalert);
		  }
		},
		
		addLoadEvent: function(func) 
		{
			var oldonload = window.onload;
			if (typeof window.onload != 'function')
			{
				window.onload = func;
			}
			else
			{
				window.onload = function() {
					oldonload();
					func();
				};
			}
		},
		
		addField: function(form, fieldType, fieldName, fieldId, fieldValue) {
			var input = document.createElement('INPUT');
			if (document.all) // what follows should work with NN6 but doesn't in M14
			{
				input.type = fieldType;
				input.name = fieldName;
				input.id = fieldId;
				input.value = fieldValue;
			}
			else if (document.getElementById) // so here is the NN6 workaround
			{
				input.setAttribute('type', fieldType);
				input.setAttribute('name', fieldName);
				input.setAttribute('id', fieldId);
				input.setAttribute('value', fieldValue);
			}
			form.appendChild(input);
		},
		
		
		popup_post: function(url)
		{
			dw = window.open(
				url,
				'dogear',
				'toolbars=no,resizable=yes,width=670,height=700');
			setTimeout('dw.focus();', 250);
		}
	},
	
	currentForm: {
		id: "",	
		nonce: "",
	
		toggleNotifyForm: function (linkid){
			if(Dogear.currentForm.id != "") {
				con = confirm(dojo.i18n.getLocalization("lconn.dogear","strings").closeformmsg);
				if(con == true) Dogear.currentForm.hideNotifyForm(Dogear.currentForm.id);
				else return;
			}
			Dogear.currentForm.showNotifyForm(linkid);
			
		},
		
		showNotifyForm: function (linkid){
			Dogear.currentForm.id = linkid;
			var notifyForm = document.getElementById('NotifyForm_'+linkid);
			$(notifyForm).style.display = '';
			//Effect.Appear(notifyForm,{duration:0.15});
			// var srcNode = document.getElementById('rteDiv_'+linkid);							
			// dojo.widget.createWidget("Editor2", {id:'editorWidget_'+linkid, height:'150px'}, srcNode);
		},
		
		hideNotifyForm: function (linkid){
			Dogear.currentForm.id = "";
			var emailsField = document.getElementById('notifyEmails_'+linkid);
			emailsField.value = "";
			var descField = document.getElementById('desc_'+linkid);
			descField.value = "";
			
			// var rteDiv = document.getElementById('rteDiv_'+linkid);
			// var children = rteDiv.childNodes;
			// for( var i = 0; i < children.length; ){
			// 	rteDiv.removeChild(children[i]);
			// }
			var notifyForm = document.getElementById('NotifyForm_'+linkid);
			// $(notifyForm).style.display = 'none';
			Effect.Fade(notifyForm,{duration:0.15});
		},
		
		notify: function (linkid){
			//var emails = encodeURIComponent(document.getElementById('notifyEmails_'+linkid).value);
			//var desc = encodeURIComponent(document.getElementById('desc_'+linkid).value);
			//var url = $('NotificationForm_'+linkid).getAttribute('action');
			//var options = {parameters: "linkid="+linkid+"&users="+emails+"&description="+desc, asynchronous: true};
			//new Ajax.Request(url, options);
			var notifyForm = document.getElementById('NotificationForm_'+linkid);
			notifyForm.submit();
			//this.hideNotifyForm(linkid);
		},
		
		setNonce: function (dogearUrl, form){
			if(Dogear.currentForm.nonce == ""){
				var request = new Ajax.Request( dogearUrl + "/securitynonce", {
    				asynchronous: false,
    				method: 'get'
				});
				Dogear.currentForm.nonce = request.transport.responseText;
			}
			if(Dogear.currentForm.nonce != "") $(form).dangerousurlnonce.value = Dogear.currentForm.nonce;
		},
		
		getNonce: function (dogearUrl){
			var request = new Ajax.Request( dogearUrl + "/securitynonce", {
				asynchronous: false,
				method: 'get'
			});
			var dNonce = request.transport.responseText;
			if(dNonce != null && dNonce != "" && Dogear.currentForm.nonce != dNonce){
				Dogear.currentForm.nonce = dNonce;
			}			
		}
	}	
}

Dogear.init();

function selectedName(li) {
	var submit;
	spns = $(li).getElementsByTagName("span"); 
	for (var i = 0; i < spns.length; i++) { 
    	name = spns[i].getAttribute("name"); 
 		if ( name == "email") { 
		     $('namefield').value= spns[i].innerHTML;
		     submit='true';
		     break;
  		}
	}
	if (submit) {
		$('actualnameform').submit();
	}
}

function clearSelectedMenuItems(menu){
	if (dojo.isIE) menu = menu.offsetParent;
	var menuitems = menu.getElementsByTagName("LI");
	if (menuitems.length){
		for(var i=0;i<menuitems.length;i++){
			if (dojo.isIE) {
				menuitems[i].className = "";
			}else {
				menuitems[i].setAttribute("class", "");
			}
		}
	}
	
}

DogearActionUpdater = Class.create();
Object.extend(Object.extend(DogearActionUpdater.prototype, Ajax.Request.prototype), {
	
	initialize: function(url,options){
		this.baseInitializer(url, options);
	},
	
	onComplete: function(){
		
	}
});

