﻿// tab切换
function tabswitch(c, config){
	this.config = config ? config : {start_delay:3000, delay:1500};
	this.container = $i(c);
	this.pause = false;
	this.nexttb = 1;
	this.tabs = this.container.getElementsByTagName('dt');
	var _this = this;
	if(this.tabs.length<1)this.tabs = this.container.getElementsByTagName('li');
	for(var i = 0; i < this.tabs.length; i++){
		var _ec = this.tabs[i].getElementsByTagName('span');
		if(_ec.length<1)_ec = this.tabs[i].getElementsByTagName('a');
		if(_ec.length<1){
			_ec = this.tabs[i]
		}else{
			_ec = _ec[0];
		}
		_ec.onmouseover = function(e) {
			_this.pause = true;
			var ev = !e ? window.event : e;
			_this.start(ev, false, null);
		};
		
		_ec.onmouseout = function() {
			_this.pause = false;
		};
		
		try{
			$i(this.tabs[i].id + '_body_1').onmouseover = function(){
				_this.pause = true;
			};
			
			$i(this.tabs[i].id + '_body_1').onmouseout = function(){
				_this.pause = false;
			};
		}catch(e){}
	}

	if ($i(c + '_sts')) {
		var _sts = $i(c + '_sts');
		var _step = _sts.getElementsByTagName('li');
		if(_step.length<1)_step = _sts.getElementsByTagName('div');
		_step[0].onclick = function() {
			if (_this.tabs[_this.tabs.length-1].className.indexOf('current') > -1) {
				_this.nexttb = _this.tabs.length + 1;
			};
			_this.nexttb = _this.nexttb - 2 < 1 ? _this.tabs.length : _this.nexttb - 2;
			//alert(_this.nexttb);
			_this.start(null, null, _this.nexttb);
		};
		
		_step[1].onclick = function() {
			_this.nexttb = _this.nexttb < 1 ? 1 : _this.nexttb;
			_this.start(null, null, _this.nexttb);
		};
	};
	
	this.start = function(e, r, n){
		if(_this.pause && !e)return;
		if(r){
			curr_tab = $i(_this.container.id + '_' + rand(4));
		}else{
			if(n){
				//alert(_this.container.id + '_' + _this.nexttb);
				curr_tab = $i(_this.container.id + '_' + _this.nexttb);
			}else{
				curr_tab = e.target ? e.target : e.srcElement;
				if(curr_tab.id=="")curr_tab = curr_tab.parentNode;
			}
		}
		
		var tb = curr_tab.id.split("_");
		for(var i = 0; i < _this.tabs.length; i++){
			if(_this.tabs[i]==curr_tab){
				_this.tabs[i].className="hot Selected current";
				try{
					//alert(_this.tabs[i].id);
					$i(_this.tabs[i].id + '_body_1').style.display = "block";
				}catch(e){}
			}else{
				_this.tabs[i].className="";
				try{
					$i(_this.tabs[i].id + '_body_1').style.display = "none";
				}catch(e){}
			}
		}
		_this.nexttb = parseInt(tb[tb.length-1]) >= _this.tabs.length ? 1 : parseInt(tb[tb.length-1]) + 1;
	};
}

$.fn.inputval = function(cs){
	var me=$(this);
	cs= cs || "";
	me.attr("ov", me.val());
	me.focus(function(){
		if($(this).val()==$(this).attr("ov"))$(this).val("");
		if(cs) $(this).addClass(cs);
	}).blur(function(){
		if($(this).val()=="")$(this).val($(this).attr("ov"));
		if(cs) $(this).removeClass(cs);
	});
};

if(/^(hangzhou|shanghai|beijing|shenzhen|guangzhou|nanjing|tianjin|chongqing|suzhou|nanjing|chengdu|fuzhou|xian|wuhan|changsha|dalian|qingdao|xiamen|shenyang|zhengzhou|ningbo|wenzhou|jinan|dongguan|lanzhou)\.quchiqu\.com$/.test(location.hostname)){
	var city = RegExp.$1;
	if(city!=$.cookie("lc")){
		//alert([RegExp.$1,$.cookie("lc")]);
		$.cookie("lc",city,{expires:90,domain:".quchiqu.com",path:"/"});
	}
}

var banners;
$(document).ready(function(){
	if ($i('banners')) {	
		banners = new tabswitch('banners', {});
		setInterval("banners.start(null, null, 1);", 4000);
	}
	var sfm = $(".ind_search_body form");
	if(sfm){
		function findValueCallback(event, data, formatted) {
			//$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
		}
		
		function formatItem(row) {
			return row[0] + " (<strong>id: " + row[1] + "</strong>)";
		}
		function formatResult(row) {
			return row[0].replace(/(<.+?>)/gi, '');
		}
		$(".ind_search_title li").click(function (){
			var cur = $(".ind_search_title li@[class=cur]");
			var me = $(this);
			if(me.text()!=cur.text()){
				cur.removeClass("cur");
				me.addClass("cur");
			}
			var prefix_search_remote_url = "/search/getsuggest?mask="+(me.attr("data")=="0"?15:63);
			$(".ind_search_body input").autocomplete(prefix_search_remote_url, {
				width: me.attr("width"),
				max: 10,
				highlight: false,
				selectFirst: false,
				scroll: true,
				scrollHeight: 300,
				formatItem: function(data, i, n, value) {
					return value.split(",")[2];
				},
				formatResult: function(data, value) {
					return value.split(",")[2];
				}
			});

		});
		$(".ind_search_title li@[class=cur]").click();
		sfm.find("a").click(function(){
			sfm.submit();
		});
		sfm.submit(function (){
			var st = $(".ind_search_title li@[class=cur]").attr("data");
			$(this).attr("action", st=="0"?"/search":"/search/coupon");
			var kw = sfm.find("input");
			kw.focus();
			kw.attr("value", kw.attr("value").replace(/^\s+|\s+$/g,""));
			kw.attr("name", st=="0"?"ab":"cp");
			return kw.attr("value")!="";
		});
		sfm.find("input").inputval("");

	}
});




