
var timeout = 200;
var blinkCount = 0;
var min_display_threshold = 0;
var max_char_count = 66;
var featured_master = null;
var latest_master = null;
var thumbsup_master = null;
var mostdled_master = null;
var tutorials_master = null;
var blueprint_master = null;
var template = null; 

var featured_book = new Array;
var latest_book = new Array;
var thumbsup_book = new Array;
var mostdled_book = new Array;
var tutorials_book = new Array;
var blueprint_book = new Array;

var MASHUP_ICON = '../img/mashup16x16.png';
var MBX_ICON = '../img/mbox16x16.png';
var PRST_ICON = '../img/presentation16x16.png';
var BPRT_ICON = '../img/bprt16x16.png';

var pic= new Image(500,250); 
pic.src="../img/TearnShareOpen.png";

var PANEL_WIDTH = 190;
var comm_master = null;
var CountPerPage = 40;
var DispRange = 5;
var gStart = 0;

var gOpenSubmit = null;
var gSubmitTemplate = null;

var gActiveBox = '';
var gSearch = 0;
var gScrolling = false;

var gHasRanked = false;
var gStage = 'home';
var gAuthor = '';
var gCategory = '';
var gFilter;
var gLastHomeFilter = null;

var gLeftMargin = null;
var gRightMargin = null;
var gAppTitle = '';

var gSpectralLines = new Array;
var gSpectralImgs = new Array;
var gSpectralCounts = new Array;
var gTransitionDuration = 0.5;  // 0.5sec

var condenserIcons = ['none','text','image','box','script','cursor','keyboard','hr','iframe','quicktime',
'flash','var','obj','array','timer','event_receiver','messenger','canvas','xml','ajax','ccrx',
'server','client','gmap','alias','camera','light','mesh','material','environment','terrain',
'billboard','particle','manualobject','cg'];
  
var hasCCRX = (getCCRX() != 0);
function getCCRX()
{
	var cx;
	try{cx = new CCRX();}catch (err){cx = 0;}
	return cx;
}

if (typeof Function.prototype.bind != 'function')
{
	// function bind is not defined on Safari, define it here
	Function.prototype.bind = function (bind) {
	    var self = this;
	    return function () {
	        var args = Array.prototype.slice.call(arguments);
	        return self.apply(bind || null, args);
	    };
	}
}

function getText(e)
{
	if (!e)
	{
		throw "Error: Reload";
		return;
	}
	
	if (e.textContent)
		return e.textContent;
	else
	{
		if (e.hasChildNodes())
			return e.childNodes[0].nodeValue;
		else
			return "";
	}	
}

function createCookie(name,value,days) 
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

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;
}

function eraseCookie(name) 
{
	createCookie(name,"",-1);
}

function giveRating(e, r)
{
	e.style.width=Math.round(r*10)+'px';
	e.parentNode.parentNode.title = r;
	e.title = r;
}

function SelectFilter(e)
{
	var al = document.getElementById('all');
	var wa = document.getElementById('prst');
	var fo = document.getElementById('mashup');
	var dr = document.getElementById('mbx');
	var bprt = document.getElementById('bprt');
	al.src = '../img/blank.png';
	wa.src = '../img/blank.png';
	fo.src = '../img/blank.png';
	dr.src = '../img/blank.png';
	bprt.src = '../img/blank.png';
	
	e.src = '../img/checked.png';
	eraseCookie('filter');
	createCookie('filter', e.id, 30);
		
	// reload contents
	if (gStage == 'home')
	{
		reloadContent();
		gLastHomeFilter = e;
	}
	else if (gStage == 'div_author')
		reloadAuthorPage(gAuthor);
	else if (gStage == 'div_category')
		reloadCatPage(gCategory);
	else
		reloadBox(gStage);
}

function QueryMaster(type) 
{
	this._type = type;
	this._top = document.getElementById(this._type);
	
	var ss = this._type.split('_');
	if (ss.length > 1) this._panel = document.getElementById(ss[1]);
	
	this._renderPage = this._renderObjs;
	
	this._filter = readCookie('filter');
	if (!this._filter) 
	{
		createCookie('filter', 'all', 30);
		this._filter = 'all';
	}
}
QueryMaster.prototype = {
  _type: 'default',
  _top: null,
  _req: null,
  _url: 'query.php',
  _container: null,
  _panel: null,
  _items: null,
  _output: null,
  _start: 0,
  _count: 0,
  _name: '',
  _desc: '',
  _comm: '',
  _tag: '',
  _implements: '',
  _category: '',
  _publisher: '',
  _attrb: '',
  _action: '',
  _guid: '',
  _orderby: '',
  _filter: '',
  _pagesize: 10,
  _xmlobjects: null,
  _timeout: timeout,
  _renderPage: null,
  _waitentity: null,
  _retain: false,
  _renderIndex: 0,
  _rendering: 0,
  parse_pos: 0,
  parse_end: 0,

  _makeQuery: function QM_MakeQuery() {
  	if (window.ActiveXObject)
  	{
  		this._req = new ActiveXObject("Microsoft.XMLHTTP"); 
  	}
	else if (window.XMLHttpRequest)
  	{
  		this._req = new XMLHttpRequest();
  	}

	if (this._req.overrideMimeType)
  		this._req.overrideMimeType('text/xml');
	this._req.open('POST', this._url, true);
	this._req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

	var data = '';
	
	if (this._name.length > 0)
		data += '&name=' + this._name;
		
	if (this._desc.length > 0)
		data += '&desc=' + this._desc;
		
	if (this._comm.length > 0)
		data += '&comm=' + this._comm;
		
	if (this._tag.length > 0)
		data += '&tag=' + this._tag;
		
	if (this._implements.length > 0)
		data += '&implements=' + this._implements;
		
	if (this._category.length > 0)
		data += '&category=' + this._category;
		
	if (this._publisher.length > 0)
		data += '&publisher=' + this._publisher;
		
	if (this._attrb.length > 0)
		data += '&attrb=' + this._attrb;
		
	if (this._orderby.length > 0)
		data += '&orderby=' + this._orderby;
		
	if (this._action.length > 0)
		data += '&action=' + this._action;
		
	if (this._guid.length > 0)
		data += '&guid=' + this._guid;
		
	if (this._count > 0)
		data += '&start=' + this._start + '&count=' + this._count;
		
	if (this._filter.length > 0)
		data += '&filter=' + this._filter;
		
	// remove the first &
	if (data.length > 0)
		data = data.substr(1);

	var myObj = this;
	this._req.onreadystatechange = function(e){myObj._onStateChange(e);};
	if (this._req.onerror) this._req.onerror = function(e){myObj._onError(e);};
	this._req.send(data);
	
	clearPageShortcuts();
  },

  _reset: function QM_reset() {
    this._start = 0;
  	this._count = 0;
  	this._name = '';
  	this._desc = '';
  	this._comm = '';
  	this._category = '';
  	this._publisher = '';
  	this._attrb = '';
  	this._orderby = '';
  },

  _onError: function QM_Error(e) {
  	alert("Error: " + e.target.status);
  },
  
  _onStateChange: function QM_onStateChange(e) {
  	if (this._req.readyState == 4)
    {
    	if (this._req.status == 200)
    	{	
			this._output = this._req.responseXML;
			if (!this._output || this._req.responseText.length == 0) 
			{
				hideAjax(this._type.substr(4));
				return;
			}

			if (this._type == "SubmitComment" && this._waitentity)
			{
				var p = this._waitentity.parentNode;
				p.removeChild(this._waitentity);
				
				// increment count
				var cc = document.getElementById("commCount");
				var count = parseInt(cc.getAttribute('count'))+1;
				var tcount = parseInt(cc.getAttribute('tcount'))+1;
				cc.setAttribute('count', count);
				if (this._attrb == '0')  // top level comment
					cc.setAttribute('tcount', tcount);
				cc.innerHTML = 'Comments ('+count+')';
				
				// show separator
				document.getElementById('hr_start').style.display = 'block';
				document.getElementById('hr_end').style.display = 'block';
				
				// reload comments
				removeAllComments();
				
				var res = tcount % CountPerPage;
				if (this._attrb == '0')
				{
					if (res == 0) res = CountPerPage;
					gStart = tcount - res;
				}
				fetchComments(this._guid, gStart, CountPerPage);
				
				return;
			}
			
			// get record count first
			var its = this._output.getElementsByTagName('items');
			if (its.length == 0) 
			{
				hideAjax(this._type.substr(4));
				return;
			}
			
			this._container = its[0];
			this._items = this._container.getElementsByTagName('item');
			if (!this._container || !this._items  || this._items.length == 0) 
			{
				hideAjax(this._type.substr(4));
				if (this._type.substr(4) == 'blueprint')
				{
					var tt = document.getElementById('blueprint');
					if (tt)
					{
						var dvs = tt.getElementsByTagName('div');
						dvs[0].innerHTML = 'Objects that implement this blueprint (0):';
					}
				}
				return;
			}

			this.parse_pos = 0;
			this.parse_end = (this._items.length > CountPerPage)? CountPerPage : this._items.length;
			this._removeAllObjs();
			
			if (this._items.length > 0)
			{	
				if (this._type.substr(4) == 'blueprint')
				{
					var tt = document.getElementById('blueprint');
					var dvs = tt.getElementsByTagName('div');
					dvs[0].innerHTML = 'Objects that implement this blueprint (' + this._items.length + '):';
					
					this._top.innerHTML = '<table><tr><td id="left-cell"/><td id="right-cell"/></tr></table>';
				}
				var myObj = this;
				setTimeout(function(){myObj._populateObjs();}, this._timeout);
			}
		}
    }
  },
  
  _onError: function QM_onError() {
  },
  
  _removeAllObjs: function QM_removeAllObjs() {
  	if (this._top)
  	{
  		while (this._top.firstChild)
  			this._top.removeChild(this._top.firstChild);
  	}
  },
  
  _populateObjs: function QM_populateObjs() {
  
  	if (this._rendering == 1 && this.parse_pos < this.parse_end)
  	{
  		// still rendering, reschedule
  		var myObj = this;
		setTimeout(function(){myObj._populateObjs();}, this._timeout);
		return;
	}
  		
  	// Parse 10 records each time
	var start = this.parse_pos;
	var key;
	this._xmlobjects= new Array();
	while (start < this.parse_end)
	{
		var ns = this._items[start].childNodes;
		if (this._type == "comm_container" || this._type == "SubmitComment")
		{
			this._xmlobjects[this._xmlobjects.length] = ns;
		}
		else if (getText(ns.item(2)) == getText(ns.item(14)))
		{
			if ( (this._type == "div_thumbsup" && parseInt(getText(ns.item(10))) <= min_display_threshold) ||
				 (this._type == "div_mostdled" && parseInt(getText(ns.item(9))) <= min_display_threshold) )
			{
				// do nothing
			}
			else
				this._xmlobjects[this._xmlobjects.length] = ns;
		}
		
		start++;
		if (this._xmlobjects.length == this._pagesize) break;
	}
	this.parse_pos = start;
	this._rendering = 1;
	if (this._renderPage) this._renderPage(this._xmlobjects);

	if (start < this.parse_end)
	{
		var myObj = this;
		setTimeout(function(){myObj._populateObjs();}, this._timeout);
	}
	else
	{
		// now render page nav bar
		if (this._type != "comm_container" && this._type != "SubmitComment")
		{
			if (gActiveBox.length > 0)
				renderPageShortcuts(this._items.length, CountPerPage, this._start);
		}
		
		if (this._type == 'div_blueprint')
		{
			var scr = new ScrollController('blueprint');
		}
		
		// make sure ajax is hidden
		hideAjax(this._type.substr(4));
	}
  },

  _renderObjs: function(ra) {
   	if (ra.length == 0) return;

   	if (this._retain == true)
   	{
   		if (this._type == 'div_featured')
			featured_book = ra.slice(0);
		else if (this._type == 'div_latest')
			latest_book = ra.slice(0);
		else if (this._type == 'div_thumbsup')
			thumbsup_book = ra.slice(0);
		else if (this._type == 'div_mostdled')
			mostdled_book = ra.slice(0);
		else if (this._type == 'div_tutorials')
			tutorials_book = ra.slice(0);
		else if (this._type == 'div_blueprint')
			blueprint_book = ra.slice(0);
		this._retain = false;
   	}

	// hide ajax
	hideAjax(this._type.substr(4));
	
	// show object
	this._panel.style.display = 'block';
	
	// clone temple, populate data, append to this._top
	this._renderIndex = 0;
   	if (this._type == 'div_blueprint')
   	{
   		template = document.getElementById('bprt-template');
   		for (var i=0; i<ra.length; i++)
   		{
   			this._renderOneObject(ra, false);
   		}
   		this._rendering = 0;
	}
	else
	{
		//use timer loop to render individual object
		setTimeout(this._renderOneObject.bind(this), 3, ra, true);
	}
  },
  
  _renderOneObject: function (ra, enableTimer) {
  	
  	var i = this._renderIndex++;
   	var c = template.cloneNode(true);
	var prs = c.getElementsByTagName('p');
	var sps = c.getElementsByTagName('span');
	var star = c.getElementsByTagName('li');
	
	var desc = Base64.decode(getText(ra[i][12]));
	if (desc.length > max_char_count)
		desc = desc.substr(0, max_char_count) + "...";
	
	var appname = Base64.decode(getText(ra[i][1]));
	prs[0].innerHTML = appname;
	prs[1].innerHTML = getTimeString(getText(ra[i][0]));
	prs[2].innerHTML = desc;
	var leadname = Base64.decode(getText(ra[i][16]));
	
	// handle image url
	var imgs = c.getElementsByTagName('img');
	var ftype = getText(ra[i][6]);
	if (ftype)
	{
		if (ftype.indexOf('mashup') >= 0)
			imgs[0].src = MASHUP_ICON;
		else if (ftype.indexOf('mbx') >= 0)
			imgs[0].src = MBX_ICON;
		else if (ftype.indexOf('prst') >= 0)
			imgs[0].src = PRST_ICON;
		else if (ftype.indexOf('bprt') >= 0)
			imgs[0].src = BPRT_ICON;
	}

	var imageurl = getText(ra[i][8]);
	if (imageurl && imageurl.length > 0)
	{
		var pat = /ccrx:[^\?]*\/([^\?\/]*)\?([^\?]*)/g;
		var result = null;
		while (result = pat.exec(imageurl))
		{
			var fname = result[1];
			var guid = result[2];
			if (guid.length > 0)
			{
				imageurl = "get.php?id="+guid+"&n="+encodeURIComponent(fname);
			}
		}
	}
	else if (ftype.indexOf('bprt') >= 0)
		imageurl = "http://www.otakhi.com/img/blueprint_large.png";
	else
		imageurl = "http://www.otakhi.com/img/undefined.png";
	imgs[1].src = imageurl;
	
	// get rating by dividing vote_count/votes
	var rating = 0;
	var votes = parseFloat(getText(ra[i][10]));
	if (votes > 0)
		rating = votes/parseFloat(getText(ra[i][11]));

	// get cost
	var appcost = getText(ra[i][18]);
	var numCost = appcost;
	if (appcost == '0') appcost = 'Free';
	else if (appcost == '1') appcost = '1 Mol';
	else appcost = appcost + ' Mols';
	
	sps[0].innerHTML = getText(ra[i][3]);
	sps[1].innerHTML = appcost;
	sps[2].innerHTML = getText(ra[i][9]) + " Downloads";
	
	var rating = rating.toFixed(1);
	giveRating(star[0], rating);
	
	var dvs = c.getElementsByTagName('div');
	dvs[2].innerHTML = '[ ' + getText(ra[i][5]) + ' ]&nbsp;';
	
	if (this._type == 'div_featured' || this._type == 'div_author' || this._type == 'div_category')
		dvs[0].style.backgroundImage = 'url(../img/topGradFeatured.png)';
	else if (this._type == 'div_latest')
		dvs[0].style.backgroundImage = 'url(../img/topGradLatest.png)';
	else if (this._type == 'div_thumbsup')
		dvs[0].style.backgroundImage = 'url(../img/topGradThumbsup.png)';
	else if (this._type == 'div_mostdled')
		dvs[0].style.backgroundImage = 'url(../img/topGradMostdled.png)';
	else if (this._type == 'div_tutorials')
		dvs[0].style.backgroundImage = 'url(../img/topGradTutorials.png)';
		
	imgs[0].setAttribute('guid', getText(ra[i][2]));
	imgs[0].setAttribute('unit', getText(ra[i][14]));
	imgs[0].setAttribute('lead', getText(ra[i][15]));
	imgs[0].setAttribute('detailurl', getText(ra[i][7]));
	imgs[0].setAttribute('imageurl', getText(ra[i][8]));
	imgs[0].setAttribute('filetype', ftype);
	imgs[0].setAttribute('publishby', getText(ra[i][3]));
	imgs[0].setAttribute('filename', appname);
	imgs[0].setAttribute('leadname', leadname);
	imgs[0].setAttribute('cost', numCost);
	
	imgs[1].setAttribute('guid', getText(ra[i][2]));
	imgs[1].setAttribute('unit', getText(ra[i][14]));
	imgs[1].setAttribute('lead', getText(ra[i][15]));
	imgs[1].setAttribute('detailurl', getText(ra[i][7]));
	imgs[1].setAttribute('imageurl', getText(ra[i][8]));
	imgs[1].setAttribute('filetype', ftype);
	imgs[1].setAttribute('publishby', getText(ra[i][3]));
	imgs[1].setAttribute('filename', appname);
	imgs[1].setAttribute('leadname', leadname);
	imgs[1].setAttribute('cost', numCost);
	
	c.style.display = 'block';
	c.setAttribute('id', 'clone'+i);
	
	if (this._type == 'div_blueprint')
	{
		var td = document.createElement('td');
		td.style.margin = '0px';
		td.style.padding = '0px';
		var rc = document.getElementById('right-cell');
		if (rc)
		{
			td.appendChild(c);
			rc.parentNode.insertBefore(td, rc);
		}
		
		/* for testing purpose
		for (var i=0; i<10; i++)
		{
		var td = document.createElement('td');
		td.style.margin = '0px';
		td.style.padding = '0px';
		td.appendChild(c.cloneNode(true));
		rc.parentNode.insertBefore(td, rc);
		}
		*/
	}
	else
	{
		var rc = this._top.appendChild(c);
		//use a timeout to show object
		setTimeout(function (ri) { ri.setAttribute('appear', true); }, 1, rc);
	}
	
	if (enableTimer)
	{
		if (this._renderIndex < ra.length) 
			setTimeout(this._renderOneObject.bind(this), 3, ra, true);
		else
			this._rendering = 0;
	}
  },
  
  _renderComments: function (ra) {
   	if (ra.length == 0) return;
	
	// show object
	this._panel.style.display = 'block';

   	for (var i=0; i<ra.length; i++)
   	{
   		// clone temple, populate data, append to this._top
		var c = template.cloneNode(true);
		var ps = c.getElementsByTagName('p');
		var ss = c.getElementsByTagName('span');
		
		if (hasCCRX)
			ps[1].style.display = 'block';
		
		var guid = getText(ra[i][1]);
		var replyto = getText(ra[i][3]);
		
		ss[0].innerHTML = getText(ra[i][2]);
		ss[1].innerHTML = "("+getTimeString(getText(ra[i][0]))+")";
		ps[2].innerHTML = getText(ra[i][4]);
		
		c.setAttribute('guid', guid);
		c.setAttribute('replyto', replyto);
		
		if (replyto != "0")
		{
			// indent 
			ps[0].style.paddingLeft = '30px';
			ps[2].style.paddingLeft = '30px';
		}
		
		c.style.display = 'block';
		this._panel.appendChild(c);
		
		// if not the last, add separator
		if (i < ra.length - 1)
		{
			var sep = document.createElement('hr');
			sep.setAttribute('class', 'commSep');
			this._panel.appendChild(sep);
		}
	}
	
	var cc = document.getElementById("commCount");
	if (cc)
	{
		var tcount = parseInt(cc.getAttribute('tcount'));
		addPageShortcuts(tcount, CountPerPage, gStart);
	}
  },

};


function getTimeString(t)
{
	var str = '';
	
	if (!t || t.length == 0) return str;

	var diff = 0;
	if (t.indexOf('-') > 0)
	{
		var d = t.split(" ");
		var f = d[0].split('-');
		var s = d[1].split(':');
		var pyear = parseInt(f[0], 10);
		var pmon = parseInt(f[1], 10);
		var pday = parseInt(f[2], 10);
		var phour = parseInt(s[0], 10);
		var pmin = parseInt(s[1], 10);
		var psec = parseInt(s[2], 10);
	
		var pd = new Date(0); 
		pd.setHours(phour,pmin,psec,0); 
		pd.setFullYear(pyear, pmon-1, pday);
	
		var c = new Date();
		var offset = c.getTimezoneOffset()*60000;
	
		diff = (c.getTime() + offset - pd.getTime())/1000.;
	
		if (isDaylight()) diff = diff - 3600.;
	}
	else
	{
		var d = new Date;
		var now = parseInt(d.getTime()/1000.);
		var gmtDiff = d.getTimezoneOffset() * 60;
		var gmt = now + gmtDiff;
		diff = gmt - t;
	}

	var gtyr = (diff > 31536000);
	var gt30 = (diff > 2592000);
	
	if (gtyr)
	{
		var yg = parseInt(parseFloat(diff)/ 31536000.);
		if (yg == 1)
			str = '1 year '
		else
			str = yg + " years ";
			
		diff -= yg*31536000;
		gt30 = (diff > 2592000);
	}
	
	if (1) 
	{
		if (gt30)
		{
			var mg = parseInt(parseFloat(diff)/ 2592000.);
			if (mg > 0)
			{
				var rs = parseInt(diff - mg*2592000.);
				var dg = Math.floor(parseFloat(rs)/ 86400.);
				if (mg == 1)
					str += '1 month ';
				else
					str += mg + ' months ';
					
				if (!gtyr)
				{
					if (dg == 0)
					{
						rs -= dg*86400.;
						var hc = Math.floor(parseFloat(rs)/ 3600.);
						if (hc == 1)
							str += '1 hour ';
						else if (hc > 1)
							str += hc + ' hours ';
						
					}
					else if (dg == 1)
						str += '1 day ';
					else if (dg > 1)
						str += dg + ' days ';
				}
			}
		}
		else // less than a month
		{
			var dg = Math.floor(parseFloat(diff)/ 86400.);
			if (dg >= 30)
				str = dg + ' days ';
			else if (dg > 0)
			{
				var rh = diff - (dg*86400);
				if (dg == 1)
					str = '1 day ';
				else
					str = dg + ' days ';
					
				var hg = Math.floor(parseFloat(rh)/ 3600.);
				if (hg > 0)
				{
					if (hg == 1)
						str += hg + ' hour ';
					else
						str += hg + ' hours ';
				}
			}
			else  // less than a day
			{
				var hg = Math.floor(parseFloat(diff)/ 3600.);
				if (hg > 24)
					str = hg + ' hours ';
				else if (hg > 0)
				{
					var rm = diff - (hg*3600);
					if (hg == 1)
						str = hg + ' hour ';
					else
						str = hg + ' hours ';
						
					var mg = parseInt(parseFloat(rm)/ 60.);
					if (mg > 0)
					{
						if (mg == 1)
							str += mg + ' min ';
						else
							str += mg + ' mins ';
					}
				}
				else  // less than an hour
				{
					var mg = parseInt(parseFloat(diff)/ 60.);
					if (mg > 0)
					{
						var rs = parseInt(diff - (mg*60));
						if (mg == 1)
							str = mg + ' min ';
						else
							str = mg + ' mins ';
						
						if (rs > 1)
							str += rs + ' seconds ';
					}
					else
					{
						if (diff >= 1)
							str = parseInt(diff) + ' seconds ';
					}
				}
			}
		}
	}
	if (str.length > 0) str += 'ago';
	return str;
}
function showAjax(n)
{
	var id = 'ajax_'+n;
	var e = document.getElementById(id);
	if (e)
	{
		e.style.display = 'block';
	}
}
function hideAjax(n)
{
	var id = 'ajax_'+n;
	var e = document.getElementById(id);
	if (e)
	{
		e.style.display = 'none';
	}
}
function loadAppsOnStartup()
{
	var agent = navigator.userAgent;
	if (agent.indexOf('oTakhi') > 0)
	{
		document.getElementById('manage-account').style.display = 'block';
	}
	
	gStage = 'home';
	gFilter = document.getElementById("filter");
	gFilter.style.display = 'block';
	
	var ft = readCookie('filter');
	if (ft && ft.length > 0)
	{
		var al = document.getElementById('all');
		var wa = document.getElementById('prst');
		var fo = document.getElementById('mashup');
		var dr = document.getElementById('mbx');
		al.src = '../img/blank.png';
		wa.src = '../img/blank.png';
		fo.src = '../img/blank.png';
		dr.src = '../img/blank.png';
	
		var e = document.getElementById(ft);
		e.src = '../img/checked.png';
		gLastHomeFilter = e;
	}

	var im = new Image(140, 100);
	im.src = "../img/loading.gif";
	
	template = document.getElementById("template");

	resetAllButtonName('More &rsaquo;');
	
	clearBox('div_featured');
	clearBox('div_latest');
	clearBox('div_mostdled');
	clearBox('div_tutorials');
	clearBox('div_thumbsup');
	clearBox('div_author');
	clearBox('div_category');
	
	showAjax('latest');
	showAjax('thumbsup');
	showAjax('mostdled');
	showAjax('tutorials');

	featured_master = new QueryMaster("div_featured");
	latest_master = new QueryMaster("div_latest");
	thumbsup_master = new QueryMaster("div_thumbsup");
	mostdled_master= new QueryMaster("div_mostdled");
	tutorials_master = new QueryMaster("div_tutorials");
	
	featured_master._start = 0;
	featured_master._count = 8;
	featured_master._attrb = 'featured';
	featured_master._retain = true;
	featured_master._makeQuery();
	
	latest_master._start = 0;
	latest_master._count = 8;
	latest_master._retain = true;
	latest_master._makeQuery();
	
	thumbsup_master._start = 0;
	thumbsup_master._count = 8;
	thumbsup_master._orderby = 'votes';
	thumbsup_master._retain = true;
	thumbsup_master._makeQuery();
	
	mostdled_master._start = 0;
	mostdled_master._count = 8;
	mostdled_master._orderby = 'pageview';
	mostdled_master._retain = true;
	mostdled_master._makeQuery();
	
	tutorials_master._start = 0;
	tutorials_master._count = 8;
	tutorials_master._attrb = 'tutorial';
	tutorials_master._retain = true;
	tutorials_master._makeQuery();
	
	setTimeout(showEndLinks, 3000);
}

function loadBlueprintOnStartup()
{
	var im = new Image(140, 100);
	im.src = "../img/loading.gif";
	
	template = document.getElementById("bprt-template");

	clearBox('div_blueprint');
	showAjax('blueprint');

	blueprint_master = new QueryMaster("div_blueprint");
	
	blueprint_master._start = 0;
	blueprint_master._count = 9999;
	blueprint_master._implements = document.getElementsByTagName('img')[0].getAttribute('guid');
	blueprint_master._retain = true;
	blueprint_master._filter = 'all';
	blueprint_master._makeQuery();
}

function loadAppCommentsOnStartup()
{
	document.getElementById('edit-doc').style.display = 'none';
	document.getElementById('edit-comment').style.display = 'none';
	
	var agent = navigator.userAgent;
	if (agent.indexOf('oTakhi') > 0)
	{
		var f = document.getElementById('featured');
		if (hasCCRX && f)
		{
			var cxx = getCCRX();
			var user = cxx.CCRxGetState('current_user');
			var is = f.getElementsByTagName('img');
			var pb = is[0].getAttribute('publishby');
			gAppTitle = is[0].getAttribute('filename');
			if (user == pb) 
			{
				document.getElementById('edit-doc').style.display = 'inline';
				document.getElementById('edit-comment').style.display = 'inline';
			}
		}
	}
	
	template = document.getElementById("template");
	gSubmitTemplate = document.getElementById('fssubmit');

	var tse = document.getElementById("app_timestamp");
	if (tse)
	{
		var ts = tse.getAttribute('timestamp');
		ts = getTimeString(ts);
		tse.innerHTML = ts;
	}
	
	if (hasCCRX)
	{
		var pc = document.getElementById("postacomment");
		if (pc) pc.style.display = 'inline';
	}
	
	// get number of comments available
	var cc = document.getElementById("commCount");
	if (cc)
	{
		var count = parseInt(cc.getAttribute('count'));
		if (count > 0)
		{
			document.getElementById('hr_start').style.display = 'block';
			document.getElementById('hr_end').style.display = 'block';
			
			// get file_guid
			var frd = document.getElementById("featured");
			if (frd)
			{
				var guid = frd.getAttribute('guid');
	
				// fetch comments
				fetchComments(guid, 0, CountPerPage);
			}
		}
	}
	
	// handle ranking setup
	if (hasCCRX)
	{
		var msg = '';
		var fd = document.getElementById('featured');
		var guid = fd.getAttribute('guid');
		var imgs = fd.getElementsByTagName('img');
		
		var cx = new CCRX();
		var rank = parseInt(cx.CCRxExecute('GetRank', guid));
		if (rank == 0)
		{
			msg = 'Rate this ';
			if (imgs[0].src == "http://www.otakhi.com/img/mashup16x16.png")
				msg += 'formation:';
			else if (imgs[0].src == "http://www.otakhi.com/img/mbox16x16.png")
				msg += 'droplet:';
			else if (imgs[0].src == "http://www.otakhi.com/img/presentation16x16.png")
				msg += 'application:';
			else if (imgs[0].src == "http://www.otakhi.com/img/bprt16x16.png")
				msg += 'blueprint:';
		}
		else if (rank>0 && rank<6)
		{
			msg = 'You rated this ';
			if (imgs[0].src == "http://www.otakhi.com/img/mashup16x16.png")
				msg += 'formation as ';
			else if (imgs[0].src == "http://www.otakhi.com/img/mbox16x16.png")
				msg += 'droplet as ';
			else if (imgs[0].src == "http://www.otakhi.com/img/presentation16x16.png")
				msg += 'application as ';
			else if (imgs[0].src == "http://www.otakhi.com/img/bprt16x16.png")
				msg += 'blueprint as ';
				
			showRankingAs(rank);
			gHasRanked = true;
			document.getElementById('rankAgain').style.display = 'block';
		}
		
		var sr = document.getElementById('submitRank');
		sr.style.display = 'block';
		
		document.getElementById('rankHeader').innerHTML = msg;
	}
}

function removeAllComments()
{
	var ct = document.getElementById('container');
	if (ct)
	{
		while (ct.firstChild)
			ct.removeChild(ct.firstChild);
	}
}

function addPageShortcuts(count, perPage, start)
{
	// count are the top level comments not reply-to comments
	if (count > perPage)
	{
		document.getElementById('hr_end').style.display = 'none';
	
		// at least 2 pages
		var bar = document.getElementById('pageBar');
		
		// remove existing items
		while (bar.firstChild)
			bar.removeChild(bar.firstChild);
			
		document.getElementById('pagePrev').style.display = 'none';
		document.getElementById('pageNext').style.display = 'none';
			
		var p = document.getElementById('pages');
		
		start++;
		var cp = Math.ceil(start/perPage);
		var tp = Math.ceil(count/perPage);
		
		var start = cp - 2;
		if (start < 1) start = 1;
		var end = cp + 2;
		if (end > tp) end = tp;
		
		var bd = document.createElement('span');
		bd.style.paddingRight = '10px';
		bd.innerHTML = 'Pages';
		bar.appendChild(bd);
			
		if (start > 1)
		{
			bd = document.createElement('span');
			bd.style.paddingRight = '5px';
			bd.innerHTML = '...';
			bar.appendChild(bd);
		}
			
		for (var i=start; i<=end; i++)
		{
			if (i == cp)
			{
				var ae = document.createElement('span');
				ae.innerHTML = i;
				ae.style.color = 'black';
				ae.style.paddingLeft = '5px';
				bar.appendChild(ae);
			}
			else
			{
				var ae = document.createElement('span');
				ae.innerHTML = i;
				ae.style.color = 'blue';
				ae.style.textDecoration = 'underline';
				ae.style.cursor = 'pointer';
				ae.style.paddingLeft = '5px';
				ae.setAttribute('onclick', "gotoPage("+i+");");
				bar.appendChild(ae);
			}
		}
		
		if (end < tp)
		{
			bd = document.createElement('span');
			bd.style.paddingLeft = '5px';
			bd.innerHTML = '...';
			bar.appendChild(bd);
		}
		
		if (cp > 1)
		{
			document.getElementById('pagePrev').style.display = 'block';
			document.getElementById('pagePrev').setAttribute('onclick', "gotoPage("+(cp-1)+");");
		}
			
		if (cp < tp)
		{
			document.getElementById('pageNext').style.display = 'block';
			document.getElementById('pageNext').setAttribute('onclick', "gotoPage("+(cp+1)+");");
		}
		
		p.style.display = 'block';
	}
}
function gotoPage(p)
{
	// get file_guid
	var frd = document.getElementById("featured");
	if (frd)
	{
		var guid = frd.getAttribute('guid');
	
		removeAllComments();
	
		// fetch comments
		var sr = (p-1) * CountPerPage;
		fetchComments(guid, sr, CountPerPage);
	}
}
function fetchComments(guid, start, count)
{
	gStart = start;
	comm_master = new QueryMaster("comm_container");
	
	comm_master._url = "query.php?action=getComments&guid="+guid+"&start="+start+"&count="+count;
	comm_master._renderPage = comm_master._renderComments;
	comm_master._makeQuery();
}

function isDaylight() {
 
    // 2nd sunday of march
    oDate = new Date();
    var dstStartDate = new Date();
    dstStartDate.setMonth(2);
    dstStartDate.setDate(1);
    dstStartDate.setDate(8 - dstStartDate.getDay() + 7);
 
    // first sunday of november
    var dstEndDate = new Date();
    dstEndDate.setMonth(10);
    dstEndDate.setDate(1);
    dstEndDate.setDate(8 - dstEndDate.getDay());
 
    if ((oDate.getMonth() > dstStartDate.getMonth()) && (oDate.getMonth() < dstEndDate.getMonth())) {
        bInDST = true;
    } else if (oDate.getMonth() == dstStartDate.getMonth()) {
        if (oDate.getDate() >= dstStartDate.getDate()) {
            bInDST = true;
        } else {
            bInDST = false;
        }
    } else if (oDate.getMonth() == dstEndDate.getMonth()) {
        if (oDate.getDate() < dstEndDate.getDate()) {
            bInDST = true;
        } else {
            bInDST = false;
        }
    } else {
        bInDST = false;
    }
    return bInDST;
}


function openTearNShare()
{
	var obj = document.getElementById('tearnshare');
	var opn = document.getElementById('tearnshareopen');
	obj.style.display = "none";
	opn.style.display = "block";
}

function closeTearNShare()
{
	var obj = document.getElementById('tearnshare');
	var opn = document.getElementById('tearnshareopen');
	obj.style.display = "block";
	opn.style.display = "none";
}

function visitAppPage(e)
{
	var t = (e.target || e.srcElement);
	var p = t.parentNode.parentNode;
	if (p)
	{
		var imgs = p.getElementsByTagName('img');
		var guid = imgs[0].getAttribute('guid');
		var uri = 'query.php?action=getAppDetail&guid='+guid;
		window.location.href = uri;
	}
}

function visitAuthorPage(e)
{
	hideEndLinks();
	hideAllBoxes();
	gSearch = 0;
	gStage = 'div_author';
	gActiveBox = 'div_author';
	var at = document.getElementById('author');
	
	var t = (e.target || e.srcElement);
	var nm = t.innerHTML;
	gAuthor = nm;
	
	document.getElementById('authorName').innerHTML = nm;
	document.getElementById('authorBack').innerHTML = 'Back';
	document.getElementById('authorBack').setAttribute('onclick', "loadHomeCategories(event);");
	
	clearBox(gActiveBox);
	
	showAjax('author');
		
	var master = new QueryMaster(gActiveBox);
	master._start = 0;
	master._count = CountPerPage * 4;
	master._publisher = nm;
	master._makeQuery();

	at.style.display = 'block';
	setTimeout(showEndLinks, 700);
}
function visitCatPage(e)
{
	hideEndLinks();
	hideAllBoxes();
	gSearch = 0;
	gStage = 'div_category';
	gActiveBox = 'div_category';
	var at = document.getElementById('category');
	
	var t = (e.target || e.srcElement);
	var p = t.innerHTML;
	if (p.length > 0)
	{
		p = p.replace(/\&nbsp;$/, "");
		p = p.replace(/^[\s\[]*/, "");
		var cat = p.replace(/[\s\]]*$/, "");
		gCategory = cat;
		
		var cn = document.getElementById('catName');
		var cop = cn.getElementsByTagName('option');
		for (var i=0; i<cop.length; i++)
		{
			if (cop[i].value == cat)
			{
				cop[i].selected = "1";
				break;
			}
		}
	
		document.getElementById('catBack').innerHTML = 'Back';
		document.getElementById('catBack').setAttribute('onclick', "loadHomeCategories(event);");
	
		clearBox(gActiveBox);
		
		showAjax('category');
		
		var master = new QueryMaster(gActiveBox);
		master._start = 0;
		master._count = CountPerPage * 4;
		master._category = cat;
		master._makeQuery();

		at.style.display = 'block';
		
		setTimeout(showEndLinks, 700);
	}
}
function renderCategory(p)
{
	gSearch = 0;
	gActiveBox = 'div_category';
	clearBox(gActiveBox);
	
	// clear search text
	var pn = document.getElementById(gActiveBox).parentNode;
	var txt = pn.getElementsByTagName('input');
	txt[0].value = "";
		
	showAjax('category');
	
	var master = new QueryMaster(gActiveBox);
	master._start = 0;
	master._count = CountPerPage * 4;
	master._category = p;
	master._makeQuery();
}
function closeOpenSubmit()
{
	if (gOpenSubmit)
	{
		var parent = gOpenSubmit.parentNode;
		if (parent) parent.removeChild(gOpenSubmit);
		gOpenSubmit = null;
	}
}
function postComment(e)
{
	if (!hasCCRX || !gSubmitTemplate) return;
	
	closeOpenSubmit();

	var c = gSubmitTemplate.cloneNode(true);
	
	var t = (e.target || e.srcElement);
	var parent = t.parentNode.parentNode;
	parent.appendChild(c);
	
	c.setAttribute('replyto', '0');
	
	c.style.display = 'block';
	
	var ts = c.getElementsByTagName('textarea');
	ts[0].focus();
	
	c.scrollIntoView(false);
	
	gOpenSubmit = c;
}

function postReply(e)
{
	if (!hasCCRX) return;
	
	closeOpenSubmit();

	var p = document.getElementById('fssubmit');
	var c = p.cloneNode(true);
	
	var t = (e.target || e.srcElement);
	var parent = t.parentNode;
	parent.appendChild(c);
	
	var ent = parent.getAttribute('guid');
	var rt = parent.getAttribute('replyto');
	if (rt != '0') ent = rt;
	
	c.setAttribute('replyto', ent);
	c.style.display = 'block';
	
	var ts = c.getElementsByTagName('textarea');
	ts[0].focus();
	
	c.scrollIntoView(false);
	
	gOpenSubmit = c;
}

function cancelSubmit(e)
{
	var t = (e.target || e.srcElement);
	var p = t.parentNode;
	var parent = t.parentNode.parentNode;
	parent.removeChild(p);
	gOpenSubmit = null;
}

function doSubmit(e)
{
	if (!hasCCRX) return;
	
	var cx = getCCRX();
	var user = cx.CCRxGetState('current_user_guid');
	
	var t = (e.target || e.srcElement);
	var p = t.parentNode;
	var replyto = p.getAttribute('replyto');
	
	var ts = p.getElementsByTagName('textarea');
	var data = ts[0].value;
	
	var frd = document.getElementById("featured");
	var guid = frd.getAttribute('guid');
				
	var sc = new QueryMaster("SubmitComment");
	
	sc._url = 'query.php?action=submitComment';
	sc._name = user;
	sc._comm = encodeURIComponent(data);
	sc._attrb = replyto;
	sc._guid = guid;
	sc._renderPage = null;
	sc._waitentity = p;
	sc._makeQuery();
	
	gOpenSubmit = null;
}

function hideAllBoxes()
{
	var au = document.getElementById('author');
	var ca = document.getElementById('category');
	var fe = document.getElementById('featured');
	var la = document.getElementById('latest');
	var mo = document.getElementById('mostdled');
	var th = document.getElementById('thumbsup');
	var tu = document.getElementById('tutorials');
	
	if (au) au.style.display = 'none';
	if (ca) ca.style.display = 'none';
	if (fe) fe.style.display = 'none';
	if (la) la.style.display = 'none';
	if (mo) mo.style.display = 'none';
	if (th) th.style.display = 'none';
	if (tu) tu.style.display = 'none';
	
	if (au) au.removeAttribute('bkgd-fade');
	if (ca) ca.removeAttribute('bkgd-fade');
	if (fe) fe.removeAttribute('bkgd-fade');
	if (la) la.removeAttribute('bkgd-fade');
	if (mo) mo.removeAttribute('bkgd-fade');
	if (th) th.removeAttribute('bkgd-fade');
	if (tu) tu.removeAttribute('bkgd-fade');
}

function showHomeBoxes()
{
	var la = document.getElementById('latest');
	la.style.minHeight = "200px";
	la.style.display = 'block';
	var mo = document.getElementById('mostdled');
	mo.style.minHeight = "200px";
	mo.style.display = 'block';
	var th = document.getElementById('thumbsup');
	th.style.minHeight = "200px";
	th.style.display = 'block';
	var tu = document.getElementById('tutorials');
	tu.style.minHeight = "200px";
	tu.style.display = 'block';
}

function clearBox(n)
{
	var t = document.getElementById(n);
	if (!t) return;
	while (t.firstChild)
		t.removeChild(t.firstChild);
}

function expandBox(e)
{
	gSearch = 0;
	hideAllBoxes();
	var t = (e.target || e.srcElement);
	
	// change label to Back.
	t.innerHTML = 'Back';
	t.setAttribute('onclick', "loadHomeCategories(event);");
	
	t.parentNode.parentNode.style.display = 'block';
	
	var n = 'div_'+t.parentNode.parentNode.id;
	gStage = n;
	gActiveBox = n;
	clearBox(n);
	
	showAjax(t.parentNode.parentNode.id);
	
	template = document.getElementById("template");
	
	var master = new QueryMaster(n);
	master._start = 0;
	master._count = CountPerPage * 4;
		
	if (n == 'div_featured')
	{
		master._attrb = 'featured';
	}
	else if (n == 'div_latest')
	{
		// do nothing
	}
	else if (n == 'div_thumbsup')
	{
		master._orderby = 'votes';
	}
	else if (n == 'div_mostdled')
	{
		master._orderby = 'pageview';
	}
	else if (n == 'div_tutorials')
	{
		master._attrb = 'tutorial';
	}
	master._makeQuery();
}
function clearPageShortcuts()
{
	var bar = document.getElementById('pageBar');
	if (bar)
	{
		while (bar.firstChild)
			bar.removeChild(bar.firstChild);
	}
		
	var p = document.getElementById('pages');
	if (p) p.style.display = 'none';
}
function renderPageShortcuts(count, perPage, start)
{	
	// count are the top level comments not reply-to comments
	if (start >= perPage || count > perPage)
	{
		var bar = document.getElementById('pageBar');
		var p = document.getElementById('pages');
	
		// at least 2 pages
		document.getElementById('pagePrev').style.display = 'none';
		document.getElementById('pageNext').style.display = 'none';

		start++;
		var cp = Math.ceil(start/perPage);
		var tp = Math.ceil(count/perPage);
		
		var start = cp - 2;
		if (start < 1) start = 1;
		var end = cp + 2;
		if (end > tp+cp-1) end = tp+cp-1;
		
		var bd = document.createElement('span');
		bd.style.paddingRight = '10px';
		bd.innerHTML = 'Pages';
		bar.appendChild(bd);
			
		if (start > 1)
		{
			bd = document.createElement('span');
			bd.style.paddingRight = '5px';
			bd.innerHTML = '...';
			bar.appendChild(bd);
		}
			
		for (var i=start; i<=end; i++)
		{
			if (i == cp)
			{
				var ae = document.createElement('span');
				ae.innerHTML = i;
				ae.style.color = 'black';
				ae.style.paddingLeft = '5px';
				bar.appendChild(ae);
			}
			else
			{
				var ae = document.createElement('span');
				ae.innerHTML = i;
				ae.style.color = 'blue';
				ae.style.textDecoration = 'underline';
				ae.style.cursor = 'pointer';
				ae.style.paddingLeft = '5px';
				ae.setAttribute('onclick', "renderPage("+i+");");
				bar.appendChild(ae);
			}
		}
		
		if (end < tp+cp-1)
		{
			bd = document.createElement('span');
			bd.style.paddingLeft = '5px';
			bd.innerHTML = '...';
			bar.appendChild(bd);
		}
		
		if (cp > 1)
		{
			document.getElementById('pagePrev').style.display = 'block';
			document.getElementById('pagePrev').setAttribute('onclick', "renderPage("+(cp-1)+");");
		}
			
		if (cp < end)
		{
			document.getElementById('pageNext').style.display = 'block';
			document.getElementById('pageNext').setAttribute('onclick', "renderPage("+(cp+1)+");");
		}
		
		p.style.display = 'block';
	}
}

function renderPage(p)
{
	if (gActiveBox.length == 0) return;

	clearBox(gActiveBox);
		
	var master = new QueryMaster(gActiveBox);
	master._start = (p-1) * CountPerPage;
	master._count = CountPerPage * 4;
	
	if (gSearch == 1)
	{
		var elem = document.getElementById(gActiveBox).parentNode;
		var txt = elem.getElementsByTagName('input');
		var srt = txt[0].value;
		
		// search text in name, desc, and tag
		master._name = srt;
		master._desc = srt;
		master._tag = srt;
	}
		
	if (gActiveBox == 'div_featured')
	{
		master._attrb = 'featured';
	}
	else if (gActiveBox == 'div_latest')
	{
		// do nothing
	}
	else if (gActiveBox == 'div_thumbsup')
	{
		master._orderby = 'votes';
	}
	else if (gActiveBox == 'div_mostdled')
	{
		master._orderby = 'pageview';
	}
	else if (gActiveBox == 'div_tutorials')
	{
		master._attrb = 'tutorial';
	}
	else if (gActiveBox == 'div_author')
	{
		master._publisher = document.getElementById('authorName').innerHTML;
	}
	else if (gActiveBox == 'div_category')
	{
		var cn = document.getElementById('catName');
		var cop = cn.getElementsByTagName('option');
		var cat = cop[cn.selectedIndex].innerHTML;
		master._category = cat;
	}
	master._makeQuery();
}

function search(e)
{
	var t = (e.target || e.srcElement);
	var p = t.parentNode;
	
	var txt = p.getElementsByTagName('input');
	var srt = txt[0].value;
	if (srt.length == 0) return;
	
	var ppn = t.parentNode.parentNode.parentNode;
	gActiveBox = 'div_'+ppn.id;
	gSearch = 1;
	
	showAjax(ppn.id);
	
	hideAllBoxes();
	ppn.style.display = 'block';
	
	// change label to Back.
	var dvs = ppn.getElementsByTagName('div');
	dvs[1].innerHTML = 'Back';
	dvs[1].setAttribute('onclick', "loadHomeCategories(event);");
	
	clearBox(gActiveBox);
	
	var master = new QueryMaster(gActiveBox);
	master._start = 0;
	master._count = CountPerPage * 4;
		
	// search text in name, desc, guid, and tag
	master._name = srt;
	master._desc = srt;
	master._tag = srt;
	master._guid = srt;
	
	if (gActiveBox == 'div_featured')
	{
		master._attrb = 'featured';
	}
	else if (gActiveBox == 'div_latest')
	{
		// do nothing
	}
	else if (gActiveBox == 'div_thumbsup')
	{
		master._orderby = 'votes';
	}
	else if (gActiveBox == 'div_mostdled')
	{
		master._orderby = 'pageview';
	}
	else if (gActiveBox == 'div_tutorials')
	{
		master._attrb = 'tutorial';
	}
	else if (gActiveBox == 'div_author')
	{
		master._publisher = document.getElementById('authorName').innerHTML;
	}
	else if (gActiveBox == 'div_category')
	{
		var cn = document.getElementById('catName');
		var cop = cn.getElementsByTagName('option');
		var cat = cop[cn.selectedIndex].innerHTML;
		master._category = cat;
	}
	master._makeQuery();
}

function starMouseOver(e)
{
	if (gHasRanked) return;
	
	var t = (e.target || e.srcElement);
	var id = parseInt(t.id.substr(4));
	
	showRankingAs(id);
}
function showRankingAs(r)
{
	var p = document.getElementById("rankStars");
	var ss = p.getElementsByTagName('div');
	
	for (var i=0; i<r; i++)
		ss[i].style.background = 'url(http://www.otakhi.com/img/redstar.png)';

	for (var i=r; i<5; i++)
		ss[i].style.background = 'url(http://www.otakhi.com/img/yellowstar.png)';
	
	var msg = document.getElementById('rankMessage');
	var aMsg = '&nbsp;&nbsp;&nbsp;&nbsp;';
	if (r == 1) msg.innerHTML = aMsg+'Not recommended';
	else if (r == 2) msg.innerHTML = aMsg+'Poor';
	else if (r == 3) msg.innerHTML = aMsg+'Okay';
	else if (r == 4) msg.innerHTML = aMsg+'Good';
	else if (r == 5) msg.innerHTML = aMsg+'Excellent';
}
function sumitStarRank(e)
{
	e.stopPropagation();
	
	if (gHasRanked) return;
	
	var t = (e.target || e.srcElement);
	var id = parseInt(t.id.substr(4));
	
	if (hasCCRX)
	{
		var fd = document.getElementById('featured');
		var guid = fd.getAttribute('guid');
		
		var author = document.getElementById('appAuthor').innerHTML;
		
		var cx = new CCRX();
		var user = cx.CCRxGetState("current_user");
		var isOwned = ( user == author);
		
		var rank = guid + '=' + id;
		cx.CCRxExecute('SetRank', rank);
		
		if (!isOwned)
		{
			// now do the actual submit
			var master = new QueryMaster("submitRank");
	
			master._url = "query.php?action=submitRank&guid="+guid+"&rank="+id;
			master._renderPage = null;
			master._makeQuery();
		}
		
		// show your ranking
		var imgs = fd.getElementsByTagName('img');
		
		var msg = 'You rated this ';
		if (imgs[0].src == "http://www.otakhi.com/img/mashup16x16.png")
			msg += 'formation as ';
		else if (imgs[0].src == "http://www.otakhi.com/img/mbox16x16.png")
			msg += 'droplet as ';
		else if (imgs[0].src == "http://www.otakhi.com/img/presentation16x16.png")
			msg += 'application as ';
		else if (imgs[0].src == "http://www.otakhi.com/img/bprt16x16.png")
			msg += 'blueprint as ';
				
		document.getElementById('rankHeader').innerHTML = msg;
		showRankingAs(id);
		gHasRanked = true;
		document.getElementById('rankAgain').style.display = 'block';
	}
}

function allowRankAgain()
{
	gHasRanked = false;

	var fd = document.getElementById('featured');

	var imgs = fd.getElementsByTagName('img');
		
	var msg = 'Rate this ';
	if (imgs[0].src == "http://www.otakhi.com/img/mashup16x16.png")
		msg += 'formation:';
	else if (imgs[0].src == "http://www.otakhi.com/img/mbox16x16.png")
		msg += 'droplet:';
	else if (imgs[0].src == "http://www.otakhi.com/img/presentation16x16.png")
		msg += 'application:';
	else if (imgs[0].src == "http://www.otakhi.com/img/bprt16x16.png")
		msg += 'blueprint:';
		
	document.getElementById('rankHeader').innerHTML = msg;
	document.getElementById('rankAgain').style.display = 'none';
	setTimeout(showYourRank, 7000);
}

function showYourRank()
{
	if (gHasRanked == false)
	{
		var fd = document.getElementById('featured');
		var guid = fd.getAttribute('guid');
		
		var cx = new CCRX();
		var rank = parseInt(cx.CCRxExecute('GetRank', guid));
		if (rank != 0)
		{
			// show your ranking
			var imgs = fd.getElementsByTagName('img');
		
			var msg = 'You rated this ';
			if (imgs[0].src == "http://www.otakhi.com/img/mashup16x16.png")
				msg += 'formation as ';
			else if (imgs[0].src == "http://www.otakhi.com/img/mbox16x16.png")
				msg += 'droplet as ';
			else if (imgs[0].src == "http://www.otakhi.com/img/presentation16x16.png")
				msg += 'application as ';
			else if (imgs[0].src == "http://www.otakhi.com/img/bprt16x16.png")
				msg += 'blueprint as ';
				
			document.getElementById('rankHeader').innerHTML = msg;
			showRankingAs(rank);
			gHasRanked = true;
			document.getElementById('rankAgain').style.display = 'block';
		}
	}
}

function loadHomeCategories(e)
{
	gStage = 'home';
	
	hideEndLinks();

	var t = (e.target || e.srcElement);
	
	// change label to Back.
	t.innerHTML = 'More &rsaquo;';
	t.setAttribute('onclick', "expandBox(event);");
	
	//resetAllButtonName('More &rsaquo;');
	//SelectFilter(gLastHomeFilter);
	hideAllBoxes();

	clearBox('div_featured');
	clearBox('div_latest');
	clearBox('div_mostdled');
	clearBox('div_tutorials');
	clearBox('div_thumbsup');
	clearBox('div_author');
	clearBox('div_category');
	
	//showAjax('latest');
	//showAjax('thumbsup');
	//showAjax('mostdled');
	//showAjax('tutorials');
	
	//use a timer to show the fetch
	setTimeout(function() {
		template = document.getElementById("template");
	
		featured_master._start = 0;
		featured_master._count = 8;
		featured_master._retain = false;
		
		latest_master._start = 0;
		latest_master._count = 8;
		latest_master._retain = false;
		
		thumbsup_master._start = 0;
		thumbsup_master._count = 8;
		thumbsup_master._orderby = 'votes';
		thumbsup_master._retain = false;
		
		mostdled_master._start = 0;
		mostdled_master._count = 8;
		mostdled_master._orderby = 'pageview';
		mostdled_master._retain = false;
		
		tutorials_master._start = 0;
		tutorials_master._count = 8;
		tutorials_master._attrb = 'tutorial';
		tutorials_master._retain = false;
		
		try
		{
			featured_master._renderObjs(featured_book);
			latest_master._renderObjs(latest_book);
			thumbsup_master._renderObjs(thumbsup_book);
			mostdled_master._renderObjs(mostdled_book);
			tutorials_master._renderObjs(tutorials_book);

		} catch (err) {
			if (err == "Error: Reload")
			{
				window.location.href = "home.html";
				return;
			}
		}
		
		setTimeout(showEndLinks, 1000); }, 5 );
}

function hideEndLinks()
{
	var el = document.getElementById('endlinks');
	if (el) el.style.display = 'none';
}

function showEndLinks()
{
	document.getElementById('endlinks').style.display = 'block';
	document.getElementById('newb').setAttribute('hilite', true);
}

function WatchVideo(m)
{
	var sp = document.getElementById('side_panel2');
	var fr = document.getElementById('how2share');
	if (m.innerHTML == 'Close')
	{
		//sp.style.left = '790px';
		sp.style.marginLeft = '2px';
		sp.style.width = '145px';
		fr.style.display = 'none';
		m.innerHTML = 'Watch a video';
	}
	else
	{
		//sp.style.left = '480px';
		sp.style.marginLeft = '-310px';
		sp.style.width = '455px';
		fr.style.display = 'block';
		m.innerHTML = 'Close';
	}
}

function fadePanel(p, e)
{
	var el = document.getElementById(p);
	if (el && el.style.display != 'none')
	{
		var r = el.getBoundingClientRect();
		if (e.clientX > r.left && e.clientX < r.right &&
			e.clientY > r.top && e.clientY < r.bottom)
			el.setAttribute('bkgd-fade', true);
		else
			el.removeAttribute('bkgd-fade');
	}
}

function mouseFadeManager(e)
{
	fadePanel('featured', e);
	fadePanel('latest', e);
	fadePanel('mostdled', e);
	fadePanel('tutorials', e);
	fadePanel('thumbsup', e);
	fadePanel('author', e);
	fadePanel('category', e);
}

function enterSearch(e)
{
	if (e.keyCode == 13) search(e);
}

function resetButtonName(n,t)
{
	var x = document.getElementById(n);
	var e = x.getElementsByTagName('div');
	e[1].innerHTML = t;
}

function resetAllButtonName(t)
{
	resetButtonName('featured', t);
	resetButtonName('latest', t);
	resetButtonName('mostdled', t);
	resetButtonName('tutorials', t);
	resetButtonName('thumbsup', t);
}

function reloadContent()
{
	gStage = 'home';
	resetAllButtonName('More &rsaquo;');
	
	clearBox('div_featured');
	clearBox('div_latest');
	clearBox('div_mostdled');
	clearBox('div_tutorials');
	clearBox('div_thumbsup');
	clearBox('div_author');
	clearBox('div_category');
	
	showAjax('latest');
	showAjax('thumbsup');
	showAjax('mostdled');
	showAjax('tutorials');
	
	featured_master = new QueryMaster("div_featured");
	latest_master = new QueryMaster("div_latest");
	thumbsup_master = new QueryMaster("div_thumbsup");
	mostdled_master= new QueryMaster("div_mostdled");
	tutorials_master = new QueryMaster("div_tutorials");
	
	featured_master._start = 0;
	featured_master._count = 8;
	featured_master._attrb = 'featured';
	featured_master._retain = true;
	featured_master._makeQuery();
	
	latest_master._start = 0;
	latest_master._count = 8;
	latest_master._retain = true;
	latest_master._makeQuery();
	
	thumbsup_master._start = 0;
	thumbsup_master._count = 8;
	thumbsup_master._orderby = 'votes';
	thumbsup_master._retain = true;
	thumbsup_master._makeQuery();
	
	mostdled_master._start = 0;
	mostdled_master._count = 8;
	mostdled_master._orderby = 'pageview';
	mostdled_master._retain = true;
	mostdled_master._makeQuery();
	
	tutorials_master._start = 0;
	tutorials_master._count = 8;
	tutorials_master._attrb = 'tutorial';
	tutorials_master._retain = true;
	tutorials_master._makeQuery();
	
	setTimeout(showEndLinks, 3000);
}

function reloadBox(n)
{
	gSearch = 0;
	hideAllBoxes();
	
	gStage = n;
	gActiveBox = n;
	clearBox(n);
	
	template = document.getElementById("template");
	
	var master = new QueryMaster(n);
	master._start = 0;
	master._count = CountPerPage * 4;
		
	if (n == 'div_featured')
	{
		master._attrb = 'featured';
	}
	else if (n == 'div_latest')
	{
		// do nothing
	}
	else if (n == 'div_thumbsup')
	{
		master._orderby = 'votes';
	}
	else if (n == 'div_mostdled')
	{
		master._orderby = 'pageview';
	}
	else if (n == 'div_tutorials')
	{
		master._attrb = 'tutorial';
	}
	master._makeQuery();
}

function reloadAuthorPage(nm)
{
	hideEndLinks();
	hideAllBoxes();
	gSearch = 0;
	gStage = 'div_author';
	gActiveBox = 'div_author';
	var at = document.getElementById('author');
	
	document.getElementById('authorName').innerHTML = nm;
	document.getElementById('authorBack').innerHTML = 'Back';
	document.getElementById('authorBack').setAttribute('onclick', "loadHomeCategories(event);");
	
	clearBox(gActiveBox);
		
	showAjax('author');
	
	var master = new QueryMaster(gActiveBox);
	master._start = 0;
	master._count = CountPerPage * 4;
	master._publisher = nm;
	master._makeQuery();

	at.style.display = 'block';
	setTimeout(showEndLinks, 700);
}

function reloadCatPage(cat)
{
	hideEndLinks();
	hideAllBoxes();
	gSearch = 0;
	gActiveBox = 'div_category';
	gStage = 'div_category';
	
	var at = document.getElementById('category');
	
	var cn = document.getElementById('catName');
	var cop = cn.getElementsByTagName('option');
	for (var i=0; i<cop.length; i++)
	{
		if (cop[i].value == cat)
		{
			cop[i].selected = "1";
			break;
		}
	}
	
	clearBox(gActiveBox);
		
	showAjax('category');
	
	var master = new QueryMaster(gActiveBox);
	master._start = 0;
	master._count = CountPerPage * 4;
	master._category = cat;
	master._makeQuery();

	at.style.display = 'block';
		
	setTimeout(showEndLinks, 700);
}

function EditPublicDoc(e)
{
	var ed = document.getElementById('edit-doc');
	if (ed)
	{
		var guid = ed.getAttribute('guid');
		var cxx = getCCRX();
		var data = guid + '\t' + gAppTitle;
		cxx.CCRxSetWindowProperty(window, 'edit_public_doc', data);
	}
}

function EditPublicComment(e)
{
	var ed = document.getElementById('edit-comment');
	if (ed)
	{
		var guid = ed.getAttribute('guid');
		var cxx = getCCRX();
		var data = guid + '\t' + gAppTitle;
		cxx.CCRxSetWindowProperty(window, 'edit_public_comment', data);
	}
}


function ScrollController(type) 
{
	this._type = type;
	this._top = document.getElementById('div_'+this._type);
	this._prepare();
}

ScrollController.prototype = {
  _type: 'default',
  _top: null,
  _x: 0,
  _y: 0,
  _start: 0,
  _end: 0,
  _rate: 0,
  _ts: 0,
  _range: 0,
  _timeout: 15,
  _timer: null,
  _down: false,
  _degradation: 0.95,
  _lastEpoch: 0,
  
  _prepare: function SC_Prepare() {
  	this._top.addEventListener('mousedown', this._dragStart.bind(this), false);
  	this._top.addEventListener('mousemove', this._drag.bind(this), false);
  	this._top.addEventListener('mouseup', this._dragEnd.bind(this), false);
  	this._top.addEventListener('mouseout', this._dragOut.bind(this), false);
  	if (this._top.childNodes.length > 0)
  	{
  		var lc = document.getElementById('left-cell');
  		var rc = document.getElementById('right-cell');
  		var minw = this._top.getBoundingClientRect().width - PANEL_WIDTH;
  		lc.style.minWidth = rc.style.minWidth = minw+'px';
  		this._range = this._top.scrollWidth - this._top.getBoundingClientRect().width;
  		
  		this._top.scrollLeft = minw + 10;
  	}
  },

  _dragStart: function SC_DragStart(e) {
  	this._x = e.clientX;
  	this._y = e.clientY;
  	this._down = true;
  	this._top.style.cursor = '-moz-grabbing';
  	this._start = this._top.scrollLeft;
  	var foo = new Date; 
	this._ts = foo.getTime();
	if (this._timer)
	{
		clearInterval(this._timer); 
		this._timer = null;
	}
  	e.stopPropagation();
  	e.preventDefault();
  	gScrolling = false;
  },
  
  _drag: function SC_Drag(e) {
  	if (this._down)
  	{
  		var dx = e.clientX - this._x;
  		this._top.scrollLeft -= dx;
  		this._x = e.clientX;
  		
  		if (Math.abs(dx) > 0 && !gScrolling)
  		{
  			gScrolling = true;
  		}
  	}
  },
  
  _dragEnd: function SC_DragEnd(e) {
  	if (this._down)
  	{
  		e.stopPropagation();
  		e.preventDefault();
  		this._end = this._top.scrollLeft;
  		var foo = new Date; 
		var amt = foo.getTime() - this._ts;
		this._rate = parseFloat(this._end - this._start) / parseFloat(amt);
			
  		this._top.style.cursor = '-moz-grab';
  		
  		if (Math.abs(this._rate * this._timeout) > 10)
  		{
  			// flip if no room to scroll
			if (this._top.scrollLeft == this._range && this._rate > 0)
				this._rate = -this._rate;
			
			if (this._top.scrollLeft == 0 && this._rate < 0)
				this._rate = -this._rate;
			
  			// start auto scrolling
  			var foo = new Date; 
			this._lastEpoch = foo.getTime();
  			this._timer = setInterval(this._autoScroll.bind(this), this._timeout);
  		}
  	}
  	this._down = false;
  },
  
  _dragOut: function SC_DragEnd(e) {
  	var cx = e.clientX;
  	var cy = e.clientY;
  	var r = this._top.getBoundingClientRect();
  	if (cx < r.left || cx > r.left + r.width ||
	  	cy < r.top || cy > r.top + r.height)
	{
  		this._dragEnd(e);
  	}
  },
  
  _autoScroll: function SC_AutoScroll() {
    var foo = new Date; 
    var span = foo.getTime() - this._lastEpoch;
    this._lastEpoch = foo.getTime();
  	var amt = this._rate * span;
  	var nsp = this._top.scrollLeft + amt;
  	if (nsp <= 0)
  	{
  		this._top.scrollLeft = Math.abs(nsp);
  		this._rate = -this._rate * this._degradation;
  	}
  	else if (nsp >= this._range)
  	{
  		this._top.scrollLeft = 2*this._range - nsp;
  		this._rate = -this._rate * this._degradation;
  	}
  	else
  	{
  		this._top.scrollLeft = nsp;
  		this._rate = this._rate * this._degradation;
  	}
  	
  	if (Math.abs(this._rate * this._timeout) <= 1.0)
  	{
  		clearInterval(this._timer);
  		this._timer = null;
  		gScrolling = false;
  	}
  },
  
};

function prepareSpectroscopy()
{
	var anc = document.getElementById('spectroscopy');
	if (!anc) return;
	var pt = document.getElementById('profile-table');
	var sp = document.getElementById('spectroscopy-separator');
	var lc = document.getElementById('js-line-count');
	var dp = document.getElementById('droplet-profile');
	var ct = pt.getAttribute('content');
	if (ct.length == 0)
	{
		pt.style.display = 'none';
		sp.style.display = 'block';
	}
	else
	{
		pt.style.display = 'block';
		sp.style.display = 'none';
		var d = ct.split(',');
		var maxCount = 0;
		var fArray = new Array;
		for (var i=0; i<d.length; i++)
		{
			if (d[i].length > 0)
			{
				var a = d[i].split(' ');
				if (a.length == 2)
				{
					if (parseInt(a[0]) == -1)
					{
						lc.innerHTML = a[1];
					}
					else if (parseInt(a[0]) == 0)
					{
						dp.innerHTML = a[1];
					}
					else
					{
						if (maxCount < parseInt(a[1]))
							maxCount = parseInt(a[1]);
							
						fArray.push(d[i]);
					}
				}
			}
		}
		
		// use log base 2 to plot
		var g = Math.log(maxCount) / Math.log(2);
		var wi = Math.floor(750./(fArray.length*2.));
		
		// get td's position
		var rt = findPos(anc);
		var ttop = rt[1]+4;
		var tleft = rt[0] + 5;
		
		// allow 5px margin, so 100px to work with
		var idx = {value:0};
		var u = 90./g;
		for (var i=0; i<fArray.length; i++)
		{
			var a = fArray[i].split(' ');
			if (a.length == 2)
			{
				setupSpectralLines(anc, ttop, tleft, parseInt(a[0]), parseInt(a[1]), u, wi, idx);
				idx.value++;
			}
		}
		
		// use a brief timeout to start transition
		setTimeout(startTransition, 500);	
	}
}

function setupSpectralLines(anc, top, left, t, c, u, wi, idx)
{
	var img = 'http://www.otakhi.com/img/condenserIcons/' + condenserIcons[t] + '_select.png';
	var h = (c==1)? 15 : Math.round((Math.log(c) / Math.log(2)) * u);
	
	var dv = document.createElement('div');
	dv.className = 'spectral-bar';
	dv.style.position = 'absolute';
	dv.style.top = top + (110 - 5) +'px';
	dv.style.left = left + 2 * wi * idx.value + 5 + 'px';
	dv.style.width = wi+'px';
	dv.style.height = '5px';
	dv.style.marginRight = (wi-1)+'px';
	dv.setAttribute('ftop', h +'px');
	dv.setAttribute('mtop', top + (110-h) +'px');
	document.body.appendChild(dv);
	
	gSpectralLines.push(dv);

	// add an image
	var m = document.createElement('img');
	m.setAttribute('src', img);
	m.setAttribute('title', condenserIcons[t] + ' molecule');
	m.style.position = 'absolute';
	m.style.top = top + (90 - 5) +'px';
	m.style.left = left + 2 * wi * idx.value + wi + 'px';
	m.style.width = '15px';
	m.style.height = '15px';
	m.style.cursor = 'pointer';
	m.setAttribute('ftop', top + (90-h) +'px');
	document.body.appendChild(m);
	
	gSpectralImgs.push(m);
	
	// add count
	var nd = c.toString().length;
	var offset = (nd == 1)? 9 : ( (nd == 2)? 14 : 20 );
	var v = document.createElement('div');
	v.style.position = 'absolute';
	v.style.top = top + (90-h) +'px';
	v.style.left = left - offset + 2 * wi * idx.value + wi + 'px';
	v.style.width = '15px';
	v.style.height = '15px';
	v.style.fontSize = '10px';
	v.innerHTML = c;
	v.style.display = 'none';
	document.body.appendChild(v);
	
	gSpectralCounts.push(v);
}
	
function startTransition()
{
	for (var n=0; n<gSpectralLines.length; n++)
	{
		var dv = gSpectralLines[n];
		var m = gSpectralImgs[n];
		var v = gSpectralCounts[n];
		
		dv.style.top = dv.getAttribute('mtop');
		dv.style.height = dv.getAttribute('ftop');
		dv.style.MozTransition = 'all ' + gTransitionDuration + 's ease-in 0s';
		dv.style.WebkitTransition = 'all ' + gTransitionDuration + 's ease-in 0s';
		
		m.style.top = m.getAttribute('ftop');
		m.style.MozTransition = 'top ' + gTransitionDuration + 's ease-in-out';
		m.style.WebkitTransition = 'top ' + gTransitionDuration + 's ease-in-out';
	}
	
	setTimeout(updateTransition, gTransitionDuration * 1000 + 300);
}

function updateTransition()
{
	for (var n=0; n<gSpectralCounts.length; n++)
	{
		var v = gSpectralCounts[n];
		v.style.display = 'block';
	}
}

function findPos(obj) 
{
	var curleft = curtop = 0;
	
	if (obj.offsetParent) {
		do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	
	return [curleft,curtop];
}

