var _sso_login_type;
var _sso_logout_type;
var _mail_factor=false;
var _nickname_factor=false;
var _verifyCode_factor=false;
var _login_cookie_key='ticket';

$(document).ready(function(){
	_sso_check_login();
	$('.user_type').hide();
	var hosturl = window.location.host;
	var navTab=$('.navigation_right li[id^="nav"] a[href*='+hosturl+']');
	$('.reg_log > a:eq(1)').attr('href','http://visas.oyesgo.com/user/register.html?'+escape(window.location.href));
	$.fn.hover_sub=function(){$(this).hover(function(){$("#sub" + this.id).addClass("on");},function(){$("#sub" + this.id).removeClass("on");});}
	$("#nav1,#nav2,#nav3,#nav4,#nav14,#nav5,#nav15,#nav6,#nav7,#nav8,#nav9,#nav10,#nav11,#nav12,#nav13,#nav16,#nav17").hover_sub();	
	if(hosturl!='' && navTab.html()!=null)navTab.addClass('on'); else $('.navigation_right li[id^="nav"] a[href*=www.oyesgo.com]').addClass('on');
	$.getScript('http://backend.oyesgo.com/home/header_notice.js');
	//$('.navigation_right>ul').append('<li id="flower"><a href="http://events.oyesgo.com/" target="_blank"><img src="http://res.oyesgo.com/styles/common_new/images/flights_event.gif" width="136" height="31" alt="" /></a></li>');
	//$.getScript('http://visas.oyesgo.com/getSearchTimes.jsp');
	_set_search_count(674);
});
 function _oyesgo_index_log(type){
	if(getDecodeCookie('oyesgo_user_trace')!=null)
	{
		var params={type:type};
    	$.getScript('http://extend.oyesgo.com/index_log.jsp?'+$.param(params));
	}
	else if(document.referrer!=null && document.referrer!='')
	{
		var params={type:type,referer:document.referrer};
    	$.getScript('http://extend.oyesgo.com/index_log.jsp?'+$.param(params));
	}
}	
function _oyesgo_unfold_log(type,keyword){
	if(getDecodeCookie('oyesgo_user_trace')!=null)
	{
		var params={type:type,keyword:keyword};
    	$.getScript('http://extend.oyesgo.com/unfold_log.jsp?'+$.param(params));
	}
	else if(document.referrer!=null && document.referrer!='')
	{
		var params={type:type,referer:document.referrer,keyword:keyword};
    	$.getScript('http://extend.oyesgo.com/unfold_log.jsp?'+$.param(params));
	}
}
function _oyesgo_search_log(type,keyword){
	if(getDecodeCookie('oyesgo_user_trace')!=null)
	{
		var params={type:type,keyword:keyword};
    	$.getScript('http://extend.oyesgo.com/search_log.jsp?'+$.param(params));
	}
	else if(document.referrer!=null && document.referrer!='')
	{
		var params={type:type,referer:document.referrer,keyword:keyword};
    	$.getScript('http://extend.oyesgo.com/search_log.jsp?'+$.param(params));
	}
}
function _oyesgo_order_log(type,provider,product){
	if(getDecodeCookie('oyesgo_user_trace')!=null)
	{
		var params={type:type,provider:provider,product:product};
    	$.getScript('http://extend.oyesgo.com/order_log.jsp?'+$.param(params));
	}
	else if(document.referrer!=null && document.referrer!='')
	{
		var params={type:type,referer:document.referrer,provider:provider,product:product};
    	$.getScript('http://extend.oyesgo.com/order_log.jsp?'+$.param(params));
	}
}
function getNewCookie(name){
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return decodeURIComponent(arr[2]); return null;
}
function save_new_history(url,type,left,right){
	left = encodeURIComponent(left);
	right = encodeURIComponent(right);
	var content = url+"+"+left+"+"+right;
	var cookie = getNewCookie("oyesgo_search_history_"+type);
	if(cookie!=null){
		if(cookie.indexOf(decodeURI(url))>-1)
			return;
		var arr = cookie.split("$");
		if(arr.length>=6){
			arr.pop();
			cookie = arr.join("$");
		}
		content = content + "$"+ encodeURI(cookie);
	}
	var Days = 30;var exp  = new Date();
  exp.setTime(exp.getTime() + Days*24*60*60*1000);
  document.cookie = "oyesgo_search_history_"+type + "="+ content + ";expires=" + exp.toGMTString()+";path=/;domain=oyesgo.com";
}
function display_cookie(type, target, s_left, s_top) {
	var cookie = null;
	cookie = getNewCookie("oyesgo_search_history_" + type);
	if (cookie == null)
		return;
	var arr = cookie.split("$");
	vtext = '<ul>';
	for ( var i = 0; i < arr.length; i++) {
		var arrr = arr[i].split("+");
		var link = arrr[0];
		if (type == "train") {
			link = link.replace(/@/g, "%u");
		}
		vtext += '<li><a href="' + encodeURI(link) + '" target="_blank"><span>' + arrr[1]
				+ '</span><em>' + arrr[2] + '</em></a></li>';
	}
	vtext += '</ul>';
	if ($(".search_note_pop").length == 0) {
		$("<div class=\"search_note_pop\" style=\"display:none;\"></div>")
				.appendTo(document.body);
	}
	$(".search_note_pop").html("").html(vtext).css("left",
			target.offset().left - (s_left?s_left:144)).css("top", target.offset().top + (s_top?s_top:36))
			.show();
}
function save_history(url,type,keyword,date,city){
	var type_copy = type;
	type = encodeURIComponent(type);
	city = encodeURIComponent(city);
	keyword = encodeURIComponent(keyword);
	if(keyword.length<=0)
		keyword = city;
	var content = url+"+"+type+"+"+keyword+"+"+date;
	var cookie = getDecodeCookie ("oyesgo_search_history");
	if(cookie!=null){
		var ca = cookie.split("$");
		var ba = [];
		for(var i = 0 ; i< ca.length ;i++){
			if (ca[i].indexOf(type_copy)<0) 
				ba.push(ca[i]);
		}
		if(ba.length>0)
			content = content + "$"+ encodeURI(ba.join("$"));
	}
	var Days = 30;
    var exp  = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = "oyesgo_search_history" + "="+ content + ";expires=" + exp.toGMTString()+";path=/;domain=oyesgo.com";
	//$.getScript('http://visas.oyesgo.com/setSearchTimes.jsp');
}
function stopBubble(e) {
	if (e && e.stopPropagation) {
		e.stopPropagation();
	} else {
		window.event.cancelBubble = true;
	}
}
function stopDefault(e)
{
	if (e && e.preventDefault) 
		e.preventDefault(); 
	else
		window.event.returnValue=false; 
}
function formatDate(date){
	var year=date.getFullYear().toString();
	var month=date.getMonth()+1;
	var day=date.getDate();
	return year+"-"+(month>9?month:("0"+month))+"-"+(day>9?day:("0"+day));
}
function getDecodeCookieVal(offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) 
        endstr = document.cookie.length;
    return document.cookie.substring(offset, endstr);
}
function getDecodeCookie(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  decodeURIComponent(getDecodeCookieVal(j));
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
    }
    return null;
}
function _set_search_count(count)
{
	$('.most_searched_num > strong').html(count)
}
function _hide_search_history()
{
	$('#pop_search_history').hide();
}
function _show_search_history(e,obj)
{
	stopBubble(e);
	var history=getDecodeCookie('oyesgo_search_history');
	if(history)
	{
		var line=new Array();
		var historys=history.split('$');
		for(var i=0;i<historys.length;i++)
		{
			var record = historys[i].split('+');
			line.push('<li><a href="');
			line.push(record[0]);
			line.push('"><span>【');
			line.push(record[1]);
			line.push('】</span> <strong>');
			line.push(record[2]);
			line.push('</strong><em>');
			line.push(record[3]);
			line.push('</em></a></li>');
		}
		$('#ul_search_history').html(line.join(''));
	}
	else
		$('#ul_search_history').html('<li><strong class="none">　　　　　　　　　　暂无任何搜索历史记录　　　　　　　　　</strong></li>');
	$('#pop_search_history').css({left:obj.offset().left-199, top:obj.offset().top+36});
	$('#pop_search_history').show();
}
function _verify_code_img(obj,type)
{
	obj.attr('src','http://visas.oyesgo.com/user/verifyCode.jsp?x='+type+'&w=70&h=23&k='+Math.random());
}
function _verify_code_submit(e,exec)
{
	var e = e?e:window.event;
	var code = e.keyCode?e.keyCode:e.which;
	if(code == 13)exec();
}
function _sso_check_login()
{	
	var key=getDecodeCookie(_login_cookie_key);
	if(key==null)
	{
		$('.login').hide();
		$('.reg_log').show();
	}
	else
	{
		var userInfo=key.substring(1,key.length-2).split('|');
		$('#loginName').html(userInfo[5]);
		$('.login').show();
		$('.reg_log').hide();
	}
}
function _sso_login()
{
	if(_sso_checkAccount($('#account').val()) && _sso_checkPassword($('#pwd').val()) && _sso_checkVerifyCode($('#code').val()))
	{
		$('#logWaitImg').show();
		url="http://visas.oyesgo.com/user/logAjax.jsp?account="+encodeURIComponent($('#account').val())+"&pwd="+$('#pwd').val()+"&code="+$('#code').val()+'&k='+_sso_login_type+'&type='+$('input[name=userType]:checked').val();
		if($('#hold').get(0).checked)url+=('&hold='+$('#hold').val());
			$.getScript(url); 
	}
}
function _sso_logout(type)
{
	_sso_logout_type=(type==null && window.location.href.indexOf('http://travelask.oyesgo.com')==0)?'oyesgo_ask_user_transact_callback();':type;
	var key=getDecodeCookie(_login_cookie_key);
	if(key!=null)
	{
		var userInfo=key.substring(1,key.length-2).split('|');
		$.getScript('http://visas.oyesgo.com/user/verifyAjax.jsp?k=5&id='+userInfo[0]+'&code='+_sso_logout_type);
	}
}
function _sso_logout_success()
{
	$('.login').hide();
	$('.reg_log').show();
}
function _sso_success(nickname)
{
	$('a[title=Close]').click();
	$('.reg_log').hide();
	$('#loginName').html(nickname);
	$('.login').show();
}
function _sso_failure()
{
	$('#logWaitImg').hide();
	$('#logVerify').attr('src','http://visas.oyesgo.com/user/verifyCode.jsp?x=1&w=70&h=23&k='+Math.random());
	$('#pwdAlt').addClass('wrong');
	$('#pwdAlt').html('您的密码错误。');
}
function _sso_error()
{
	$('#logWaitImg').hide();
	$('#logVerify').attr('src','http://visas.oyesgo.com/user/verifyCode.jsp?x=1&w=70&h=23&k='+Math.random());
	$('#logCode').addClass('wrong');
	$('#logCode').html('您的验证码错误。');
}
function _sso_account_error(message)
{
	$('#logWaitImg').hide();
	$('#logVerify').attr('src','http://visas.oyesgo.com/user/verifyCode.jsp?x=1&w=70&h=23&k='+Math.random());
	$('#accountAlt').addClass('wrong');
	$('#accountAlt').html(message);
}
function _sso_checkAccount(val)
{
	return _sso_judge(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/.test(val),$('#accountAlt'),
	'','您的用户名不存在。');
}
function _sso_checkPassword(val)
{
	return _sso_judge(val.length>3,$('#pwdAlt'),
	'','您的密码错误。');
}
function _sso_checkVerifyCode(val)
{
	return _sso_judge(/^[0-9a-zA-Z]{4}$/.test(val),$('#logCode'),
	'','您的验证码错误。');
}
function _sso_judge(factor,obj,correct,error)
{
	if(factor)
	{	
		obj.removeClass('wrong');
		obj.html(correct);
		return true;
	}
	else
	{
		obj.addClass('wrong');
		obj.html(error);
		return false;
	}
}
function _reg_check(){
	if(_reg_checkMail($('#email'),true) && _reg_checkNickname($('#nickname'),true) 
	&& _reg_checkPassword($('#passwd')) && _reg_checkRePwd($('#repwd')) 
	&& _reg_checkVerifyCode($('#rcode'),true))
	{
		$('#regWaitImg').show();
		var mailVal=$('#email').val();
		var mailUrl=mailVal.substr(mailVal.indexOf('@')+1);
		$('#mailhost').html(mailUrl);
		if(mailVal.indexOf('@vip.163.com')>0)
			$('#mailsite').click(function(){window.open('http://vip.163.com');});
		else if(mailVal.indexOf('@yahoo.c')>0)
			$('#mailsite').click(function(){window.open('http://mail.cn.yahoo.com');});
		else if(mailVal.indexOf('@yeah.net')>0 || mailVal.indexOf('@gmail.com')>0 || mailVal.indexOf('@hotmail.com')>0)
			$('#mailsite').click(function(){window.open('http://www.'+mailUrl);});
		else if(mailVal.indexOf('@sina.com')>0 || mailVal.indexOf('@163.com')>0 || mailVal.indexOf('@sohu.com')>0 
				|| mailVal.indexOf('@126.com')>0 || mailVal.indexOf('@qq.com')>0)
			$('#mailsite').click(function(){window.open('http://mail.'+mailUrl);});
		else
			$('#mailsite').hide();
		$('[name=mailAlt]').html(mailVal);
		url="http://visas.oyesgo.com/user/regAjax.jsp?email="+mailVal+"&nickname="+encodeURIComponent($('#nickname').val())+"&pwd="+$('#passwd').val()+"&code="+$('#rcode').val();
		$.getScript(url);
	}
}
function _reg_checkMail(obj,exec)
{
	if(obj.val()=='')
		return _reg_judge(false,obj,'用来登陆网站和找回密码，请填写自己的可用邮箱。','您的电子邮箱必须填写。');
	else
	{
		var factor=/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/.test(obj.val());
		if(factor)
		{
			if(_mail_factor)return true;
			$.getScript('http://visas.oyesgo.com/user/verifyAjax.jsp?k=1&email='+obj.val()+'&w='+exec);
		}
		else
			return _reg_judge(factor,obj,'用来登陆网站和找回密码，请填写自己的可用邮箱。','邮箱格式错误，请重新填写。');
	}
}
function _reg_checkNickname(obj,exec)
{
	if(obj.val()=='')
		return _reg_judge(false,obj,'不超过20个字符(包括字母,数字,下划线,中文),请不要使用特殊字符。','您的昵称必须填写。');
	else
	{
		var factor=/^[a-zA-Z0-9\u4e00-\u9fa5_]{1,20}$/.test(obj.val());
		if(factor)
		{
			if(_nickname_factor)return true;
			$.getScript('http://visas.oyesgo.com/user/verifyAjax.jsp?k=2&nickname='+encodeURIComponent(obj.val())+'&w='+exec);
		}
		else
			return _reg_judge(factor,obj,'不超过20个字符(包括字母,数字,下划线,中文),请不要使用特殊字符。','昵称中不能包含特殊字符。');
	}
}
function _reg_checkPassword(obj)
{
	return _reg_judge(obj.val()==''?false:obj.val().length>3,obj,
	'4~16位，字母区分大小写。',obj.val()==''?'您的密码不能为空。':'密码太短，请输入4~16位。');
}
function _reg_checkRePwd(obj)
{
	return _reg_judge(obj.val()==''?false:obj.val()==$('#passwd').val(),obj,
	'请再输入一次密码。',obj.val()==''?'您重输的密码不能为空。':'您两次输入的密码不一致。');
}
function _reg_checkVerifyCode(obj,exec)
{
	if(obj.val()=='')
		return _reg_judge(false,obj,'请输入验证码。','您的验证码不能为空。');
	else
	{
		var factor=/^[0-9a-zA-Z]{4}$/.test(obj.val());
		if(factor)
		{
			if(_verifyCode_factor)return true;
			$.getScript('http://visas.oyesgo.com/user/verifyAjax.jsp?k=3&code='+obj.val()+'&w='+exec);
		}
		else
			return _reg_judge(factor,obj,'请输入验证码。','您输入的验证码不正确。');
	}
}
function _reg_judge(factor,obj,correct,error)
{
	var alt=obj.nextAll('span:not(.sign_code)');
	if(factor)
	{
		obj.next('img').css('display','block');
		alt.html(correct);
		alt.removeClass('error');
		return true;
	}
	else
	{
		obj.next('img').css('display','none');
		alt.html(error);
		alt.addClass('error');
		return false;
	}
}
function _reg_success()
{
	_sso_check_login();
	$('#con_one_1').hide();
	$('#con_one_2,.tit').fadeOut('fast',function(){
		$('#con_one_3').fadeIn('fast');
	});
}
function _sso_tab()
{
	$('#con_one_2').fadeOut('fast',function(){
		$('.simplemodal-container').animate({
			height:'243px'
		},'fast');
		$('#one1').addClass('on');
		$('#one2').removeClass('on');
		$('#con_one_1').fadeIn('fast');
	});
}
function _reg_tab()
{
	$('#con_one_1').fadeOut('fast',function(){
		$('.simplemodal-container').animate({
			height:'341px'
		},'fast',function(){
			$('#one1').removeClass('on');
			$('#one2').addClass('on');
			$('#con_one_2').fadeIn('fast');
		});
	});
}
function _header_modal(type)
{
	_sso_login_type=(type==null && window.location.href.indexOf('http://travelask.oyesgo.com')==0)?'oyesgo_ask_user_transact_callback();':type;;
	$('#logVerify').attr('src','http://visas.oyesgo.com/user/verifyCode.jsp?x=1&w=70&h=23&k='+Math.random());
	$('#regVerify').attr('src','http://visas.oyesgo.com/user/verifyCode.jsp?x=2&w=70&h=23&k='+Math.random());
    try {$('.signin').modal({position: [150,]});}catch(e){_header_modal(_sso_login_type);}
}
function _header_modal_open(dialog)
{
	dialog.data.show();
	dialog.overlay.fadeIn('fast', function () {
		dialog.container.fadeIn('fast');
	});
}
function _header_modal_close(dialog)
{
	dialog.data.fadeOut('fast', function () {
		dialog.container.hide('normal', function () {
			dialog.overlay.fadeOut('fast', function () {
				$.modal.close();
			});
		});
	});
}
function _reg_email_error()
{
	_mail_factor=false;
	$('#emailImg').hide();
	$('#emailAlt').addClass('error');
	$('#emailAlt').html('此邮箱已经被注册，请用其他邮箱。');
}
function _reg_nickname_error()
{
	_nickname_factor=false;
	$('#nicknameImg').hide();
	$('#nicknameAlt').addClass('error');
	$('#nicknameAlt').html('此昵称已被使用，请另选一个。');
}
function _reg_code_error()
{
	_verifyCode_factor=false;
	$('#verifyCodeImg').hide();
	$('#verifyCodeAlt').addClass('error');
	$('#verifyCodeAlt').html('验证码错误，请重新输入。');
	$('#regVerify').attr('src','http://visas.oyesgo.com/user/verifyCode.jsp?x=2&w=70&h=23&k='+Math.random());
}
function _reg_email_correct(factor)
{
	_mail_factor=true;
	$('#emailImg').show();
	$('#emailAlt').removeClass('error');
	$('#emailAlt').html('用来登陆网站和找回密码，请填写自己的可用邮箱。');
	if(factor)_reg_check($('#reg'));
}
function _reg_nickname_correct(factor)
{
	_nickname_factor=true;
	$('#nicknameImg').show();
	$('#nicknameAlt').removeClass('error');
	$('#nicknameAlt').html('不超过20个字符(包括字母,数字,下划线,中文),请不要使用特殊字符。');
	if(factor)_reg_check($('#reg'));
}
function _reg_code_correct(factor)
{
	_verifyCode_factor=true;
	$('#verifyCodeImg').show();
	$('#verifyCodeAlt').removeClass('error');
	$('#verifyCodeAlt').html('请输入验证码。');
	if(factor)_reg_check($('#reg'));
}
function _reg_send_mail()
{
	$.getScript('http://visas.oyesgo.com/user/verifyAjax.jsp?k=4&email='+$('#email').val());
}
function _reg_mail_error()
{
	$('#mailRefuse').show();
	$('#resend').hide();
}

;(function($){var ie6=$.browser.msie&&parseInt($.browser.version)==6&&typeof window['XMLHttpRequest']!="object",ieQuirks=null,w=[];$.modal=function(data,options){return $.modal.impl.init(data,options);};$.modal.close=function(){$.modal.impl.close();};$.fn.modal=function(options){return $.modal.impl.init(this,options);};$.modal.defaults={appendTo:'body',focus:true,opacity:50,overlayId:'simplemodal-overlay',overlayCss:{},containerId:'simplemodal-container',containerCss:{},dataId:'simplemodal-data',dataCss:{},minHeight:200,minWidth:300,maxHeight:null,maxWidth:null,autoResize:false,zIndex:1000,close:true,closeHTML:'<a class="modalCloseImg" title="Close"></a>',closeClass:'simplemodal-close',escClose:true,overlayClose:false,position:null,persist:false,onOpen:null,onShow:null,onClose:null};$.modal.impl={opts:null,dialog:{},init:function(data,options){if(this.dialog.data){return false;}ieQuirks=$.browser.msie&&!$.boxModel;this.opts=$.extend({},$.modal.defaults,options);this.zIndex=this.opts.zIndex;this.occb=false;if(typeof data=='object'){data=data instanceof jQuery?data:$(data);if(data.parent().parent().size()>0){this.dialog.parentNode=data.parent();if(!this.opts.persist){this.dialog.orig=data.clone(true);}}}else if(typeof data=='string'||typeof data=='number'){data=$('<div/>').html(data);}else{alert('SimpleModal Error: Unsupported data type: '+typeof data);return false;}this.create(data);data=null;this.open();if($.isFunction(this.opts.onShow)){this.opts.onShow.apply(this,[this.dialog]);}return this;},create:function(data){w=this.getDimensions();if(ie6){this.dialog.iframe=$('<iframe src="javascript:false;"/>').css($.extend(this.opts.iframeCss,{display:'none',opacity:0,position:'fixed',height:w[0],width:w[1],zIndex:this.opts.zIndex,top:0,left:0})).appendTo(this.opts.appendTo);}this.dialog.overlay=$('<div/>').attr('id',this.opts.overlayId).addClass('simplemodal-overlay').css($.extend(this.opts.overlayCss,{display:'none',opacity:this.opts.opacity/100,height:w[0],width:w[1],position:'fixed',left:0,top:0,zIndex:this.opts.zIndex+1})).appendTo(this.opts.appendTo);this.dialog.container=$('<div/>').attr('id',this.opts.containerId).addClass('simplemodal-container').css($.extend(this.opts.containerCss,{display:'none',position:'fixed',zIndex:this.opts.zIndex+2})).append(this.opts.close&&this.opts.closeHTML?$(this.opts.closeHTML).addClass(this.opts.closeClass):'').appendTo(this.opts.appendTo);this.dialog.wrap=$('<div/>').attr('tabIndex',-1).addClass('simplemodal-wrap').css({height:'100%',outline:0,width:'100%'}).appendTo(this.dialog.container);this.dialog.data=data.attr('id',data.attr('id')||this.opts.dataId).addClass('simplemodal-data').css($.extend(this.opts.dataCss,{display:'none'}));data=null;this.setContainerDimensions();this.dialog.data.appendTo(this.dialog.wrap);if(ie6||ieQuirks){this.fixIE();}},bindEvents:function(){var self=this;$('.'+self.opts.closeClass).bind('click.simplemodal',function(e){e.preventDefault();self.close();});if(self.opts.close&&self.opts.overlayClose){self.dialog.overlay.bind('click.simplemodal',function(e){e.preventDefault();self.close();});}$(document).bind('keydown.simplemodal',function(e){if(self.opts.focus&&e.keyCode==9){self.watchTab(e);}else if((self.opts.close&&self.opts.escClose)&&e.keyCode==27){e.preventDefault();self.close();}});$(window).bind('resize.simplemodal',function(){w=self.getDimensions();self.opts.autoResize?self.setContainerDimensions():self.setPosition();if(ie6||ieQuirks){self.fixIE();}else{self.dialog.iframe&&self.dialog.iframe.css({height:w[0],width:w[1]});self.dialog.overlay.css({height:w[0],width:w[1]});}});},unbindEvents:function(){$('.'+this.opts.closeClass).unbind('click.simplemodal');$(document).unbind('keydown.simplemodal');$(window).unbind('resize.simplemodal');this.dialog.overlay.unbind('click.simplemodal');},fixIE:function(){var p=this.opts.position;$.each([this.dialog.iframe||null,this.dialog.overlay,this.dialog.container],function(i,el){if(el){var bch='document.body.clientHeight',bcw='document.body.clientWidth',bsh='document.body.scrollHeight',bsl='document.body.scrollLeft',bst='document.body.scrollTop',bsw='document.body.scrollWidth',ch='document.documentElement.clientHeight',cw='document.documentElement.clientWidth',sl='document.documentElement.scrollLeft',st='document.documentElement.scrollTop',s=el[0].style;s.position='absolute';if(i<2){s.removeExpression('height');s.removeExpression('width');s.setExpression('height',''+bsh+' > '+bch+' ? '+bsh+' : '+bch+' + "px"');s.setExpression('width',''+bsw+' > '+bcw+' ? '+bsw+' : '+bcw+' + "px"');}else{var te,le;if(p&&p.constructor==Array){var top=p[0]?typeof p[0]=='number'?p[0].toString():p[0].replace(/px/,''):el.css('top').replace(/px/,'');te=top.indexOf('%')==-1?top+' + (t = '+st+' ? '+st+' : '+bst+') + "px"':parseInt(top.replace(/%/,''))+' * (('+ch+' || '+bch+') / 100) + (t = '+st+' ? '+st+' : '+bst+') + "px"';if(p[1]){var left=typeof p[1]=='number'?p[1].toString():p[1].replace(/px/,'');le=left.indexOf('%')==-1?left+' + (t = '+sl+' ? '+sl+' : '+bsl+') + "px"':parseInt(left.replace(/%/,''))+' * (('+cw+' || '+bcw+') / 100) + (t = '+sl+' ? '+sl+' : '+bsl+') + "px"';}}else{te='('+ch+' || '+bch+') / 2 - (this.offsetHeight / 2) + (t = '+st+' ? '+st+' : '+bst+') + "px"';le='('+cw+' || '+bcw+') / 2 - (this.offsetWidth / 2) + (t = '+sl+' ? '+sl+' : '+bsl+') + "px"';}s.removeExpression('top');s.removeExpression('left');s.setExpression('top',te);s.setExpression('left',le);}}});},focus:function(pos){var self=this,p=pos||'first';var input=$(':input:enabled:visible:'+p,self.dialog.wrap);input.length>0?input.focus():self.dialog.wrap.focus();},getDimensions:function(){var el=$(window);var h=$.browser.opera&&$.browser.version>'9.5'&&$.fn.jquery<='1.2.6'?document.documentElement['clientHeight']:$.browser.opera&&$.browser.version<'9.5'&&$.fn.jquery>'1.2.6'?window.innerHeight:el.height();return[h,el.width()];},getVal:function(v){return v=='auto'?0:parseInt(v.replace(/px/,''));},setContainerDimensions:function(){var ch=this.getVal(this.dialog.container.css('height')),cw=this.dialog.container.width(),dh=this.dialog.data.height(),dw=this.dialog.data.width();var mh=this.opts.maxHeight&&this.opts.maxHeight<w[0]?this.opts.maxHeight:w[0],mw=this.opts.maxWidth&&this.opts.maxWidth<w[1]?this.opts.maxWidth:w[1];if(!ch){if(!dh){ch=this.opts.minHeight;}else{if(dh>mh){ch=mh;}else if(dh<this.opts.minHeight){ch=this.opts.minHeight;}else{ch=dh;}}}else{ch=ch>mh?mh:ch;}if(!cw){if(!dw){cw=this.opts.minWidth;}else{if(dw>mw){cw=mw;}else if(dw<this.opts.minWidth){cw=this.opts.minWidth;}else{cw=dw;}}}else{cw=cw>mw?mw:cw;}this.dialog.container.css({height:ch,width:cw});if(dh>ch||dw>cw){this.dialog.wrap.css({overflow:'auto'});}this.setPosition();},setPosition:function(){var top,left,hc=(w[0]/2)-((this.dialog.container.height()||this.dialog.data.height())/2),vc=(w[1]/2)-((this.dialog.container.width()||this.dialog.data.width())/2);if(this.opts.position&&this.opts.position.constructor==Array){top=this.opts.position[0]||hc;left=this.opts.position[1]||vc;}else{top=hc;left=vc;}this.dialog.container.css({left:left,top:top});},watchTab:function(e){var self=this;if($(e.target).parents('.simplemodal-container').length>0){self.inputs=$(':input:enabled:visible:first, :input:enabled:visible:last',self.dialog.data);if(!e.shiftKey&&e.target==self.inputs[self.inputs.length-1]||e.shiftKey&&e.target==self.inputs[0]||self.inputs.length==0){e.preventDefault();var pos=e.shiftKey?'last':'first';setTimeout(function(){self.focus(pos);},10);}}else{e.preventDefault();setTimeout(function(){self.focus();},10);}},open:function(){this.dialog.iframe&&this.dialog.iframe.show();if($.isFunction(this.opts.onOpen)){this.opts.onOpen.apply(this,[this.dialog]);}else{this.dialog.overlay.show();this.dialog.container.show();this.dialog.data.show();}this.focus();this.bindEvents();},close:function(){if(!this.dialog.data){return false;}this.unbindEvents();if($.isFunction(this.opts.onClose)&&!this.occb){this.occb=true;this.opts.onClose.apply(this,[this.dialog]);}else{if(this.dialog.parentNode){if(this.opts.persist){this.dialog.data.hide().appendTo(this.dialog.parentNode);}else{this.dialog.data.hide().remove();this.dialog.orig.appendTo(this.dialog.parentNode);}}else{this.dialog.data.hide().remove();}this.dialog.container.hide().remove();this.dialog.overlay.hide().remove();this.dialog.iframe&&this.dialog.iframe.hide().remove();this.dialog={};}}};})(jQuery);