// JavaScript Document
$.fn.formListener = function(todo) {
	var submited = false;
	if (!$("#ifr").length) $('<iframe id="ifr" name="ifr"></iframe>').appendTo($(this));
	if ($(this).attr("target") != "ifr") $(this).attr("target","ifr");
	//if ($(this).attr("action") != to) $(this).attr("action", to);
	if (!submited) $(this).submit();	
	var doc = document.all?$(document.frames('ifr').document.body).html():$(document.getElementById('ifr').contentWindow.document.body).html();
	
	if (doc == '') doc = null;
	if (doc != null){
		todo(doc);
	}else{
		//alert(document.getElementById("ifr").contentDocument.body.innerHTML);
		setTimeout(function(){$(this).formListener( todo );},100);
	}
}


$(document).ready(function(){
	
	if (!$("#tb_box").length) {
		$('<div id="tb_box"></div>').appendTo($("body"));			
	}
		
	$("#top-keyword").focus(function(){
		if($(this).val() == "商铺出租") $(this).val("");
	}).blur(function(){
		if ($(this).val() == '') $(this).val("商铺出租");
	});
	
	$("#select-category-list li").click(function(){
		$("#top-category").val($(this).attr("title"));
	});
	category = $("#top-category").val();
	$("#select-category-selected").text($("#select-category-list li[title='"+category+"']").text());
	
	$(".logo-bar-menu li a[class!=nav-m2-off menu-item]").each(function(id){
		if ((window.location.pathname == '/' || window.location.pathname == '/index.php') && (id == 0)){
			this_on = $(this).attr("class").replace("-off","-on");
			$(this).removeClass().addClass(this_on);
		}else if ((window.location.href.indexOf($(this).attr("href")) != -1) && $(this).attr("href") != 'http://localhost/' && $(this).attr("href") != 'http://localhost/index.php'){
			this_on = $(this).attr("class").replace("-off","-on");
			$(this).removeClass().addClass(this_on);
			
			//return(false);
		}
	 });
	
	
	$(document).click(function(){
		$("#select-category-list").hide();
	});
	
	$("#select-category-selected").toggle(
	function(){
		var left = $(this).position().left ;
		var top = $(this).position().top +$(this).height();
		$("#select-category-list").css({"top":top+'px',"left":left+'px'}).show();
	},function(){
		$("#select-category-list").hide();
	});
	
	$("#select-category-list li").hover(function(){
		$(this).css({"background-color":"#000000","color":"#FFFFFF"});
	}).mouseout(function(){
		$(this).css({"background-color":"#FFFFFF","color":"#000000"});
	}).click(function(){
		$("#select-category-selected").text($(this).text());
	});
	
	
	$(".global-box-contents ul[id^='tabs']").each(function(){
		//alert($(this).attr("id").split('-')[1]);
		if($(this).attr("id").split('-')[1] != 0) $(this).hide();
	})
	/*TABS*/
	$(".global-box-title-tabs a[name^='tabs']").mouseover(function(){
		tab = this.name.split("-");
		$(".global-box-title-tabs a[name^='"+tab[0]+"']").parent().removeClass("tabs-on");
		$(this).parent().addClass("tabs-on");

		$(".global-box-contents ul[id^='" + tab[0] + "']").hide();
		$(".global-box-contents ul[id='"+ this.name +"']").show();
	})
	
	//ajax tip info
	$.ajaxSetup({
		async: false
	}); 
	
	$("#tip_ajax_state").ajaxSend(function(){								   
		$(this).parent().fadeIn();
		$(this).css({color:'green'});		
		$(this).html('').html('正在发送数据...');
	});
	
	$("#tip_ajax_state").ajaxError(function(){
		$(this).css({color:'red'});
		$(this).html('').html('发送数据失败,请稍后再试...');
		

	});
	
	$("#tip_ajax_state").ajaxComplete(function(){
		var $tas = $(this);
		setTimeout(function(){
			$tas.parent().fadeOut();
		}, 500);									   
	});
	
	//input 样式
	$('input[type=text],input[type=password]').css({border:'1px solid #aaaaaa'});

	
	$('input[type=text],input[type=password]').focus(function(){
		$(this).css({border:'1px solid #000000'});
	});
	
	$('input[type=text],input[type=password]').blur(function(){
		$(this).css({border:'1px solid #aaaaaa'});
	});
		
	
	
});



var doResult = function(res){
	var data = eval('(' + res + ')');
		if (data.msg) {
		
		if (!$("#tb_box").length) {
			$('<div id="tb_box"></div>').appendTo($("body"));			
		}
		
		$("#tb_box").hide().empty().html('<p class="msg_right">' + data.msg + '</p> <p><a href="javascript:void(0)" id="tb_close" onclick="tb_remove()">Close</a></p>');//data.msg
		
		tb_show('信息', "#TB_inline?height=90&width=320&inlineId=tb_box&modal=false" , null);
		$('#TB_closeWindowButton,#tb_close').unbind().click(function(){
			tb_remove();
			if (data.url) {
				if (data.url == 'reload'){
					window.location.reload();
				}else{
					window.location.href = data.url;
				}
			}
		});
		
	}
	
	
}



