type_error = "error";
type_debug = "debug";

function log( type, msgs ) {
    var result;
	for (var i = 0; i < arguments.length; i++){
		result += arguments[0];
	}
	if (type = type.debug) alert(result);
}

if (typeof console == "undefined") { 
	var console = {}; 
	console.error = log; 
	console.debug = log;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/* 
Syntax for params is key, value or a single string with in standard cookie
format: "key=value; key=value; key=value; etc."
*/ 
function setLinkParams( params, value ) {
	console.debug("linkp start document.cookie",document.cookie);
	var key_delim = "&";
	var key_cookie = "lpkeys";
	var keys = readCookie(key_cookie);
	if ( keys ) { //check if keys exist
		keys = keys.split(key_delim);
	} else {
		keys = new Array();
	}
	if(value) { // check if arguments are key, value
		keys.push(params);
		params = params + "=" + value; 
		// console.debug("linkp key, value",params);
		document.cookie = params + "; path=/"; 
	} else { // add all keys
		kvpairs = params.split(";")
		for(var i=0;i < kvpairs.length;i++) {
			var c = kvpairs[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			keys.push(c.substring(0,c.indexOf("=")));
			document.cookie = kvpairs[i] + "; path=/"; 
		}
	}
	keys = keys.toString()
	re = /,/g
	keys = keys.replace(re,"&");
	document.cookie = key_cookie + "=" + keys + "; path=/";
	console.debug("linkp document.cookie",document.cookie);
}


function getEl( id ){ return document.getElementById( id ); }

/* --------------------------------------- */
function URLEncode( plaintext )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    //alert( "Unicode Character '" 
                //        + ch 
                //        + "' cannot be encoded using standard URL encoding.\n" +
				//          "(URL encoding only supports 8-bit characters.)\n" +
				//		  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function URLDecode( encoded )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				//alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};



// Tab Object

if (!modArray) { var modArray = new Array(); }

function chngeTab() {
	var prnt = this.tabCntl.tabLst.getElementsByTagName( 'li' );
	for ( p=0; p<prnt.length; p++) {
		prnt[p].className = '';
	}
	this.className = this.tabCntl.ovrClss;
	
	var idx = 0;
	for( ; idx < modArray.length; idx++ ) { if (modArray[idx][0] == this.modNme ) break; }
	if ( idx  < modArray.length ) {
		this.tabCntl.trgDiv.innerHTML = modArray[idx][1];
	} else {
		var trgDiv = this.tabCntl.trgDiv;
		var modNme = this.modNme;
		dojo.io.bind({
			url: '/cannae/.module/'+  this.modNme + this.tabCntl.modPath + this.tabCntl.assetID,
			handler: function(type, data, evt ) {
				trgDiv.innerHTML = data;
				modArray.push([modNme,data]);
			},
			error: function(type, error){
				console.error('Error when retrieving ' + modNme + ' from the server: '+error);
			}
		});
	}
	if (this.tabInit) this.tabInit(this.tabCntl.trgDiv);
}

function addTab( modNme, elId, tabInit, params ) {
	var el = getEl(elId);
	el.onclick = chngeTab;
	el.modNme = modNme;
	el.tabCntl = this;
	el.tabInit = tabInit;
	var ajxurl = '/cannae/.module/'+  modNme + this.modPath + this.assetID + '?' + params;
	dojo.io.bind({
		url: ajxurl,
		handler: function(type, data, evt ) {
			modArray.push([modNme,data]);
		},
		error: function(type, error){
			console.error('Error when retrieving ' + modNme + ' from the server: '+error);
		}
	});
}


function tabControl(trgDivId, modPath, assetID, ovrClss, tabLstId ){
	this.trgDiv=getEl(trgDivId);
	this.modPath=modPath;
	if (assetID) this.assetID = assetID;
	this.addTab = addTab;
	if (ovrClss) this.ovrClss = ovrClss;
	if (tabLstId) this.tabLst = getEl(tabLstId);
}

function videoTabInit( trgDiv, showFull ) {
	// uses this.trgDiv or param
	// console.debug("videoTabInit",trgDiv);
	// Initialize video buttons
	var md = trgDiv.getElementsByTagName( 'div' );
	for (var h=0; h<md.length; h++ ) {
		if (md[h].className == "scvBx") {
			md[h].onmouseover = function() { this.className="scvBx hghLght";}
			md[h].onmouseout = function() { this.className="scvBx";}
		}
	}
	
	// Initialize the more video button
	var btns = dojo.html.getElementsByClass("rrBttn noFlt",trgDiv);
	if ( btns[1] ) {
		var shw10 = dojo.html.getElementsByClass("shw10",trgDiv)[0];
		btns[1].trg = shw10;
		btns[1].onclick = showMoreVidLstClick;
		if ( showFull ) {
			btns[1].style.display = "none";
			shw10.style.display = "block";
		}
	}
}

//


// If this function is used in module include dojo.require("dojo.lfx.html");
function showMoreVidLstClick( ) {
	setLinkParams('fulllist','true');
	this.style.display='none';
	// dojo.lfx.html.wipeIn(this.trg,1000).play();
	this.trg.style.display='block';
}

// If this function is used in modules include dojo.require("dojo.html.util");
function insertNodeTextBefore( el, txt ) {
	var modNodes = dojo.html.createNodesFromText(txt,true)
	for(var i = 0; i < modNodes.length; i++) {
		el.parentNode.insertBefore(modNodes[i], el.previousSibling);
	}
}

function findPos(obj) {  	
         var curleft = curtop = 0; 	
         if (obj.offsetParent) { 	
                 curleft = obj.offsetLeft 	
                 curtop = obj.offsetTop 	
                 while (obj = obj.offsetParent) { 	
                         curleft += obj.offsetLeft 	
                         curtop += obj.offsetTop 	
                 } 	
         } 	
         return [curleft,curtop]; 	
 }
 
 
 function snsSignIn( obj, leftOffSet, topOffSet ) {
 	if (!leftOffSet) leftOffSet = 0;  
 	if (!topOffSet) topOffSet = 0;  
	var p = getEl("snsMiniUI");
 	var pos = findPos(obj);
	p.innerHTML = _sns_var_;
	p.style.position = 'absolute';
	p.style.left = ( pos[0] + leftOffSet ) + 'px';
	p.style.top = ( pos[1] + topOffSet ) + 'px';
}

function getSelectedValue( selObj ){
	return selObj.options[selObj.selectedIndex].value;
}
 
 /*
	Construct an objectURI to store in TRRI.
	form of:
		urn:x-aol:oid:<entity-type>:<entity-source>:<entity-id>
		where entity-type = video
				entity-source = data store (pmmms/truveo)
				entity-id = assetID pmmmsId/truveoId
*/
function getObjectURI(entity_id, entity_source, entity_type){
	if(entity_type == null || entity_type == '')
		entity_type = 'video';
	if(entity_source == null || entity_source == '')
		entity_source = 'pmms';
	return ('urn:x-aol:oid:video:' + entity_source + ':' + entity_id);
}
 