
Array.prototype.remove = function(index)
{	
	if (isNaN(index) || index >= this.length)
		return false;
	this.splice(index, 1);
}

Array.prototype.foreach = function(handler)
{	
	for(var i=0; i<this.length; i++)
	{
		var obj = this[i];
		if (handler){ if ( handler(obj, i, this) ) { break; } }
	}
}

Array.prototype.peek = function()
{	
	if (this.length == 0){ return null;	}
	return this[this.length-1];
}

Array.prototype.insert = function(index, obj)
{	
	if (index > this.length) { return; }
	
	this.length++;
	for(var i=this.length-1; i>index; i--)
	{
		this[i] = this[i-1];
	}
	this[index] = obj;
}

Array.prototype.move = function move(s, t)
{
	try
	{
		if (s > this.length-1 || t > this.length-1) { return; }
		if (s > t)
		{
			var e = null;
			for(var i=s; i>t; i--)
			{
				if(s == i ) { e = this[i]; }
				if(i == t ) { this[i] = e; } else { this[i] = this[i-1]; }
			}
		}
		else
		{
			var e = null;
			for(var i=s; i<=t; i++)
			{
				if(s == i ) { e = this[i]; }
				if(i == t ) { this[i] = e; } else { this[i] = this[i+1]; }
			}
		}
	}
	catch (e){}
}

Date.prototype.addDays = function(v)
{
	var d = this;
	d.setTime (d.getTime() + v * (24 * 60 * 60 * 1000));
	return d;
}

String.prototype.format = function()
{
	var number;
	var template = this;
	for (var i = 0; i < arguments.length; i++) 
	{
		number = "\{(" + i + ")\}";
		var reg = new RegExp(number, "ig");
		template = template.replace(reg, arguments[i]);
	}
   
	return template;
}

String.prototype.trim = function()
{
	var s = this;
	var reg = /(^\s*)|(\s*$)/g;  
	return s.replace(reg, "");   
}

String.prototype.replaceAll = function(s,t)
{
	try
	{
		var self = this;
		var c = "(" + s + ")";
		var reg = new RegExp(c, "ig");
		return self.replace(reg, t);
	}
	catch(e){}
}

function System(){}
System.prototype.createElement = function(tagName, objID, hFn)
{
	var obj	= document.getElementById(objID);
	if (obj == null)
	{
		obj = document.createElement(tagName);
		obj.id = objID;
	}
	if (hFn) { hFn(obj); }
	
	return obj;
}

System.prototype.createDIV = function(objID, hFn)
{
	return this.createElement("div", objID, hFn);
}

System.prototype.getClientHeight = function()
{
	var h = 0;
	if(document.documentElement && document.documentElement.clientHeight>0){
		h = document.documentElement.clientHeight;
	}else{
		h = document.body.clientHeight;
	}
	return h;
}

System.prototype.getScrollTop = function()
{
	var scrollPos; 
	if (typeof window.pageYOffset != 'undefined') { 
	   scrollPos = window.pageYOffset; 
	} 
	else if (typeof document.compatMode != 'undefined' && 
		 document.compatMode != 'BackCompat') { 
	   scrollPos = document.documentElement.scrollTop; 
	} 
	else if (typeof document.body != 'undefined') { 
	   scrollPos = document.body.scrollTop; 
	} 
	return scrollPos;
}

//设置cookie值
//参数[cookie名,cookie值,有效期,有效域]
//var expdate = new Date();//设置有效期方法
//expdate.setTime (expdate.getTime() + 1 * (24 * 60 * 60 * 1000)); //+1Day
System.prototype.setCookie = function(name, value)
{
	var argv = this.setCookie.arguments;
	var argc = this.setCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var domain = (argc > 3) ? argv[3] : null;
	var path = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + encodeURI(value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((secure == true) ? "; secure" : "");
}

//设置cookie键值
//参数[cookie名,cookie子键名,cookie子键值,有效期,有效域]
System.prototype.setCookies = function(name, key, value)
{
	var argv = this.setCookies.arguments;
	var argc = this.setCookies.arguments.length;
	var expires = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var path = (argc > 5) ? argv[5] : null;
	var secure = (argc > 6) ? argv[6] : false;
	var becookie = this.getCookie(name);
	if(becookie == "")
		value = key + "=" + encodeURI(value);
	else{
		var re = new RegExp("(" + key + "=)[^&]*", "gi");
		if(re.test(becookie))
			value = becookie.replace(re, "$1" + encodeURI(value));
		else
			value = becookie + "&" + key + "=" + encodeURI(value);
	}
	document.cookie = name + "=" + value +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((secure == true) ? "; secure" : "");
}

//获取cookie值
//参数[cookie名,cookie子键名]
System.prototype.getCookie = function(name, key)
{
	var arg = name + "=";
	var i = document.cookie.indexOf(arg);
	if(i==-1) return '';
	var str = document.cookie.substring(i+arg.length);
	var j=str.indexOf(";");
	if(j>-1) str=str.substring(0,j);
	if(key&&key.length>0){
		arg=key+"=";
		i=str.indexOf(arg);
		if(i==-1)return '';
		str=str.substring(i+arg.length);
		j=str.indexOf("&");
		if(j>-1)str=str.substring(0,j);
	}
	return decodeURI(str);
}

var system = new System();


function DragBar(){}
//memer
DragBar.prototype.m_mouseOldX = 0;

//属性 *
//是否有效
DragBar.prototype.Disabled	= false;

//拖动事件 *
DragBar.prototype.OnDrag	= null;
DragBar.prototype.OnOut		= null;

DragBar.prototype.On_Stop	= null;
DragBar.prototype.On_Create	= null;
DragBar.prototype.On_ChangePos	= null;

//HTML控件 *
DragBar.prototype.Pointer	= null;
DragBar.prototype.Bar		= null;

DragBar.prototype.m_basePoint = 0;
DragBar.prototype.m_mouseState = 0;

DragBar.prototype.getBarWidth = function()
{		
	return this.Bar.Width - this.Pointer.Width;
}

DragBar.prototype.create = function(pointer, bar)
{
	this.Pointer	= pointer;
	this.Bar		= bar;
	
	var self = this;
	$(self.Pointer).bind("mousedown", function(e){ self.startDraw(e) })
	$(self.Pointer).css("position", "absolute");
	this.OnCreate();
}

//调整初始位
DragBar.prototype.adjustPos = function(n)
{
	var self = this;
	$(self.Pointer).css("left", n+"px");
}

//设置鼠标左键所按住的按钮和进度条
DragBar.prototype.startDraw = function(e)
{
	if (this.Disabled == false)	{ return false;	}
	this.m_mouseState = 1;
	
	if ($.browser.msie)
	{
		this.Pointer.setCapture();
	}
	else
	{
		e.stopPropagation();
	}
	
	var self = this;	
	$(document).bind("mousemove", function(e){ self.move(e); });			//鼠标移动时实现图标拖动
	$(document).bind("mouseup", function(e){ self.resetPointer(e) });	//鼠标移动时实现图标拖动
	this.m_mouseOldX = e.clientX;
}

DragBar.prototype.OnChangePos = function(pos)
{
	if (this.On_ChangePos)	{ this.On_ChangePos(pos) }
}

DragBar.prototype.OnCreate = function()
{
	if (this.On_Create)	{ this.On_Create(this) }
}

DragBar.prototype.OnDrag = function()
{
	if (this.On_Drag != null) { this.On_Drag(this); }
}

DragBar.prototype.OnOut = function()
{
	if (this.On_Out != null) { this.On_Out(this); }
}

//拖动进度条
DragBar.prototype.move = function(e)
{		
	var base		= this.m_basePoint;
	var barWidth	= this.getBarWidth();
	
	//指示点和条没NULL,或者位置不在指定范围,返回
	if((this.Pointer == null && this.Bar == null)) { return; }
	
	//更新指示器位置
	this.adjustPos(parseInt(this.Pointer.style.left) + e.clientX-parseInt(this.m_mouseOldX));
	this.m_mouseOldX = e.clientX;
	
	//限制进度条的范围
	if( parseInt(this.Pointer.style.left) > base + barWidth ) { this.adjustPos(base + barWidth); }
	if(parseInt(this.Pointer.style.left) < base) { this.adjustPos(base); }
	this.OnDrag();
}


//取消设置鼠标左键所按住的按钮
DragBar.prototype.resetPointer = function()
{	
	this.OnOut(this);
	if (this.m_mouseState == 0) return;	
	
	if ($.browser.msie)
		this.Pointer.releaseCapture();
	else
	{
	}
	
	$(document).unbind("mousemove");			//鼠标移动时实现图标拖动
	$(document).unbind("mouseup");	//鼠标移动时实现图标拖动
	this.m_mouseState = 0;
}


//播放
function Play()
{
	try
	{
		var o = GetMedia(); if (!o) return; o.Play();
	}catch(e){}
}

//暂停
function Pause()
{
	var o = GetMedia(); if (!o){ return; }  o.Pause();
}

//停止
function Stop()
{
	var o = GetMedia(); if (!o) return; o.Stop();
}

//静音
function SetMute()
{
	var o = GetMedia(); if (!o) return; o.IsMuted = (!o.IsMuted);
}

//静音状态
function GetMute()
{
	var o = GetMedia(); if (!o) { return false;} else { return o.IsMuted;}
}

//设置数据源
function SetSource(url)
{
	var o = GetMedia(); if (!o) { return; } o.Source = url;
}

//设置音量
function SetVolume(v)
{
	var o = GetMedia(); if (!o) return; o.Volume = v;
}

//获取音量
function GetVolume()
{
	var o = GetMedia(); if (!o) return 0; return o.Volume;
}

//设置播放位置
function SetMediaPosition(v)
{
	var o = GetMedia(); if (!o) return; o.Position = SecTotime(v);
}

//获取播放位置
function GetMediaPosition()
{
	var o = GetMedia(); if (!o) return 0; return o.Position.Seconds;
}

//秒转换成标准时间格式
function SecTotime(sec)
{
	sec = parseInt(sec);
	var minu = parseInt(sec/60);
	var hour = parseInt(sec/(60*60));
	var sec = parseInt(sec - minu*60);
	if (minu < 10) minu = "0" + minu;
	if (sec < 10) sec = "0" + sec;
	if (hour < 10) hour = "0" + hour;
	return hour + ":" + minu + ":" + sec;	
}

function $E(id)
{
	return document.getElementById(id);
}

//获取用户的唯一标识
function getUserID(){
	var passid=system.getCookie("9Sky_PassID");
	var arr=passid.split("\r");
	if(passid.length>0 && !/[^\d]/.test(arr[0])) return arr[0];
	return 0;
}
//获取用户当前或上次登录帐号
function getUserName(){
	var passid = system.getCookie("9Sky_PassID");
	var arr=passid.split("\r");
	if(arr.length>1) 
	{
		return decodeURI(arr[1].replace("%40", "@"));
	}
	return "";
}

//获取登录状态
function getLoginState()
{
	var v = system.getCookie("9Sky_Passporta");
	if (v == "" || v == null) { v = system.getCookie("9Sky_Passportb"); }
	if (v == "" || v == null) { return false; } else { return true; }
}


function AjaxScript(url, varName, params, onsuccess)
{	
	var arr = document.getElementsByTagName("script");
	var url =url.toLowerCase();
	var isLoad = true;
	if (arr)
	{
		for(i=0; i<arr.length; i++)
		{
			var src = arr[i].src;
			if (src) 
			{
				if (src.toLowerCase() == url) { isLoad = false; }
			}
		}
	}
	
	if (isLoad)
	{
		var s = document.createElement("script");
		var params = (params ? params : "") + (params && varName ? "&" : "") + (varName ? "varName=" + varName : "")
		if (url.indexOf("?") >= 0)
		{
			url += (params && params != "" ? "&" + params  : "");
		}
		else
		{
			url += (params && params != "" ? "?" + params : "");
		}
		
		s.src = url;
		s.type = "text/javascript";
		s.onreadystatechange = function()
		{	
			switch(this.readyState)
			{
				case "complete":
				case "loaded":
					var cmd = "try {	if (onsuccess) { onsuccess({0});  {1} }	}catch(e){}".format((varName != null ?  varName : ""), (varName != null ? "delete {0};".format(varName) : "") )
					eval(cmd);
					break;
					
				case "loading":
					break;
			}
		}
		s.onload = function() { 
					var cmd = "try {	if (onsuccess) { onsuccess({0});  {1} }	}catch(e){}".format((varName != null ?  varName : ""), (varName != null ? "delete {0};".format(varName) : "") )
					eval(cmd);
				}
		var arr = document.getElementsByTagName("head");
		var h= arr[0];
		document.body.appendChild(s);
	}
	else
	{
		eval("if (onsuccess) { onsuccess({0}); }".format( varName != null ?  varName : "" ));
	}
}

function LoadCSS(url)
{
	var arr = document.getElementsByTagName("link");
	var isLoad = true;
	
	if (arr)
	{
		for(i=0; i<arr.length; i++)
		{
			var type = arr[i].type;
			if (type && type.toLowerCase() == "text/css")
			{
				var href = arr[i].href;
				if (href) 
				{
					if (href.toLowerCase() == url.toLowerCase()) { isLoad = false; }
				}
			}
		}
	}
	if (isLoad)
	{
		var s = document.createElement("link");
		s.href = url;
		s.type = "text/css";
		s.rel = "stylesheet";
		document.body.appendChild(s);
	}
}


