﻿var currentFlag = "home";
$(document).ready(function() {
	$("#" + currentFlag).attr("class", "current");
});

function SetCookie(n, v) {
	if (v == undefined) {
		return $.cookie(n);
	} else {
		$.cookie(n, v, { path: '/' });
	}
}

function AddFavorite(url, title) {
    try {
        window.external.addFavorite(url, title);
    } catch (e) {
        try {
            window.sidebar.addPanel(title, url, "");
        } catch (e) {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}

function SaveHistoryHotel(hotelID) {
	if (SetCookie("HH") == null) SetCookie("HH", hotelID);
	var hh = SetCookie("HH");
	if ((',' + hh + ',').indexOf(',' + hotelID + ',') == -1) {
		hh = hh.split(",");
		hh.push(hotelID);
		if (hh.length > 6) hh = hh.slice(1);
		SetCookie("HH", hh.join(","));
	}
}

function subStr(str, len){
	if(!str || !len) return "";
	var a = 0; //预期计数：中文2字节，英文1字节
	var i = 0; //循环计数
	var temp = ""; //临时字串
	for (i=0; i<str.length; i++) {
		if (str.charCodeAt(i)>255) {
			a+=2;
		} else {
			a++;
		}
		if(a > len) return temp + "..."; //如果增加计数后长度大于限定长度，就直接返回临时字符串
		temp += str.charAt(i); //将当前内容加到临时字符串
	}
	return str; //如果全部是单字节字符，就直接返回源字符串
}

//todo: 测试用的
jQuery().ajaxError(function(request, settings) {
	alert(settings.responseText);
});

String.prototype.oReplace=function(o){
	return this.replace(/\{(\w+)\}/g,function(_,k){
		return (k in o)?o[k]:_;
	});
};
String.prototype.xReplace = function(o){
	return this.replace(/\{([0-9])\}/g,function(_,n){
		n=parseInt(n);
		return (n in o)?o[n]:_;
	});
};
(function($) {
	$.fn.SwfSlide = function(options) {
		if (this.length == 0) return;
		options = $.extend({
			swfFile: '/images/pixviewer.swf?1',
			fwidth: 200,
			fheight: 160,
			theight: 18,
			files: '',
			links: '',
			texts: ''
		}, options || {});
		
		var swfheight = options.fheight + options.theight;
  		this.html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="[url]http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6[/url],0,0,0" width="'+ options.fwidth +'" height="'+ swfheight +'"><param name="movie" value="' + options.swfFile + '"><param name="allowScriptAccess" value="sameDomain"><param name="quality" value="high"><param name="bgcolor" value="#ffffff"><param name="menu" value="false"><param name="wmode" value="opaque"><param name="FlashVars" value="pics=' + options.files +'&links=' + options.links + '&texts=' + options.texts + '&borderwidth=' + options.fwidth + '&borderheight=' + options.fheight + '&textheight=' + options.theight + '"><embed allowscriptaccess="sameDomain" src="' + options.swfFile + '" wmode="opaque" flashvars="pics=' + options.files +'&links=' + options.links + '&texts=' + options.texts + '&borderwidth=' + options.fwidth + '&borderheight=' + options.fheight + '&textheight=' + options.theight + '" menu="false" quality="high" width="' + options.fwidth + '" height="'+ swfheight +'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
		return this;
	}
	
})(jQuery);

$.fn.ToolTip = function(bgcolour, fgcolour)
{
	this.mouseover(
		function(e)
		{
			if((!this.title && !this.alt) && !this.tooltipset) return;
			// get mouse coordinates
			// based on code from http://www.quirksmode.org/js/events_properties.html
			var mouseX = e.pageX || (e.clientX ? e.clientX + document.body.scrollLeft : 0);
			var mouseY = e.pageY || (e.clientY ? e.clientY + document.body.scrollTop : 0);
			mouseX += 10;
			mouseY += 10;
			bgcolour = bgcolour || "#eee";
			fgcolour = fgcolour || "#000";
			// if there is no div containing the tooltip
			if(!this.tooltipdiv)
			{
				// create a div and style it
				var div = document.createElement("div");
				this.tooltipdiv = div;
				$(div).css(
				{
					border: "1px outset #ddd",
					padding: "3px",
					backgroundColor: bgcolour,
					color: fgcolour,
					position: "absolute"
				})
				// add the title/alt attribute to it
				.html((this.title || this.alt));
				this.title = "";
				this.alt = "";
				$("body").append(div);
				this.tooltipset = true;
			}
			$(this.tooltipdiv).show().css({left: mouseX + "px", top: mouseY + 3 + "px"});
		}
	).mouseout(
		function()
		{
			if(this.tooltipdiv)
			{
				$(this.tooltipdiv).hide();
			}
		}
	);
	return this;
}

jQuery.copy = function(t)
{
	if(typeof t=='undefined') {
		t='';
	}
	d = document;
	if (window.clipboardData) {
		window.clipboardData.setData('Text',t);
	}
	else {
		var f='flashcopier';
		if(!d.getElementById(f)) {
			var dd=d.createElement('div');
			dd.id=f;
			d.body.appendChild(dd);
		}
		d.getElementById(f).innerHTML='';
		var i='<embed src="/Js/copy.swf" FlashVars="clipboard='+encodeURIComponent(t)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
		d.getElementById(f).innerHTML=i;
	}
}
/**
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 *
 */
jQuery.cookie=function(B,I,L){if(typeof I!="undefined"){L=L||{};if(I===null){I="";L.expires=-1}var E="";if(L.expires&&(typeof L.expires=="number"||L.expires.toUTCString)){var F;if(typeof L.expires=="number"){F=new Date();F.setTime(F.getTime()+(L.expires*24*60*60*1000))}else{F=L.expires}E="; expires="+F.toUTCString()}var K=L.path?"; path="+L.path:"";var G=L.domain?"; domain="+L.domain:"";var A=L.secure?"; secure":"";document.cookie=[B,"=",encodeURIComponent(I),E,K,G,A].join("")}else{var D=null;if(document.cookie&&document.cookie!=""){var J=document.cookie.split(";");for(var H=0;H<J.length;H++){var C=jQuery.trim(J[H]);if(C.substring(0,B.length+1)==(B+"=")){D=decodeURIComponent(C.substring(B.length+1));break}}}return D}}


// 判断手机号码的正确性
function IsValidMobile(m) {
	var r = new RegExp("^13[0-9]|^15[0-9]{9}$|^18[0-9]{9}$")
	if (m.length != 11 && m.length != 12) return false;
	
	if (m.length == 12) {
		if (m.substr(0, 1) == 0) {
			m = m.substr(1, 11);
		} else {
			return false;
		}
	}
	return r.test(m);
} 

// 判断EMAIL的正确性
function IsValidEmail(e) {
	return /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/.test(e);
}

//验证是否为数字
function IsNumber(v) {
	if(!v) return false;
	var strP=/^\d+(\.\d+)?$/;
	if(!strP.test(v)) return false;
	try {
		if(parseFloat(v) != v) return false;
	} catch(ex) {
		return false;
	}
	return true;
}

// 验证是否为数字
function IsNumber(v) {
	if(!v) return false;
	var strP = /^\d+(\.\d+)?$/;
	if(!strP.test(v)) return false;
	try {
		if(parseFloat(v) != v) return false;
	} catch(ex) {
		return false;
	}
	return true;
}

// 验证是否为整数
function IsInteger(v) {
	if (!IsNumber(v)) return false;
	if (v.indexOf('.') != -1 || v.indexOf('-') != -1) return false;
	return true;
}


function LinkBox(o) {
	// title
	var titleList = $(".title ul li", o);
	var curPos = 1;
	// 初始化第一个
	$(titleList[0]).addClass('c');
	ShowList(1);
	titleList.mouseover(function() {
		$(".title .c", o).removeClass('c');
		$(".content ul:visible", o).hide();
		$(this).addClass('c');
		ShowList($(this).attr("value"));
	});
	function ShowList(pos) {
		$('.content .c' + pos).show();
	}
}