if(typeof(Sonata)=='undefined') Sonata = {};
Sonata.JSROOT = "/";
Sonata.JSPLATFORM = "/sonata.lib/";

Sonata.isCSS=false;
Sonata.isW3C=false;
Sonata.isIE4=false;
Sonata.isNN4=false;
Sonata.isIE6CSS=false;

Sonata.isOpera = navigator.userAgent.indexOf("Opera") > -1;
if(Sonata.isOpera) Sonata.isIE4=false;
Sonata.isIE55 = navigator.userAgent.indexOf("MSIE 5.5;")> -1;
Sonata.isIE60 = navigator.userAgent.indexOf("MSIE 6.0;")> -1;
Sonata.isIE70 = navigator.userAgent.indexOf("MSIE 7.0;")> -1;
Sonata.isFirefox = navigator.userAgent.indexOf("Firefox")> -1;
Sonata.isGecko = navigator.userAgent.indexOf("Gecko")> -1;
Sonata.isMac = navigator.userAgent.indexOf("Macintosh") > -1;
Sonata.isSafari = navigator.userAgent.indexOf("Safari") > -1;
if(navigator.userAgent.indexOf("AppleWebKit")> -1)
	Sonata.isSafari = true;
if(Sonata.isMac || Sonata.isSafari) Sonata.isGecko=false;
if(Sonata.isIE55 || Sonata.isIE60) Sonata.isIE4 = true;
Sonata.isIE = false;
if(Sonata.isIE4 || Sonata.isIE55 || Sonata.isIE60 || Sonata.isIE70)
	Sonata.isIE = true;
if(Sonata.isOpera) Sonata.isIE = false;
Sonata.init = function()
{
	var ua = navigator.userAgent;
    if (document.images)
	{
        Sonata.isCSS = (document.body && document.body.style) ? true : false;
        Sonata.isW3C = (Sonata.isCSS && document.getElementById) ? true : false;
        Sonata.isIE4 = (Sonata.isCSS && document.all) ? true : false;
        Sonata.isNN4 = (document.layers) ? true : false;
        Sonata.isIE6CSS =
		(document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;

		Sonata.isOpera = ua.indexOf("Opera") > -1;
		if(Sonata.isOpera) Sonata.isIE4=false;
		Sonata.isIE55 = ua.indexOf("MSIE 5.5;")> -1;
 		Sonata.isIE60 = ua.indexOf("MSIE 6.0;")> -1;
 		Sonata.isIE70 = ua.indexOf("MSIE 7.0") > -1;
   }
}
$(document).ready(Sonata.init);

Sonata.eval = function(text)
{
	var v = '(' + text + ')';
	//mprint("eval v=" + v);
	return eval(v);
}

Sonata.defArg = function(arg,def) {
	var ret = typeof arg == 'undefined'?def:arg;
	return ret;
}

//js include, css include
//********************************************************
//modified by jeff grimm 2005
//from http://www.phpied.com/javascript-include/
//********************************************************
if(typeof Sonata == 'undefined')
	Sonata = {};
	
Sonata.include_lib = function(file)
{
	Sonata.include(Sonata.JSPLATFORM + file);
}
Sonata.include_root = function(file)
{
	Sonata.include(Sonata.JSROOT + file);
}

Sonata.include = function(script_filename)
{
	//mprint("include " + script_filename);
	Sonata.include.include_once(script_filename);
}
include = Sonata.include;
include_lib = Sonata.include_lib;
include_root = Sonata.include_root;

Sonata.include.included_files = new Array();
Sonata.include._include_string = "";

Sonata.include.include_once = function(script_filename)
{
	if (!in_array(script_filename, Sonata.include.included_files))
	{
		Sonata.include.included_files[Sonata.include.included_files.length] = script_filename;
		Sonata.include.include_dom(script_filename);
	}
	
	function in_array(name,arr)
	{
	    var i,n = arr.length;
	    for(i=0;i<n;i++)
	    {
	        if(name == arr[i]) return true;
	    }
	    return false;
	}
}
Sonata.include._include_write = function(script_filename)
{
    document.write('<' + 'script');
    document.write(' language="javascript"');
    document.write(' type="text/javascript"');
    document.write(' src="' + script_filename + '">');
    document.write('</' + 'script' + '>');
}
Sonata.include._include_write_css = function(script_filename)
{
    document.write('<' + 'link');
    document.write(' rel="stylesheet"');
    document.write(' type="text/css"');
    document.write(' href="' + script_filename + '">');
    document.write('</' + 'link' + '>');
}
Sonata.include.include_dom = function(script_filename)
{
	var html_doc = document.getElementsByTagName('head').item(0);
	var t = script_filename.substring(script_filename.lastIndexOf('.')+1);
	var file = null;
    var sel = 2;
    if(Sonata.isIE55 || Sonata.isIE) sel=2;
	if(Sonata.isFirefox || Sonata.isGecko || Sonata.isOpera) sel=1;
	if (t =='js')
	{
		if(sel==2)
		{
		    Sonata.include._include_write(script_filename);
			Sonata.include._include_string += "WRITE:" + script_filename + "\n";
		}
		else
		{
			file = document.createElement('script');
			file.setAttribute('language','javascript');
			file.setAttribute('type','text/javascript');
			file.setAttribute('src',script_filename);
			Sonata.include._include_string += "DOM:" + script_filename + "\n";
			html_doc.appendChild(file);
		}
	}
	else if (t=='css')
	{
		if(sel==2)
		{
		    Sonata.include._include_write_css(script_filename);
			Sonata.include._include_string += "WRITE:" + script_filename + "\n";
		}
		else
		{
			file = document.createElement('link');
			file.setAttribute('rel','stylesheet');
			file.setAttribute('type','text/css');
			file.setAttribute('href',script_filename);
			html_doc.appendChild(file);
		}
	}
	return false;
}
document._uniqueIdCnt = 1149;
document.uniqueId = function(base,cnt)
{
    // could use Math.random and Math.floor to generate random text strings
    //like base + Math.floor(Math.random() * 1000000)
    function getnext()
    {
        return document._uniqueIdCnt++;
        //return "_" + Math.floor(Math.random() * 1000000);
    }

	if(typeof cnt == "undefined")
		cnt = 1;
	if(typeof base == "undefined")
		base = "u";
	var id;
	if(cnt==1)
	{
		id = getnext();
		return base + id;
	}
	var i;
	var ret = new Array(cnt);
	for(i=0;i<cnt;i++)
	{
		id = getnext();
		ret[i] = base + id;
	}
	return ret;
}
Sonata.debugToMain = function(msg,doNewline)
{
	if(typeof msg == "undefined") msg ="";
	doNewline = typeof doNewline == 'undefined'?true:doNewline;
    if (!Sonata.debugToMain.box)
    {
        createBox();
    }
    prt();
	function prt()
	{
	    //this converts \n to <br>
	    //all other HTML will be displayed
		msg = cnv(msg);//msg.replace(/\n/g,"<br>");
		var div;
		if(doNewline) div = document.createElement('div');
		else div = document.createElement('span');
		div.innerHTML = msg;
		Sonata.debugToMain.box.appendChild(div);
		return;
	}
	function cnv(str)
	{
		str = str.replace(/\n/g,"<br>");
		str = str.replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;");
		//str = str.replace(/\s/g,"&nbsp;");
		//mprint("msg=" + msg);
		return str;
	}

	function createBox()
	{
	    // Create a new <div> element
	    Sonata.debugToMain.box = document.createElement("div");
	    Sonata.debugToMain.box.style.backgroundColor = "white";
	    Sonata.debugToMain.box.style.fontFamily = "monospace";
	    Sonata.debugToMain.box.style.border = "solid red 3px";
	    Sonata.debugToMain.box.style.padding = "10px";

	    // And append our new <div> element to the end of the document
	    document.body.appendChild(Sonata.debugToMain.box);

	    //now create a header for our debugging area
	    var h1 = document.createElement("h1");
	    h1.style.textAlign = "center";
	    h1.style.backgroundColor = "pink";
	    h1.appendChild(document.createTextNode("Debugging Output"));

	    Sonata.debugToMain.box.appendChild(h1);
	}

}
Sonata.debugToMain.box = null;

mprint_text = function(msg)
{
	//add msg as a text node, usefull for printing html
	mprintstr(""); //make sure debugToMain.box is created!
	var d = document.createTextNode(msg);
	Sonata.debugToMain.box.appendChild(d);
	mprint("");//add the <br>
}
mprint = function(msg)
{
	//var div = document.createElement('div');
	//div.innerHTML = msg;
	//debugToMain.box.appendChild(div);
	Sonata.debugToMain(msg,true);
}

mprintstr = function(msg)
{
	//var div = document.createElement('span');
	//div.innerHTML = msg;
	//debugToMain.box.appendChild(div);
	Sonata.debugToMain(msg,false);
}
Sonata.prObject = function(text,obj) {
	var name;
	mprint(text + " = " + obj);
	for(name in obj) {
		var v = obj[name];
		if(typeof v == 'function')
			v = "function";
		mprint(text+": name="+name+" value="+v);
	}
}

//some jQuery plugins

/*
 * jQuery Timer Plugin
 * http://www.evanbot.com/article/jquery-timer-plugin/23
 *
 * @version      1.0
 * @copyright    2009 Evan Byrne (http://www.evanbot.com)
 */ 

jQuery.timer = function(time,func,callback){
	var a = {timer:setTimeout(func,time),callback:null}
	if(typeof(callback) == 'function'){a.callback = callback;}
	return a;
};

jQuery.clearTimer = function(a){
	clearTimeout(a.timer);
	if(typeof(a.callback) == 'function'){a.callback();};
	return this;
};
Sonata.Timer = function(time,func) {
	this.timer = setTimeout(func,time);
}
Sonata.Timer.prototype.clear = function() {
	clearTimeout(this.timer);
}
Sonata.Timer.setTimer = function(func,time) {
	var t= new Sonata.Timer(time,func);
	return t;
}	
GenLib = {};
GenLib.init = function() {
}
if(typeof Doc == 'undefined')
	Doc = {};
Doc.POS_LEFT = 1;
Doc.POS_TOP = 2;
Doc.POS_RIGHT = 3;
Doc.POS_BOTTOM = 4;
Doc.POS_BOTTOMRIGHT = 5;
Doc.POS_MIDDLERIGHT = 6;

document.rect = function(left,top,width,height)
{
	if(typeof left=="undefined") left = 0;
	if(typeof top=="undefined") top = 0;
	if(typeof width=="undefined") width = 0;
	if(typeof height=="undefined") height = 0;
	var r = {left:left,top:top,width:width,height:height,
			right:left+width,bottom:top+height,
			toString: asString};
	return r;
	
	//the toString method
	function asString(label)
	{
	    if(typeof label=="undefined") label="";
	    s = label + " left=" + r.left + " top=" + r.top +
	        " width=" + r.width + " height=" + r.height +
	        " right=" + r.right + " bottom=" + r.bottom;
		return s;
	}
}
document.getPositionElem = function(obj,relative)
{
	var p = $(obj);
	var pos = p.offset();
	var height = p.height();
	var width = p.width();
	return document.rect(pos.left,pos.top,width,height);
}	
Doc.getPositionElem = function(elem) {
	return document.getPositionElem(elem);
}
Doc.setPositionAt = function(elemId,atElemId,pos,addLeft,addTop,relative)
{
    var p = Doc.getPositionAt(elemId,atElemId,pos,addLeft,addTop,relative);
    //mprint("p=" + p.toString());
    Doc.setPosition(elemId,p.left,p.top,relative);
    return p;
}

Doc.uniqueId = function(base,cnt)
{
	return document.uniqueId(base,cnt);
}
Doc.getElementById = function(elemId) {
	var e = $("#"+elemId);
	if(e.size()!=0) return e[0];
	return null;
}
Doc.setPosition = function(elemId,left,top,relative)
{
	var d = Doc.getElementById(elemId);
	if(d)
	{
	    Doc.setPositionElem(d,left,top,relative)
	}
}
Doc.setPositionElem = function(d,left,top,relative)
{
	if(d)
	{
		d.style.left = left + "px";
		d.style.top = top + "px";
		//handle the tricky borders in gecko
		function fixup()
		{
			var drect = Doc.getPosition(elemId,relative);
			if(drect.top != top)
			{
				d.style.top = (top + 2*Math.abs(drect.top-top)) + "px";
			}
			if(drect.left != left)
			{
				d.style.left = (left + 2*Math.abs(drect.left-left)) + "px";
			}
		}
		//mprint("after left="+left+ " top=" + top);
	}
}
Doc.getPositionAt = function(elemId,atElemId,pos,addLeft,addTop,relative)
{
	//debugToMain("show elemId=" + elemId);
	var d = Doc.getElementById(elemId);
	var at = Doc.getElementById(atElemId);
	//mprint("d=" + d + " at=" + at);
	return Doc.getPositionElemAt(d,at,pos,addLeft,addTop,relative);
}

Doc.getPositionElemAt = function(d,at,pos,addLeft,addTop,relative)
{
	var left = 0;
	var top = 0;
	if(d && at)
	{
		if(typeof addLeft == "undefined")
			addLeft = 0;
		if(typeof addTop == "undefined")
			addTop = 0;
		var drect = Doc.getPositionElem(d,relative);
		var atrect = Doc.getPositionElem(at,relative);
		//Doc.prRect("drect",drect);
		//Doc.prRect("atrect",atrect);
		var isie = Sonata.isIE;//isIE4
		var adjBottom=isie? -1 : 1;
		var adjTop=isie? 2 : 3;
		var adjRight=isie? 2:1;//-1 : 1;
		var adjLeft=isie? 2 : 3;
		var movLeft= isie? 0 : 2;
		var movDown= isie? 0 : 2;
		left = atrect.left;
		top = atrect.top;
		if(pos == Doc.POS_BOTTOM)
		{
			top = atrect.bottom + adjBottom; //2;
			left += movLeft;
			//mprint("atrect.top=" + atrect.top + " atrect.height=" + atrect.height);
		}
		else if(pos == Doc.POS_TOP)
		{
			top = atrect.top + adjTop - drect.height;
			left += movLeft;
		}
		else if(pos == Doc.POS_RIGHT)
		{
			left = atrect.right + adjRight;//2;
			top += movDown;
		}
		else if(pos == Doc.POS_LEFT)
		{
			left = atrect.left + adjLeft - drect.width;
			top += movDown;
		}
		else if(pos == Doc.POS_BOTTOMRIGHT)
		{
			left = atrect.right + adjRight;//2;
			top = atrect.bottom;// + adjBottom;
		}
		else if(pos == Doc.POS_MIDDLERIGHT)
		{
			left = atrect.right + adjRight;//2;
			top = atrect.bottom - atrect.height/2;
		}
		left += addLeft;
		top += addTop;
		//mprint("d=" + d + " id=" + d.id);
		//mprint("left=" + left + " top=" + top);
		//d.style.left = left;
		//d.style.top = top;
		//Doc.setPosition(elemId,left,top);
        //mprint("d=" + d + " left=" + left + " top=" + top);
		//d.style.left = left + "px";
		//d.style.top = top + "px";
        //mprint("d=" + d + " left=" + d.style.left + " top=" + d.style.top);

	}
	return {left:left, top:top,
	toString: function()
	{
		return "left=" + left + " top=" + top;
	}
	};
}	
Doc.really_private_counter = 100;
Doc.bringToFront = function(elementToDrag)
{
	var par = elementToDrag.parentNode; //document.body;
	if ( par.childNodes[par.childNodes.length-1] !== elementToDrag )
	{
		// move to bottom of document
		elementToDrag.style.zIndex = Doc.really_private_counter + "";
		Doc.really_private_counter++;
		par.appendChild(elementToDrag);
	}
}