function gel(a){
	return document.getElementById?document.getElementById(a):null;
}

function gelstn(a){
	return document.getElementsByTagName?document.getElementsByTagName(a):new Array();
}

function gelsn(a){
	return document.getElementsByName?document.getElementsByName(a):new Array();
}

/*
 * Copyright (c) 2008, www.jc001.cn! All rights reserved.
 * Jc.Flash, Jc.PageLoad
 *
 * description : 001 前端js工具集
 * web         : http://www.jc001.cn
 * author      : stcer (ab12cxyh@163.com)
 * version     : 0.1.0 
 */
var Jc = Jc || {};

/**
 * importJs 动态加载JS文件
 */
Jc.loadJs = Jc.loadJs || function(uri, x){
	var IsIE = (navigator.userAgent.indexOf("MSIE") == -1)? false : true;
	var scriptEle = document.getElementsByTagName("head")[0].appendChild(document.createElement("script"));
	scriptEle.type = "text/javascript";
	scriptEle.src = uri;
	if (x){
		if (IsIE) {
			scriptEle.onreadystatechange = x;
		} else {
			scriptEle.onload = x;
		}
	}
}

/**
 * print flash html
 */
Jc.Flash = Jc.Flash || function(src, width, height){
	this.src = src;
	this.version = '7,0,19,0';
	this.vars = {};
	this.oattrs = {
		'classid' : 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000',
		'codebase' : 'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + this.version,
		'width' : width,
		'height' : height
	};
	
	this.eattrs = {
		'pluginspage' : 'http://www.macromedia.com/go/getflashplayer',
		'type' : 'application/x-shockwave-flash',
		'quality' : 'high',
		'src' : src,
		'width' : width,
		'height' : height,
		'wmode' : 'opaque'
	};
	
	this.pattrs = {
		'movie' : src,
		'menu' : 'false',
		'quality' : 'high',
		'wmode' : 'opaque'
	};
	
	this.setVar = function(k, v){
		this.vars[k] = v;
	}; 
	
	this.setParam = function(k, v){
		if(k == 'movie'){
			k = 'src';	
		}
		if(k == 'src'){
			this.eattrs[k] = v;	
			this.eattrs['movie'] = v;
			return;
		}
		
		if(typeof this.oattrs[k] != 'undefined'){
			this.oattrs[k] = v;
		}
		if(typeof this.eattrs[k] != 'undefined'){
			this.eattrs[k] = v;
		}
		if(typeof this.pattrs[k] != 'undefined'){
			this.pattrs[k] = v;
		}
	};
	
	this.__object = function(){
		var html = '';
		for(var k in this.oattrs){
			html += ' ' + k + '="' +  this.oattrs[k] + '"';
		}
		return '<object' + html + '>';
	}
	
	this.__params = function(){
		var html = '';
		for(var k in this.pattrs){	
			html += '<param name="' + k + '" value="' + this.pattrs[k]  + '" />'
		}
		for(k in this.vars){
			html += '<param name="' + k + '" value="' + this.vars[k]  + '" />'
		}
		return html;
	}
	
	this.__embed = function(){
		var html = '';
		for(var k in this.eattrs){	
			html += ' ' + k + '="' +  this.eattrs[k] + '"';
		}
		for(k in this.vars){	
			html += ' ' + k + '="' +  this.vars[k] + '"';
		}
		return '<embed' + html + '></embed>';
	}
	
	this.write = function(id){
		//alert(this.__object() + this.__params() + this.__embed() + '</object>');
		if(typeof id == 'string'){
			id = document.getElementById(id);
		}
		if(id == undefined || !id){
			document.write(this.__object() + this.__params() + this.__embed() + '</object>');
		}else{
			id.innerHTML = this.__object() + this.__params() + this.__embed() + '</object>';
		}
	}
}

/**
 * replace window.onload
 */
Jc.PageLoad = Jc.PageLoad || {
	funcs : [],
	add : function(){
		
		var numargs = arguments.length;
		if (numargs < 1 || (typeof(arguments[0]) != 'function')){
			return	
		}
		var tmp = [];
		for(var i = 0; i < arguments.length; i++){
			tmp[i] = arguments[i];
		}
		this.funcs.push({'func':tmp.shift(), 'args':tmp});
	},
	run : function(){
		for(var i = 0; i < this.funcs.length; i++){
			this.funcs[i].func.apply(null, this.funcs[i].args);
		}
	}
}

window.onload = function(){
	Jc.PageLoad.run();
}


/**
 * Marquee
 * description : html tab label
 * author      : stcer (ab12cxyh@163.com)
 * version     : 0.1.0 (2008-8)
 */
if(!document.all){
 HTMLElement.prototype.__defineGetter__("children", 
   function () { 
    var returnValue = new Object(); 
    var number = 0; 
    for (var i=0; i<this.childNodes.length; i++) { 
     if (this.childNodes[i].nodeType == 1) { 
      returnValue[number] = this.childNodes[i]; 
      number++; 
     } 
    } 
    returnValue.length = number; 
    return returnValue; 
   } 
 );
}

var Marquee = Marquee || function(demo, direction){
 var _this = this;
 var handel, demo1;
 var speed = 20, stepLong = 31;
 var step = 1;
 var counter = 0;
 var currentTop = 0, stoptime = 0;
 var posSuffix = direction == 'left' ? 'Left' : 'Top';
 var sizeSuffix = direction == 'left' ? 'Width' : 'Height';
 
 if(typeof(demo) == 'string') 
  demo = document.getElementById(demo);
 if(!demo) return;
 this.start = function(nid){
  demo1 = this.getCntNode(nid);
  if(eval('demo.offset' + sizeSuffix + ' < demo1.scroll' + sizeSuffix)){ 
   demo1.parentNode.insertBefore(demo1.cloneNode(true), demo1.nextSibling);
   handel = setInterval(_this.marquee, speed);
   demo.onmouseover = function(){ clearInterval(handel); };
   demo.onmouseout = function(){ handel = setInterval(_this.marquee, speed); }
  }
 }
 
 this.marquee = function(){
  currentTop++ ; 
  if(currentTop == stepLong){ 
    currentTop--;
    if(stoptime++ == 80){ 
        currentTop = stoptime = 0;
    }
  }else{
      if(eval('demo.scroll' + posSuffix + ' >= demo1.scroll' + sizeSuffix)){
       eval('demo.scroll' + posSuffix + ' = 0');
      }else{ 
       eval('demo.scroll' + posSuffix + ' += step');
      }
  }
 }
 
 this.setStepLong = function(long){
	 if(long != undefined){
		 stepLong = long;
		 currentTop = long - 1;
	 }
 }
 
 this.getCntNode = function(nid){
  if(typeof(nid) != 'undefined' && (node = document.getElementById(nid))){
   return node;
  }else{
   return demo.children[0];
  }
 }
}


/**
 * tabs
 * description : html tab label
 * author      : stcer (ab12cxyh@163.com)
 * version     : 0.1.0 (2008-8)
 */
function tabs(handle){
	var handle = document.getElementById(handle);
	if(!handle){
		return false;
	}
	
	var links = handle.getElementsByTagName('a');
	if(links.length > 0){
		var cLink;
		cLink = links[0];
		cLink.className = 'sel';
		for(var i = 0; i < links.length; i++){
			links[i].onmouseover = function(){
				if(cLink != this){
					var cHandle
					cHandle = document.getElementById(cLink.rel);
					if(cHandle){
						cHandle.style.display = 'none';
						cLink.className = '';
						
						cHandle = document.getElementById(this.rel);
						cHandle.style.display = 'block';
						this.className = 'sel';
						
						cLink = this;
					}
				}
			}
		}
	}
}

/**
 * more
 * description : 显示隐藏掉的更多
 * author      : stcer (ab12cxyh@163.com)
 * version     : 0.1.0 (2008-8)
 */
function more(element, top){
	if(typeof(element) == 'string')	 element = document.getElementById(element);
	if(typeof(element) != 'object') return false;	
	if(!top) top = 6;
	
	Jc.PageLoad.add(function(){
		with(element.style){
			//position  = 'relative';
			paddingRight = 30;
		}
		
		// pos
		var pos = { "x" : 0, "y" : 0 }
		_element = element;
		pos.x = document.body.offsetLeft;
		pos.y = document.body.offsetTop;
		do {
			pos.x += _element.offsetLeft;
			pos.y += _element.offsetTop;
			_element = _element.offsetParent;
			tagName = _element.tagName.toUpperCase();
		}while (tagName != 'BODY' && tagName != 'HTML');
		
		// more
		var more = document.createElement("a");
		document.body.appendChild(more);
		with(more){
			id = '_moreInfoHref';
			href = "#";
			innerHTML = '更多>>';
			style.position = 'absolute';
			style.top = (pos.y + top) + 'px';
			style.left = (pos.x + element.offsetWidth) + 'px';
			style.color = '#f00';
			className = 'moreInfoLink';
		}
		
		// hide wrap
		var wrap = document.createElement("div");
		document.body.appendChild(wrap);
		with(wrap){
			id = '_moreInfoWrap';
			className = 'moreInfo';
			style.display = 'none';
			style.position = 'absolute';
			style.top = (pos.y + element.offsetHeight + 5) + 'px';
			style.left = pos.x + 'px';
			style.width = element.offsetWidth;
			style.border = "1px solid #ccc";
			style.padding = "10px";
			style.background = "#fff";
			style.zIndex = 1000;
			innerHTML = element.innerHTML + '<div class="ar"><a href="#">关闭</a></div>';
		}
		
		var fixIfrm = document.createElement("iframe");
		with(fixIfrm){
			frameborder = 1;
			src = "###"
			style.position = 'absolute';
			style.visibility = 'inherit';
			style.top = 0;
			style.left = 0;
			style.width = '100%';
			style.zIndex = -1;
			style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
		}
		wrap.appendChild(fixIfrm);
	
		// event mouseover
		more.onmouseover = function(e){
			wrap.style.display = 'block';
			fixIfrm.height = wrap.clientHeight;
			fixIfrm.width = wrap.clientWidth;
			this.blur();

			evt = e ? e : window.event;
			if(evt.stopPropagation){
				evt.stopPropagation();
			}
			evt.cancelBubble = true;
		}
		
		var links = element.getElementsByTagName('a');
		for(var i=0; i<links.length; i++){
			//links[i].onmouseover = more.onmouseover;
		}
		
		document.onclick = function(){
			wrap.style.display = 'none';
		}
	});
}

function setMenuActive(id){
	var href = document.location.href;
	if(currentchnl = href.match(/http:\/\/([^\.]+)/)){
		chnl = currentchnl[1];
	}else{
		return;	
	}
	
	var elNav = gel(id);
	if(!elNav){
		return;	
	}
	
	var links = gelstn('a');
	for(var i = 0; i < links.length; i++){
		if(mc = links[i].href.match(/http:\/\/([^\.]+)/)){
			if(chnl == mc[1]){
				links[i].className = 'sel';
				break;
			}
		}
	}
}

var Jtools = Jtools || {
	recommend : function(type, id){
		var action = '/util/recommend.php';
		this.submit(action, type, id, url);
	},
	
	bookmark : function(type, id){
		var action = '/util/addFavorite.php';
		this.submit(action, type, id, url);
	},
	
	submit : function(action, type, id){
		url = window.location.href;
		url = encodeURIComponent(encodeURI(url));
		document.location.href = action 
			+ '?path=' + type 
			+ '&supplyId=' + id 
			+ '&url=' + url;	
	},
	
	copy : function(){
		window.clipboardData.setData('text', window.location.href);
		var msg = '已经把该信息地址复制到系统剪贴板，'
		msg += '\n\n您可以使用（Ctrl+V或鼠标右键）粘贴功能，';
		msg += '\n\n把有用的信息粘贴到QQ、MSN、UC等即时通信工具，';
		msg += '\n\n讯速传递给你的好友，商友。';
		alert(msg);	
	},
	
	show : function(type, id){
		var html;
		html = '<a href="#" onClick="Jtools.bookmark(\'' + type + '\', ' + id + ')">收藏信息</a> ';
		html += '<a href="#" onClick="Jtools.recommend(\'' + type + '\', ' + id + ')">推荐朋友</a> ';
		html += '<a href="#" onClick="Jtools.copy()">复制网址</a>';
		document.write(html);
	},
	
	subscribe : function(id, keyword, classId){
		var html, url;
		if(!classId){
			classId = '';
		}
		
		if(!keyword){
			keyword = '';
		}
		
		if(this.readCookie('JZuserid')){
			url = '/SiteGo.php?action=mysub';	
		}else{
			url = '/util/subscribe/do.php?keyword=' + keyword + '&classId=' + classId;	
		}
		
		html = '<a href="' + url + '" target="_blank" id="subscribe">订阅信息</a>'
		if(id == undefined 
		   || (typeof(id) == 'string' 
				&& !(id = document.getElementById(id))
		)){
			document.write(html);
		}else{
			id.innerHTML = html;
		}
	},
	
	readCookie : function(name){
	  var cookieValue = "";
	  var search = name + "=";
	  if(document.cookie.length > 0){ 
		offset = document.cookie.indexOf(search);
		if (offset != -1){ 
		  offset += search.length;
		  end = document.cookie.indexOf(";", offset);
		  if (end == -1) end = document.cookie.length;
		  cookieValue = unescape(document.cookie.substring(offset, end))
		}
	  }
	  return cookieValue;
	}
}

function AddFavorite(sURL, sTitle){
    try{
        window.external.addFavorite(sURL, sTitle);
    }catch (e){
        try{
            window.sidebar.addPanel(sTitle, sURL, "");
        }catch (e){
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}

function SetHome(obj,vrl){
	try{
		obj.style.behavior='url(#default#homepage)';
		obj.setHomePage(vrl);
	}catch(e){
		if(window.netscape) {
			try {
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
			}catch (e){ 
				alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'");  
			}
			var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
			prefs.setCharPref('browser.startup.homepage',vrl);
		}
	}
}
