
/////////////////////////////////////////////////////////////////////
////                                                             ////
////                       Common                                ////
////    CreateName: Tree                                         ////
////    Email:      yuhai717@163.com                             ////
////    Version:    1.0                                          ////
////    CreateDate: 2008-2-18                                    ////
////    EditDate:   2008-8-5                                     ////
////                                                             ////
/////////////////////////////////////////////////////////////////////

var Tree = {
	Version : '1.0.0.0',
	
	Browser: {
    IE:     !!(window.attachEvent && !window.opera),
    Opera:  !!window.opera,
    WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
    Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
    MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
  },

  BrowserFeatures: {
    XPath: !!document.evaluate,
    ElementExtensions: !!window.HTMLElement,
    SpecificElementExtensions:
      document.createElement('div').__proto__ &&
      document.createElement('div').__proto__ !==
        document.createElement('form').__proto__
  }
};

//Extend Objcect
Object.extend = function(destination, source) {
  for (var property in source)
    destination[property] = source[property];
  return destination;
};

Object.extend(Object,{
	isString : function(obj){
		return typeof obj == 'string';
	},	
	isInt : function(obj){
		return typeof obj == 'number';
	},
	isFunction : function(obj){
		return typeof obj == 'function';
	},
	isElement : function(obj){
		return obj && obj.nodeType == 1;
	},
	isUndefined: function(obj) {
    return typeof obj == 'undefined';
  }
});

Object.extend(Tree,{
	docHeight : function(){
	    var _cHeight = document.documentElement.clientHeight;
	    var _sHeight = document.documentElement.scrollHeight;
	    
	    if (_cHeight > _sHeight)
	        return _cHeight;
	    else
	        return _sHeight;
	},
	docWidth : function(){
	    var _cWidth = document.documentElement.clientWidth;
	    var _sWidth = document.documentElement.scrollWidth;
	     
	    if (_cWidth > _sWidth)
	        return _cWidth;
	    else
	        return _sWidth;
	},
	ScrollLeft : function(){
	    return (document.body.scrollLeft == 0 ? document.documentElement.scrollLeft : document.body.scrollLeft);
	},
	ScrollTop : function(){
	    return (document.body.scrollTop == 0 ? document.documentElement.scrollTop : document.body.scrollTop);
	},
	//add Event
	addEvent: function(oTarget,eventType,fnEvent){
	    if(oTarget.addEventListener)
	        oTarget.addEventListener(eventType,fnEvent,false);
	    else if (oTarget.attachEvent)
	        oTarget.attachEvent('on' + eventType,fnEvent);
	    else
	        oTarget['on' + eventType] = fnEvent;
	},
	//Remove Event
	removeEvent : function(oTarget,eventType,fnEvent){
	    if(oTarget.removeEventListener)
	        oTarget.removeEventListener(eventType,fnEvent,false);
	    else if (oTarget.detachEvent)
	        oTarget.detachEvent('on' + eventType,fnEvent);
	    else
	        oTarget['on' + eventType] = null;
	},
	$ : function(_sID){
	    return document.getElementById(_sID);
	},
	ClientHeight : function(){
	    return document.documentElement.clientHeight;
	},
	ClientWidth : function(){
	    return document.documentElement.clientWidth;
	},
	getEvent : function(){
        if(window.event){
            return this.formatEvent(window.event);
        } else {
            return Tree.getEvent.caller.arguments[0];
        }
    },
    formatEvent : function(e){            
        if(this.Browser.IE)
        {                
            e.charCode = (e.type == 'keypress' ? e.keyCode : 0);
            e.eventPhase = 2;
            e.isChar = (e.charCode > 0);
            e.pageX = e.clientX + Tree.ScrollLeft();
            e.pageY = e.clientY + Tree.ScrollTop();
            e.preventDefault = function(){
                this.returnValue = false;
            };
            if(e.type == 'mouseout'){
                e.relatedTarget = e.toElement;
            } else if (e.type == 'mouseover') {
                e.relatedTarget = e.fromElement;
            }
            e.stopPropagation = function(){
                this.cancelBubble = true;
            };
            e.target = e.srcElement;
            e.time = (new Date()).getTime();
        }
        return e;
    }
});


function AddFavorite()
{
    var surl = 'http://www.joy.cn/';
    var stitle = '激动网视频联盟';
   try
   {
       window.external.addFavorite(surl, stitle);
   }
   catch (e)
   {
       try
       {
           window.sidebar.addPanel(stitle, surl, "");
       }
       catch (e)
       {
           alert("加入收藏失败，请使用Ctrl+D进行添加");
       }
   }
}


function setHomepage()
{
    if (document.all)
    {
        document.body.style.behavior='url(#default#homepage)';
        document.body.setHomePage('http://www.joy.cn/');
    } else if (window.sidebar) {
        if(window.netscape)
        {
            try
            {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e)
            {
                alert("Firefox暂无此功能，请手动设置。" );
            }
        }
    var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
    prefs.setCharPref('browser.startup.homepage','http://www.joy.cn/');
    }
}

function copyinfo(inputid)
{
    var copyvalue = $('#'+inputid).val();
    if (window.clipboardData)
	{
		window.clipboardData.setData('text',copyvalue);
		alert('复制成功,请按[Ctrl+V]粘贴到您想要的地方');
	}
	else
		alert('抱歉，在你的游览器里不能自动复制到剪贴板～');
}

function spantolabel(id)
{
    var sp = $('#'+id+' span');
        
    var val = sp.html();
    var style = sp.attr('style');
    
    var sppre = sp.prev();
    
    if(sppre.length == 0)
    {
        sppre = sp.next();
        sp.remove();
        sppre.before('<label style="'+style+'">'+val+'</label>');
    }
    else
    {
        sp.remove();        
        sppre.after('<label style="'+style+'">'+val+'</label>');
    }
}
