var searchTip = "行政区划如：北京市   经纬度如：116.40287,39.92582";
var leftbottomTip ="如：116.40287,39.92582";
var righttopTip ="如：117.40287,40.92582";
// 定义全局变量
var header_h=60, footer_h=24, main_b_h = 2, main_r_b_w = 1;
var main_l_w, tool_menu_h, map_margin_t;
var $main, $mainL, $mainC, $mainR, $mapPanel,$toolMenu,$header_div;
var isHidd, isIE6=false;
var $searchTxtObj=null, $showAreaChk=null,$lbcoors=null,$rtcoors=null;

// 设置全局变量
function initGlobalVal() {
	if ($.browser.msie && $.browser.version == "6.0") { isIE6=true; }
	isHidd = false;
	$searchTxtObj = $("#search_text");
	$lbcoors = $("#lbcoors");
	$rtcoors = $("#rtcoors");
	$header_div = $("#header");
	$main = $("div.main");
	$mainL = $("div.main_l");
	$mainC = $("div.main_c");
	$mainR = $("div.main_r");
	$mapPanel = $("#mapPanel");
	$MC_SCR_ID = $("#full_screen_span");
	$MC_SCR_ID[0].isFull = 0;
	$MC_SCR_ID.bind("click", onFullScreen );
	$toolMenu = $("div.tool_menu");
	main_l_w = $mainL.width();
	tool_menu_h = $toolMenu.height();
	map_margin_t = tool_menu_h;
	$showAreaChk = $("#show_area_check");
	if (isIE6) { map_margin_t = 0; }
}
// 窗口尺寸变化响应处理函数
function resizeWin() {
	hiddLoc();
	var h = $(this).height();
	var w = $(this).width();
	var main_h = h - header_h - footer_h - main_b_h;
	var map_h = main_h - tool_menu_h;
	var map_w = w - main_l_w - main_r_b_w;
	if (isHidd) { map_w = w - main_r_b_w; }
	var mainL_w = main_l_w;
	if (isIE6) { mainL_w -= 3; }
	$mainC.css({height:main_h});
	$mainL.css({width:mainL_w, height:main_h});
	$mainR.css({width:map_w, height:main_h});
	$toolMenu.css({width:map_w});
	$("#mapPanel").css({width:map_w, height:map_h, marginTop:map_margin_t});
	map.checkResize();
}
// 点击事件处理器
function clickHandler() {
	$searchTxtObj.focus(function() {
		if ($(this).attr("title") == "") {
			this.value = "";
			$(this).attr("title", "请输入查询条件");
			$(this).css({color:"#000", fontSize:12});
		}
	});
	$searchTxtObj.blur(function() {
		if (this.value == "") {
			$(this).attr("title", "");
			this.value = searchTip;
			$(this).css({color:"#ccc", fontSize:12});
		}
	});
	
	$lbcoors.focus(function() {
		if ($(this).attr("title") == "") {
			this.value = "";
			$(this).attr("title", "请输入左下角经纬度");
			$(this).css({color:"#000", fontSize:12});
		}
	});
	$lbcoors.blur(function() {
		if (this.value == "") {
			$(this).attr("title", "");
			this.value = leftbottomTip;
			$(this).css({color:"#ccc", fontSize:12});
		}
	});
	
	$rtcoors.focus(function() {
		if ($(this).attr("title") == "") {
			this.value = "";
			$(this).attr("title", "请输入右上角经纬度");
			$(this).css({color:"#000", fontSize:12});
		}
	});
	$rtcoors.blur(function() {
		if (this.value == "") {
			$(this).attr("title", "");
			this.value = righttopTip;
			$(this).css({color:"#ccc", fontSize:12});
		}
	});
	
	
	// 检索城市列表
	$searchTxtObj.keydown(function(e) {
		if (e.keyCode == 13) { showResult(); }
	});
	$(".main_c").toggle(
		function() {
			isHidd = true;
			$(this).css({"background-image":"url('img/to_show.jpg')", left:0});
			if (isIE6) {
				$(".map_center").css({marginLeft:13});
			} else {
				$(".map_center").css({marginLeft:20});
			}
			$("div.main_l").addClass("hidden");
			$mainR.css({marginLeft:0});
			resizeWin();
		}, function() {
			isHidd = false;
			$(this).css({"background-image":"url('img/to_hidd.jpg')", left:332});
			$(".map_center").css({marginLeft:5});
			$("div.main_l").removeClass("hidden");
			if (isIE6) {
				$mainR.css({marginLeft:347});
			} else {
				$mainR.css({marginLeft:350});
			}
			resizeWin();
		}
	);
	$showAreaChk.click(function() {
		if (this.checked) {
			showAllPolygon();
		} else {
			hiddAllPolygon();
		}
		setShowAreaCheck(this.checked);
	});
}
var isLocShow = false;
function initAutoCss() {
	$(".map_center a.img_bg").hover(
		function() {
			$(this).css({background:"url('/img/list_2.png')"});
			$(this).parent().parent().addClass("li_hover");
		}, function() {
			if (!isLocShow) {
				$(this).css({background:"url('/img/list_1.png')"});
				$(this).parent().parent().addClass("li_hover");
			}
		}
	);
	$(".map_center li:gt(0)").hover(
		function() {
			$(this).addClass("li_hover");
			$(this).children().children("a.img_bg").css({background:"url('/img/list_1.png')"});
		}, function() {
			if (!isLocShow) {
				$(this).removeClass("li_hover");
				$(this).children().children("a.img_bg").css({background:"url('/img/list_0.png')"});
			}
		}
	);
	$("#close_info_ctrl").click(function() {
		var d = new Date();
		d.setDate(d.getDate()+1000*60*2);
		
		document.cookie = "BSEI_BUY_tip_time=1;expires="+d.toUTCString();
		$("#rolling_info_ctrl").addClass("hidden");
	});	
	if(document.cookie.indexOf("BSEI_BUY_tip_time=1")!=-1){
		$("#rolling_info_ctrl").addClass("hidden");
	};
}
function initConfig() {
	setShowAreaCheck(false);
	$searchTxtObj[0].value = searchTip;
	$searchTxtObj.css({color:"#ccc", fontSize:12, "font-family":"宋体"});
	$searchTxtObj.attr({title:""});
	
	$lbcoors[0].value = leftbottomTip;
	$lbcoors.css({color:"#ccc", fontSize:12, "font-family":"宋体",width:"150px",height:"16px",paddingTop:"2px"});
	$lbcoors.attr({title:""});
	
	$rtcoors[0].value = righttopTip;
	$rtcoors.css({color:"#ccc", fontSize:12, "font-family":"宋体",width:"150px",height:"16px",paddingTop:"2px"});
	$rtcoors.attr({title:""});
}
var showAreachecked;
function setShowAreaCheck(flag) {
	$showAreaChk[0].checked = flag;
	showAreachecked = flag;
}
function select() {$(this).addClass("list_hover");}
function unselect() {$(this).removeClass("list_hover");}

function initHome() {
	initGlobalVal();
	initAutoCss();
	clickHandler();
	onLoad();
	resizeWin();
	initConfig();
	window.onresize = resizeWin;
	
}
function onAllLoad(){
	initByGet();
}

var full_list = full_list||{};
function onFullScreen(e){
	if(!full_list.full_layer) full_list.full_layer = document.createElement("div");
	$(full_list.full_layer).css({
			position: "absolute",
			width: "100%",
			height: "100%",
			top: "0px",
			left: "0px"
		});
	if(this.isFull==0){
//		防止IE6下控件遮盖
//		$mainL.addClass("hidden");
		$header_div.addClass("hidden");
//		放入一个100%	div中
		full_list.full_layer.appendChild($("#tool_menu")[0]);
		full_list.full_layer.appendChild($mapPanel[0]);
		document.body.appendChild(full_list.full_layer);
//		计算height
		$("#tool_menu").width("100%");
		$mapPanel.width("100%");
		$mapPanel.height("100%");
		$mapPanel.height($mapPanel.height()-$("#tool_menu").height());
		map.checkResize();
		window.onresize = resetFullScreen;
//		更改图标
		$MC_SCR_ID.removeClass("full_screen");
		$MC_SCR_ID.addClass("un_full_screen");
		$MC_SCR_ID[0].title="还原";
		this.isFull=1;
	}else{
//		$mainL.removeClass("hidden");
		$header_div.removeClass("hidden");
//		注意顺序,如果以后在$mainR中添加div的话,可能会影响这个结构
		$mainR.append($("#tool_menu")[0]);
		$mainR.append($mapPanel[0]);
		window.onresize = resizeWin;
		resizeWin();
		document.body.removeChild(full_list.full_layer);
//		更改图标
		$MC_SCR_ID.addClass("full_screen");
		$MC_SCR_ID.removeClass("un_full_screen");
		$MC_SCR_ID[0].title="全屏显示";
		this.isFull=0;
	}
	resetMoreMapbtn(this.isFull==1);
}
function resetFullScreen() {
	$mapPanel.height("100%");
	$mapPanel.height($mapPanel.height()-$("#tool_menu").height());
	map.checkResize();
}
// 更多操作显示接口
var $MORE_MAP_BTN = null;
var moreMapbtnHid = true;

function moreMapbtn(obj) {
	if ($MORE_MAP_BTN == null) { $MORE_MAP_BTN = $("#more_btn"); }
	if (moreMapbtnHid) {
		$MORE_MAP_BTN.removeClass("hidden");
		if (obj) {
			curTop = $(obj).offset().top + $(obj).height() + 3;
			$MORE_MAP_BTN.css({top:curTop});
		}
	} else {
		$MORE_MAP_BTN.addClass("hidden");
	}
	moreMapbtnHid = !moreMapbtnHid;
}


function resetMoreMapbtn(isFull) {
	if ($MORE_MAP_BTN != null) {
		curTop = (isFull) ? (curTop-70) : (curTop+70);
		$MORE_MAP_BTN.css({top:curTop});
	}
}

function showDIV(showdiv,hiddendiv,tle)
{
	if(!showDIV.last){
		showDIV.last = $("#buy_title_data_show")[0];
	}
	if(showDIV.last){
		var f = showDIV.last.getAttribute("img_f");
		var b = showDIV.last.getAttribute("img_b");
		$(showDIV.last).removeClass(f);
		$(showDIV.last).addClass(b);
	}
	showDIV.last = tle;
	var f = showDIV.last.getAttribute("img_f");
	var b = showDIV.last.getAttribute("img_b");
	$(showDIV.last).removeClass(b);
	$(showDIV.last).addClass(f);
	
	showdiv.removeClass("hidden");
	hiddendiv.addClass("hidden");
	
	if(showDIV.last.id=="buy_title_data_show"){
		location.href = "#";
		map.setZoomLevels([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]);
		gutoubang.resetLength();
		document.title = "影像库_图享受";
	}else{
		
		location.href = "#type=buy";
		map.setZoomLevels([1,2,3,4,5,6,7,8,9,10,11,12,13,14]);
		if(map.getCurrentZoom()>14){map.zoomTo(14)}
		map.resetImages();
		gutoubang.resetLength();
		document.title = "影像订购_图享受";
	}
}	

function getQueryStringByHash(key) {
	var url = window.location.href;
	if (url.indexOf("#") > 0) {
		var params = url.substr(url.indexOf("#")+1).split("&");
		for (var i = 0; i < params.length; i++) {
			var index = params[i].indexOf("=");
			if (index > 0) {
				var name = params[i].substr(0,index);
				var value = params[i].substr(index+1);
				if(name == key) {
					return decodeURIComponent(value);
				}
			}
		}
	}
	return "";
}


function getQueryStringByGet(key) {
	var url = window.location.href;
	if (url.indexOf("?") > 0) {
		url = url.split("#")[0];
		var params = url.substr(url.indexOf("?")+1).split("&");
		for (var i = 0; i < params.length; i++) {
			var index = params[i].indexOf("=");
			if (index > 0) {
				var name = params[i].substr(0,index);
				var value = params[i].substr(index+1);
				if(name == key) {
					return decodeURIComponent(value);
				}
			}
		}
	}
	return "";
}