	/*
		Created for: File to hold some of predefined objects
		Created by : Kathirvel.P
		Objects :
			browser : To get browser details(vesrion, co-ordinates, element size)
			dialog  : To create a model / modeless popup layers.
			drag    : To make a element draggable.
	*/
	
	function detect() {
		var agent 	= navigator.userAgent.toLowerCase();
		// detect platform
		this.isMac		= (agent.indexOf('mac') != -1);
		this.isWin		= (agent.indexOf('win') != -1);
		this.isWin2k	= (this.isWin && (
				agent.indexOf('nt 5') != -1));
		this.isWinSP2	= (this.isWin && (
				agent.indexOf('xp') != -1 || 
				agent.indexOf('sv1') != -1));
		this.isOther	= (
				agent.indexOf('unix') != -1 || 
				agent.indexOf('sunos') != -1 || 
				agent.indexOf('bsd') != -1 ||
				agent.indexOf('x11') != -1 || 
				agent.indexOf('linux') != -1);
		
		// detect browser
		this.isSafari	= (agent.indexOf('safari') != -1);
		this.isSafari2 = (this.isSafari && (parseFloat(agent.substring(agent.indexOf("applewebkit/")+"applewebkit/".length,agent.length).substring(0,agent.substring(agent.indexOf("applewebkit/")+"applewebkit/".length,agent.length).indexOf(' '))) >=  300));
		this.isOpera	= (agent.indexOf('opera') != -1);
		this.isNN		= (agent.indexOf('netscape') != -1);
		this.isIE		= (agent.indexOf('msie') != -1);
		this.isFirefox	= (agent.indexOf('firefox') != -1);
		
		// itunes compabibility
		this.isiTunesOK	= this.isMac || this.isWin2k;
		
		this.getClientWidth = function() {
			return(window.innerWidth||
				  (document.documentElement && document.documentElement.clientWidth)||
				  (document.body && document.body.clientWidth)||
				  0);
		}
	}
	browser = new detect();	
	browser.getClientHeight = function() {
		return(window.innerHeight||
			  (document.documentElement && document.documentElement.clientHeight)||
			  (document.body && document.body.clientHeight)||
			  0);
	}
	browser.getPageScrollTop = function() {
		return(document.documentElement && document.documentElement.scrollTop)||
			  (document.body && document.body.scrollTop)||
			  0;
	}
	browser.getPageScrollLeft = function() {
		return(document.documentElement && document.documentElement.scrollLeft)||
			  (document.body && document.body.scrollLeft)||
			  0;
	}
	browser.getPageSize = function() {
		var xScroll,yScroll;
		if(window.innerHeight && window.scrollMaxY)	{
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		}	else if(document.body.scrollHeight > document.body.offsetHeight)	{
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		}	else	{
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth,windowHeight;
		if(self.innerHeight)	{
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if(document.documentElement && document.documentElement.clientHeight) {
			windowWidth  = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if(document.body) {
			windowWidth  = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}
		var pageHeight,pageWidth;
		
		if(yScroll < windowHeight) pageHeight = windowHeight;
		else pageHeight = yScroll;
		
		if(xScroll < windowWidth) pageWidth = windowWidth;
		else pageWidth = xScroll;
		
		scrollleft = browser.getPageScrollLeft();
		scrolltop = browser.getPageScrollTop();
		return { pageWidth:pageWidth, pageHeight:pageHeight, 
				 windowWidth:windowWidth, windowHeight:windowHeight, 
				 scrollLeft:scrollleft, scrollTop:scrolltop};
	}
	//	Popup creater.
	function dialog(o) {
		o = o || {};
		var page = browser.getPageSize();
		var title = o.title || 'Warnning!'; 
		var width = o.width || 400;
		var height = o.height || 300;
		var ismodel = o.ismodel || false;
		var isdrag = o.isdrag || false;
		var onclose = o.onclose || 'dialog.close()';
		var content = o.content || '<span class="loading" >Loading...</span>';
		var left = o.left || ((page.windowWidth/2) - (width/2));
		var top = o.top || ((page.windowHeight/2) - (height/2));
		this.loaded = ( o.content ) ? true : false;
		left = left + page.scrollLeft;
		top  = top + page.scrollTop;
		
		bgid = 'dialog_bg_'+Math.random();
		pop  = 'dialog_pop_'+Math.random();
		popcontainer  = 'dialog_container_'+Math.random();	
		if( ismodel == true ) {	//	Gray background
			this.bg = document.createElement('div');		
			this.bg.id = bgid;							
			this.bg.style.width = page.pageWidth; 
			this.bg.style.height = page.pageHeight; 
			this.bg.className = (browser.IsSafari) ? "popupBackgroundSafari" : "popupBackground";
			document.body.appendChild(this.bg); 
		}
		//	window
		this.win = document.createElement('div');
		this.win.id = pop;
		this.win.style.width = width+'px';		
		this.win.style.height = height+'px';
		this.win.style.left = left +'px';		
		this.win.style.top = top +'px';
		this.win.style.position = "absolute";		
		//	title bar
		this.tit = document.createElement('div');
		this.tit.style.width = width+'px';
		this.tit.id = 'dialog-titlebar';			
		header = '<span>' + title + '</span><div><a href="javascript:'+onclose+';" >X</a></div>'
		this.tit.innerHTML = header;
		this.win.appendChild(this.tit);
		//	Content area		
		this.con = document.createElement('div');
		this.con.id = popcontainer;
		this.con.style.width = width+'px';
		this.con.style.height = height+'px';
		this.con.className = 'dialog-content';
		this.con.innerHTML = content;
		this.win.appendChild(this.con);		
		
		this.show = function() {
			document.body.appendChild(this.win);
			if( isdrag ) {
				this.tit.style.cursor = 'move';
				this.drag = new drag( {src:this.tit, draggable:this.win, identy:this.tit.id} )
			}
		}
		this.sethtml = function(html) {
			this.con.innerHTML = this.con.style.width + '<br>' + this.con.style.height + '<br>' +html;
			this.loaded = true;
		}
		this.gethtml = function() {
			return this.con.innerHTML;
		}		
		this.close = function() {
			if( this.loaded ) {
				if( this.bg ) this.bg.parentNode.removeChild(this.bg);
				if( this.win ) this.win.parentNode.removeChild(this.win);
			}
		}
	}	
	//	Drag
	function drag(o)	{		
		var src = o.src || '';
		var identy = o.identy || '';
		if( !src ) return;
		if( identy == '' ) return;
		var draggable = o.draggable || src;
		
		var draginit = function(e) {
			var htype = '-moz-grabbing';
			if (e == null) { e = window.event; htype = 'move';} 
			var target = e.target != null ? e.target : e.srcElement;
			 target = (target.nodeType == 1 || target.nodeType == 9) ? target : target.parentNode;
			//cursor = target.style.cursor;
			if ( target.className == identy || target.id == identy ) {
				//target.style.cursor = htype;
				target = draggable;
				dragging = true;
				dragXoffset = e.clientX - parseInt(draggable.style.left);
				dragYoffset = e.clientY - parseInt(draggable.style.top);
				src.onmousemove = function(e) {
					if (e == null) { e = window.event } 
					  if (e.button <=1 && dragging ) {
						 draggable.style.left = e.clientX - dragXoffset+'px';
						 draggable.style.top = e.clientY - dragYoffset+'px';
						 return false;
					  }
				}
				src.onmouseup = function() {
					src.onmousemove = null;
					src.onmouseup = null;
					//src.style.cursor = 'move';
					dragging = false;
				}
				return false;
			}
		}
		
		if(browser.isIE) src.attachEvent("onmousedown", draginit);
		else src.addEventListener("mousedown", draginit, false);
	}	
