function openwinx(url,name,w,h)
{
    window.open(url,name,"top=100,left=400,width=" + w + ",height=" + h + ",toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no");
}

function Dialog(url,name,w,h)
{
	return showModalDialog(url, name, 'dialogWidth:'+w+'px; dialogHeight:'+h+'px; help: no; scroll: yes; status: no');
}

function redirect(url)
{
	if(url.lastIndexOf('/.') > 0)
	{
		url = url.replace(/\/(\.[a-zA-Z]+)([0-9]+)$/g, "/$2$1");
	}
	else if(url.match(/\/([a-z]+).html([0-9]+)$/)) {
		url = url.replace(/\/([a-z]+).html([0-9]+)$/, "/$1-$2.html");
	}
	else if(url.match(/-.html([0-9]+)$/)) {
		url = url.replace(/-.html([0-9]+)$/, "-$1.html");
	}

	if(url.indexOf('://') == -1 && url.substr(0, 1) != '/' && url.substr(0, 1) != '?') url = $('base').attr('href')+url;
	location.href = url;
}

//添加收藏夹
function myAddPanel(title,url)
{
    if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function'))
    {
        window.sidebar.addPanel(title,url,"");
    }
    else
    {
        window.external.AddFavorite(url,title);
    }
}
function confirmurl(url,message)
{
	if(confirm(message)) redirect(url);
}

function confirmform(form,message)
{
	if(confirm(message)) form.submit();
}

function getcookie(name)
{
    name = cookie_pre+name;
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while(i < clen)
	{
		var j = i + alen;
		if(document.cookie.substring(i, j) == arg) return getcookieval(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if(i == 0) break;
	}
	return null;
}

function setcookie(name, value, days)
{
    name = cookie_pre+name;
	var argc = setcookie.arguments.length;
	var argv = setcookie.arguments;
	var secure = (argc > 5) ? argv[5] : false;
	var expire = new Date();
	if(days==null || days==0) days=1;
	expire.setTime(expire.getTime() + 3600000*24*days);
	document.cookie = name + "=" + escape(value) + ("; path=" + cookie_path) + ((cookie_domain == '') ? "" : ("; domain=" + cookie_domain)) + ((secure == true) ? "; secure" : "") + ";expires="+expire.toGMTString();
}

function delcookie(name)
{
    var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	var cval = getcookie(name);
    name = cookie_pre+name;
	document.cookie = name+"="+cval+";expires="+exp.toGMTString();
}

function getcookieval(offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if(endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function checkall(fieldid)
{
	if(fieldid==null)
	{
		if($('#checkbox').attr('checked')==false)
		{
			$('input[type=checkbox]').attr('checked',true);
		}
		else
		{
			$('input[type=checkbox]').attr('checked',false);
		}
	}
	else
	{
		var fieldids = '#'+fieldid;
		var inputfieldids = 'input[boxid='+fieldid+']';
		if($(fieldids).attr('checked')==false)
		{
			$(inputfieldids).attr('checked',true);
		}
		else
		{
			$(inputfieldids).attr('checked',false);
		}
	}
}

function checkradio(radio)
{
	var result = false;
	for(var i=0; i<radio.length; i++)
	{
		if(radio[i].checked)
		{
			result = true;
			break;
		}
	}
    return result;
}

function checkselect(select)
{
	var result = false;
	for(var i=0;i<select.length;i++)
	{
		if(select[i].selected && select[i].value!='' && select[i].value!=0)
		{
			result = true;
			break;
		}
	}
    return result;
}

var flag=false;
function setpicWH(ImgD,w,h)
{
	var image=new Image();
	image.src=ImgD.src;
	if(image.width>0 && image.height>0)
	{
		flag=true;
		if(image.width/image.height>= w/h)
		{
			if(image.width>w)
			{
				ImgD.width=w;
				ImgD.height=(image.height*w)/image.width;
				ImgD.style.display="block";
			}else{
				ImgD.width=image.width;
				ImgD.height=image.height;
				ImgD.style.display="block";
			}
		}else{
			if(image.height>h)
			{
				ImgD.height=h;
				ImgD.width=(image.width*h)/image.height;
				ImgD.style.display="block";
			}else{
				ImgD.width=image.width;
				ImgD.height=image.height;
				ImgD.style.display="block";
			}
		}
	}
}

var Browser = new Object();
Browser.isMozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument!='undefined');
Browser.isIE = window.ActiveXObject ? true : false;
Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox")!=-1);
Browser.isSafari = (navigator.userAgent.toLowerCase().indexOf("safari")!=-1);
Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera")!=-1);

var Common = new Object();
Common.htmlEncode = function(str)
{
	return str.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

Common.trim = function(str)
{
	return str.replace(/(^\s*)|(\s*$)/g, "");
}

Common.strlen = function (str)
{
	if(Browser.isFirefox)
	{
		Charset = document.characterSet;
	}
	else
	{
		Charset = document.charset;
	}
	if(Charset.toLowerCase() == 'utf-8')
	{
		return str.replace(/[\u4e00-\u9fa5]/g, "***").length;
	}
	else
	{
		return str.replace(/[^\x00-\xff]/g, "**").length;
	}
}

Common.isdate = function (str)
{
   var result=str.match(/^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
   if(result==null) return false;
   var d=new Date(result[1], result[3]-1, result[4]);
   return (d.getFullYear()==result[1] && d.getMonth()+1==result[3] && d.getDate()==result[4]);
}

Common.isnumber = function(val)
{
    var reg = /[\d|\.|,]+/;
    return reg.test(val);
}

Common.isalphanumber = function (str)
{
	var result=str.match(/^[a-zA-Z0-9]+$/);
	if(result==null) return false;
	return true;
}

Common.isint = function(val)
{
    var reg = /\d+/;
    return reg.test(val);
}

Common.isemail = function(email)
{
    var reg = /([\w|_|\.|\+]+)@([-|\w]+)\.([A-Za-z]{2,4})/;
    return reg.test( email );
}

Common.fixeventargs = function(e)
{
    var evt = (typeof e == "undefined") ? window.event : e;
    return evt;
}

Common.srcelement = function(e)
{
    if (typeof e == "undefined") e = window.event;
    var src = document.all ? e.srcElement : e.target;
    return src;
}

Common.isdatetime = function(val)
{
	var result=str.match(/^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
	if(result==null) return false;
	var d= new Date(result[1], result[3]-1, result[4], result[5], result[6], result[7]);
	return (d.getFullYear()==result[1]&&(d.getMonth()+1)==result[3]&&d.getDate()==result[4]&&d.getHours()==result[5]&&d.getMinutes()==result[6]&&d.getSeconds()==result[7]);
}

var FileNum = 1;
function AddInputFile(Field)
{
    FileNum++;
	var fileTag = "<div id='file_"+FileNum+"'><input type='file' name='"+Field+"["+FileNum+"]' size='20' onchange='javascript:AddInputFile(\""+Field+"\")'> <input type='text' name='"+Field+"_description["+FileNum+"]' size='20' title='名称'> <input type='button' value='删除' name='Del' onClick='DelInputFile("+FileNum+");'></div>";
	var fileObj = document.createElement("div");
	fileObj.id = 'file_'+FileNum;
	fileObj.innerHTML = fileTag;
	document.getElementById("file_div").appendChild(fileObj);
}

function DelInputFile(FileNum)
{
    var DelObj = document.getElementById("file_"+FileNum);
    document.getElementById("file_div").removeChild(DelObj);
}

function FilePreview(Url, IsShow)
{
	Obj = document.getElementById('FilePreview');
	if(IsShow)
	{
		Obj.style.left = event.clientX+80;
		Obj.style.top = event.clientY+20;
		Obj.innerHTML = "<img src='"+Url+"'>";
		Obj.style.display = 'block';
	}
	else
	{
		Obj.style.display = 'none';
	}
}

function setEditorSize(editorID,flag)
{
	var minHeight = 400;
	var step = 150;
	var e=$('#'+editorID);
	var h =parseInt(e.height());
	if(!flag && h<minHeight)
	{
		e.height(200);
		return ;
	}
	return flag?(e.height(h+step)):(e.height(h-step));
}

function EditorSize(editorID)
{
	$('a[action]').parent('div').css({'text-align':'right'});
	$('a[action]').css({'font-size':'24px','font-weight':700,display:'block',float:'right',width:'28px','text-align':'center'});
	$('a[action]').click(function(){
		var flag= parseInt($(this).attr('action'));
		setEditorSize(editorID,flag);
	});
}

function loginCheck(form)
{
	var username = form.username;
	var password = form.password;
	var cookietime = form.cookietime;
	if(username.value == ''){alert("请输入用户名");username.focus();return false;}
	if(password.value == ''){alert("请输入密码");password.focus();return false;}
	var days = cookietime.value == 0 ? 1 : cookietime.value/86400;
	setcookie('username', username.value, days);
	return true;
}

function modal(url, triggerid, id, type)
{
	id = '#' + id;
	triggerid = '#' + triggerid;
	switch(type)
	{
		case 'ajax':
			$(id).jqm({ajax: url, modal:false, trigger: triggerid});
		break;
		default:
			$(id).jqm();
		break;
	}
	$(id).html('');
	$(id).hide();
}

function menu_selected(id)
{
	$('#menu_'+id).addClass('selected');
}

function CutPic(textid,thumbUrl){
  var thumb= $('#'+textid).val();
	if(thumb=='')
	{
		alert('请先上传标题图片');
		$('#'+textid).focus();
		return false;
	}
	else
	{
		if(thumb.indexOf('://') == -1) thumb = thumbUrl+thumb;
	}
  var arr=Dialog(phpcms_path + 'corpandresize/ui.php?'+thumb,'',700,500);
  if(arr!=null){
    $('#'+textid).val(arr);
  }
}

function is_ie()
{
	if(!$.browser.msie)
	{
		$("body").prepend('<div id="MM_msie" style="border:#FF7300 solid 1px;padding:10px;color:#FF0000">本功能只支持IE浏览器，请用IE浏览器打开。<div>');
	}
}

function select_catids()
{
	$('#addbutton').attr('disabled','');

}

function transact(update,fromfiled,tofiled)
{
	if(update=='delete')
	{
		var fieldvalue = $('#'+tofiled).val();

		$("select[@id="+tofiled+"] option").each(function()
		{
		   if($(this).val() == fieldvalue){
			$(this).remove();
		   }
		});
	}
	else
	{
		var fieldvalue = $('#'+fromfiled).val();
		var have_exists = 0;
		var len = $("select[@id="+tofiled+"] option").length;
		if(len>5)
		{
			alert('最多添加 6 项');
			return false;
		}
		$("select[@id="+tofiled+"] option").each(function()
		{
		   if($(this).val() == fieldvalue){
			have_exists = 1;
			alert('已经添加到列表中');
			return false;
		   }
		});
		if(have_exists==0)
		{
			fieldvalue = "<option value='"+fieldvalue+"'>"+fieldvalue+"</option>"
			$('#'+tofiled).append(fieldvalue);
			$('#deletebutton').attr('disabled','');
		}
		
	}
}
var set_show = false;


//ie6 css 背景缓存
if (/* @cc_on @_jscript_version === 5.6 || @ */false) try {document.execCommand("BackgroundImageCache", false, true);} catch (err) {}

/* Browser Storage */
(function(){if(!(window.google&&google.gears)){var c=null;if(typeof GearsFactory!="undefined")c=new GearsFactory;else try{c=new ActiveXObject("Gears.Factory");c.getBuildInfo().indexOf("ie_mobile")!=-1&&c.privateSetGlobalObject(this)}catch(g){if(typeof navigator.mimeTypes!="undefined"&&navigator.mimeTypes["application/x-googlegears"]){c=document.createElement("object");c.style.display="none";c.width=0;c.height=0;c.type="application/x-googlegears";document.documentElement.appendChild(c)}}if(c){window.google|| (google={});if(!google.gears)google.gears={factory:c}}}})(); (function(){function c(){return g.init()||h.init()||i.init()||j.init()||false}window.BrowserStore={isReady:false,initialize:function(){this.storage=c()||{};this.isReady=true},set:function(a,b){this.isReady||this.initialize();this.storage.setStorage(a,b?jQuery.toJSON(b):null);return this},get:function(a,b){this.isReady||this.initialize();this.storage.getStorage(a,b)},remove:function(a){if(!a||!this.storage)return false;this.storage.removeStorage(a);return this},clear:function(){if(!this.storage)return false; this.storage.clearStorage();return this}};var g={name:"Google Gears",init:function(){var a;try{if(a=google.gears.factory.create("beta.database")){a.open("Asa_database");a.execute("create table if not exists status (skey text, sval text)");this.db=a;this.Master="gears"}else return false}catch(b){return false}return this},setStorage:function(a,b){var d=this.db.execute("select * from status where skey=?",[a]);if(d.isValidRow()){this.db.execute("update status set sval=? where skey=?",[b,a]);d.close()}else this.db.execute("insert into status values (?,?)", [a,b])},getStorage:function(a,b){var d=this.db.execute("select * from status where skey=?",[a]);d.isValidRow()?b(d.field(1)):b();d.close()},removeStorage:function(a){this.db.execute("delete from status where skey=?",[a])},clearStorage:function(){this.db.execute("drop table status")}},h={name:"globalStorage",init:function(){if(!window.globalStorage)return false;this._storage=globalStorage[location.hostname];return this},setStorage:function(a,b){this._storage.setItem(a,b);return true},getStorage:function(a, b){var d=this._storage.getItem(a);b(d?d.value:null)},removeStorage:function(a){this._storage.removeItem(a);return true},clearStorage:function(){if(this._storage.clear)this._storage.clear();else for(c in this._storage)this._storage[c].value&&this.remove(c);return true}},i={name:"userdata",init:function(){this.Master="ie6+";if(!$.browser.msie)return false;var a=document.createElement("span");a.style.display="none";a.style.behavior="url('#default#userData')";document.body.appendChild(a);this._storage= a;return this},setStorage:function(a,b){this._storage.setAttribute(a,b);this._storage.save("AsaData");return true},getStorage:function(a,b){this._storage.load("AsaData");b(this._storage.getAttribute(a))},removeStorage:function(a){this._storage.removeAttribute(a);this._storage.save("AsaData");return true},clearStorage:function(){var a=new Date;a.setMinutes(a.getMinutes()-1);this._storage.expires=a.toUTCString();this._storage.save("AsaData");this._storage.load("AsaData");return true}},j={name:"openDatabase", init:function(){if(!window.openDatabase)return false;this._storage=window.openDatabase("viewState","1.0","Asa Storage",2E4);this._createTable();return this},setStorage:function(a,b){this._storage.transaction(function(d){d.executeSql("SELECT v FROM SessionStorage WHERE k = ?",[a],function(f,e){e.rows.length>0?f.executeSql("UPDATE SessionStorage SET v = ? WHERE k = ?",[b,a]):f.executeSql("INSERT INTO SessionStorage (k, v) VALUES (?, ?)",[a,b])})});return true},getStorage:function(a,b){this._storage.transaction(function(d){v= d.executeSql("SELECT v FROM SessionStorage WHERE k = ?",[a],function(f,e){if(e.rows.length>0)return b(e.rows.item(0).v);b(null)})})},removeStorage:function(a){this._storage.transaction(function(b){b.executeSql("DELETE FROM SessionStorage WHERE k = ?",[a])});return true},clearStorage:function(){this._storage.transaction(function(a){a.executeSql("DROP TABLE SessionStorage",[])});return true},_createTable:function(){this._storage.transaction(function(a){a.executeSql("SELECT COUNT(*) FROM SessionStorage", [],$empty,function(b){b.executeSql("CREATE TABLE SessionStorage (k TEXT, v TEXT)",[],$empty)})})}}})();
/*Protype*/
String.prototype.cnLength = function()
{
  return this.replace('/[^\x00-\xff]/ig',"**").length;
};
App = {};
App._model = [];
App.loadModel = function( mName ){
	if( typeof( this._model[mName] )=='function')
	{
		this._model[mName]();
		return true;
	}
	throw mName+' model is not exist';
};
App.addModel = function( mName, fun )
{
	if( typeof( this._model[mName] )!='undefined')
	{
		throw mName+' model is exist,will be override';
	}
	if( typeof( fun )=='function' )
	{
		this._model[mName] = fun;
	}
	else
	{
		throw 'Register model error,it`s not a function';
	}
};

App.addModel('slider',function(){	
loopedSlider = {
	total:null,
	now:null,
	slidParent:null,
	slids:null,
	time:null,
	handles:null,
	start:function(){
		loopedSlider.time = setInterval('loopedSlider.next()',2500);
	},
	clearTime:function(){
		clearInterval(loopedSlider.time);
	},
	next:function(index){
		if(index==undefined){
			var prev = loopedSlider.now;
			var next = (prev+1>=loopedSlider.total)?0:prev+1;
			
		}else{
			//相等不执行效果
			if( index==loopedSlider.now ) return;
			var prev = loopedSlider.now;			
			var next = index;
		}
		loopedSlider.slids.eq(prev).animate({opacity:0},{queue:false,duration:500}).removeClass('current');
		loopedSlider.slids.eq(next).animate({opacity:1},{queue:false,duration:500}).addClass('current');
		var href = loopedSlider.slids.eq(next).find('a').attr('href');
		loopedSlider.handles.children().removeClass('current');
		loopedSlider.handles.eq(next).children().addClass('current');
		loopedSlider.shade.attr('href',href);
		loopedSlider.now = next;
	},
	initEvent:function(){
		loopedSlider.handles.bind({
			'mouseover':function(){
				loopedSlider.clearTime();
				var index = loopedSlider.handles.index(this);
				loopedSlider.next(index);
			},
			'mouseleave':function(){
				loopedSlider.start();
			}
		})
	},
	init:function(){
		this.slidParent = $('#slider-list');
		this.slids = this.slidParent.children();
		this.total = this.slids.length;
		this.shade = $('#slider-shade');
		if(!this.total) return false;
		this.handles = $('#slider-btn a');
		this.now = 0;
			
		//开始运行
		this.initEvent();
		this.start();
	}
}
loopedSlider.init();
});

App.addModel('toShare',function(){
	var toShare = {
		b_rr: function(){
			var d = "http://share.xiaonei.com/share/buttonshare.do?link=" + encodeURIComponent(document.location.href) + "&title=" + encodeURIComponent(document.title);
			if (!window.open(d, "xiaonei", "toolbar=0,resizable=1,scrollbars=yes,status=1,width=626,height=436")) {
				location.href = d
			}
		},
		b_kx: function(){
			var d = "http://www.kaixin001.com/repaste/share.php?rtitle=" + encodeURIComponent(document.title) + "& rurl=" + encodeURIComponent(document.location.href);
			if (!window.open(d, "kaixin")) {
				location.href = d;
			}
		},
		b_db: function(){
			var d = "http://www.douban.com/recommend/?url=" + encodeURIComponent(document.location.href) + "&title=" + encodeURIComponent(document.title);
			if (!window.open(d, "douban", "toolbar=0,resizable=1,scrollbars=yes,status=1,width=450,height=330")) {
				location.href = d;
			}
		},
		b_qq: function(){
			var d = 'http://shuqian.qq.com/post?from=3&title='+encodeURIComponent(document.title)+'&uri='+encodeURIComponent(document.location.href)+'&jumpback=2&noui=1';
			if (!window.open(d, "qq", 'width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes') ){
				location.href = d;
			}
		
		},
		b_wb: function(){
			var f = "http://v.t.sina.com.cn/share/share.php", g = {
				c: "spr_web_bd_hululady_weibo",
				url: document.location.href,
				title: document.title,
				source: "hululady",
				sourceUrl: "http: //www.hululady.com/",
				content: "gb2312"
			}, e = f + "?" + $.param(g);
			if (!window.open(e, "mb", "toolbar=0,resizable=1,scrollbars=yes,status=1,width=440,height=430,left=" + (screen.width - 440) / 2 + ",top=" + (screen.height - 430) / 2)) {
				location.href = e;
			}
		},
		b_wy: function(){
			var f = 'http://t.163.com/article/user/checkLogin.do'
			var g = 'link='+encodeURIComponent(location.href)+'&source='+encodeURIComponent('葫芦女人')+'&info='+encodeURIComponent(document.title.split('_')[0])+' '+encodeURIComponent(location.href)+'&'+new Date().getTime();
			var e = f+'?'+g;
			if (!window.open(e, "mb","toolbar=0,resizable=1,scrollbars=yes,status=1,width=560,height=330,left="
							+ (screen.width - 560) / 2 + ",top="
							+ (screen.height - 330) / 2)) {
				location.href = e;
			}
		}
	};
	$('#share_button a').click(function(){	
		if(toShare[$(this).attr('class')]) toShare[$(this).attr('class')]();
		return false;
	});
});

App.addModel('VideoFunc',function(){
	var func = {
		func_collect:function()
		{
			myAddPanel(document.title,location.href);
		},
		func_down:function()
		{
			alert('下载稍后开放');
		},
		func_comment:function()
		{
			location.href = location.href.split('#')[0]+'#comment-box';
		}
	};
	$('#content_func').children().click(function(){
		var n = $(this).attr('class');
		if(func[n]) func[n]();
		return false;
	});
});

App.addModel('QCGuide',function(){
	var oLeft = $('#qc-scroll-left');
	var oRight = $('#qc-scroll-right');
	var oScroll = $('#qc-scroll-inner');
	var oWidth = $('#qc-scroll-box').width()-780;
	var aList = $('#qc-scroll-inner div.aa a');
	var aText = $('<div id="qc-text" class="hidden"></div>').appendTo(document.body);
	aList.hover(
		function(){
			var text = $(this).find('pre').text();
			var left = $( this ).offset().left-5;
			var top = $( this ).offset().top-25;
			aText.text( text ).css({ top:top, left:left }).removeClass('hidden');
		},
		function(){ aText.addClass('hidden'); }
	);
	oRight.click(function(){
		aText.addClass('hidden');
		var left = parseInt(oScroll.css('left'))||0;
		if( left<oWidth ) return false;
		oScroll.animate({'left':left-120},'normal');
		return false
	});
	oLeft.click(function(){
		aText.addClass('hidden');
		var left = parseInt(oScroll.css('left'))||0;
		if( left>-120 && left<=0 )
		{
			oScroll.css('left',0);
		}
		else
		{
			oScroll.animate({'left':left+120},'normal');
		}
		return false;
	});
});
App.addModel('tabs',function(){
	$('ul.tab-menu li').hover(function(){
		if($(this).hasClass('hover')) return;
		var mParent = $(this).parent();
		var content = mParent.next();
		var index = mParent.children().index($(this));
		mParent.children().removeClass('hover').eq(index).addClass('hover');
		content.children().addClass('hidden').eq(index).removeClass('hidden');
	});
})

