﻿$(function(){
	var mid = $("#logo").attr("name");
	Helper.initZone("local", "zone", "所有区域", "所有区域", 0);
	Helper.setTheme("theme", "所有主题", mid, -1);
	def.paged(1);
});

function def(){}
def.paged = function(pageIndex) {
	var pageSize = 15;
	var mid = $("#logo").attr("name");
	var p1 = parseInt($("#area").attr("name"));
	p1 = isNaN(p1) ? 0 : p1;
	var p2 = $.trim($("#key").attr("name"));
	var key = String.format("[State]=1 and [MemberID]='{0}'", mid);
	var hasParam = p2.length > 0;
	switch (p1) {
		case 0:
			key += hasParam ? String.format(" and [RouteZoneLocal]='{0}'", p2) : "";
			break;
		case -9:
			key += " and [RouteZoneArea]<0";
			break;
		case 9:
			key += " and [RouteZoneArea]>0";
			break;
		case -10:
			key += hasParam ? String.format(" and CHARINDEX('{0}',[Mode])>0", p2) : "";
			break;
		case 10:
			key += hasParam ? String.format(" and [Theme]='{0}'", p2) : " and [Theme]<>'无'";
			break;
		case -11:
			if (hasParam) {
				var keys = p2.split(" ");
				if (keys.length > 0) {key += " and (";}
				$.each(keys, function(i,n){
					if (n.length > 0) {
						if (i > 0) {key += " or ";}
						key += String.format("(CHARINDEX('{0}',[Name])>0 or CHARINDEX('{0}',[Theme])>0 or CHARINDEX('{0}',[RouteZoneLocal])>0 or CHARINDEX('{0}',[RouteZoneName])>0 or CHARINDEX('{0}',[RouteTheme])>0 or CHARINDEX('{0}',[RouteName])>0 or CHARINDEX('{0}',[RouteProv])>0 or CHARINDEX('{0}',[RouteCity])>0)", n);
					}
				});
				if (keys.length > 0) {key += ")";}
			}
			break;
		case 11:
			var v = parseInt($("#area").getSelectedValue());
			switch (v) {
				case 9:
					key += " and [RouteZoneArea]>0";
					break;
				case -9:
					key += " and [RouteZoneArea]<0";
					break;
			}
			if ($("#local").getSelectedValue() != "%") {
				key += String.format(" and [RouteZoneLocal]='{0}'", $("#local").getSelectedText());
			}
			if ($("#zone").getSelectedValue() != "%") {
				key += String.format(" and [RouteZoneName]='{0}'", $("#zone").getSelectedText());
			}
			if ($("#theme").getSelectedValue() != "%") {
				key += String.format(" and [Theme]='{0}'", $("#theme").getSelectedText());
			}
			
			v = $("#ap").getSelectedValue();
			if (v != "%") {
				if (v == "1") {key += " and [AdultPrice] between 0 and 300";}
				else if (v == "2") {key += " and [AdultPrice] between 301 and 600";}
				else if (v == "3") {key += " and [AdultPrice] between 601 and 1000";}
				else if (v == "4") {key += " and [AdultPrice] between 1001 and 1500";}
				else if (v == "5") {key += " and [AdultPrice] between 1501 and 3000";}
				else if (v == "6") {key += " and [AdultPrice] between 3001 and 5000";}
				else if (v == "7") {key += " and [AdultPrice] between 5001 and 10000";}
				else if (v == "8") {key += " and [AdultPrice] > 10000";}
			}
			
			v = $("#rd").getSelectedValue();
			if (v != "%") {
				if (v == "1") {key += " and [RouteDays]=1";}
				else if (v == "2") {key += " and [RouteDays]=2";}
				else if (v == "3") {key += " and [RouteDays]=3";}
				else if (v == "4") {key += " and [RouteDays]=4";}
				else if (v == "5") {key += " and [RouteDays]=5";}
				else if (v == "6") {key += " and [RouteDays]=6";}
				else if (v == "7") {key += " and [RouteDays]=7";}
				else if (v == "8") {key += " and [RouteDays] between 8 and 15";}
				else if (v == "9") {key += " and [RouteDays] > 15";}
			}
			v = $.trim($("#key").val());
			if (v.length > 0) {
				var keys = v.split(" ");
				if (keys.length > 0) {key += " and (";}
				$.each(keys, function(i,n){
					if (n.length > 0) {
						if (i > 0) {key += " or ";}
						key += String.format("(CHARINDEX('{0}',[Name])>0 or CHARINDEX('{0}',[Theme])>0 or CHARINDEX('{0}',[RouteZoneLocal])>0 or CHARINDEX('{0}',[RouteZoneName])>0 or CHARINDEX('{0}',[RouteTheme])>0 or CHARINDEX('{0}',[RouteName])>0 or CHARINDEX('{0}',[RouteProv])>0 or CHARINDEX('{0}',[RouteCity])>0)", n);
					}
				});
				if (keys.length > 0) {key += ")";}
			}
			break;
		default:
			key += hasParam ? String.format(" and [RouteZoneLocal]='{0}'", p2) : String.format(" and [RouteZoneArea]={0}", p1);
			break;
	}

	$.ajax({url:"/Route/RouteSalesBySearch", type:"POST", data:"pageIndex="+pageIndex + "&pageSize="+pageSize + "&where="+escape(key), success:function(d) {
		var items = $(d).find("item");
		var len = items.length;
		var result = $("#li1");
		result.empty();
		for (var i = 0; i < 5 && i < len; ++i) {
			var n = $(items[i]);
			result.append(String.format('<div class="tjbox1"><a href="/Shop/Route/{0}" title="{1}"><img style="padding:6px; border:1px solid #F0F0F0; margin:0 0 5px 0; background-color:#FFFFFF;" src="/up/route/{2}" width="120" height="90"/><p class="listbt">{1}</p></a><div class="redmoney2">¥{3}</div>', n.attr("id"), n.attr("name"), n.attr("img"), n.attr("ap")));
		}
		for (var i = 5; i < len; ++i) {
			var n = $(items[i]);
			result.append(String.format('<li class="listmidli"><div class="title"><a href="/Shop/Route/{0}">{1}</a></div><div class="redmoney2">¥{2}</div><img style="float:left; margin:10px 0 0 0;" src="/Content/shop/def/img/chutuanbtn.gif"/></li>', n.attr("id"), n.attr("name"), n.attr("ap")));
		}
		$("#page").html(Helper.paged(pageIndex, pageSize, $(d).find("count").text(), "def"));
	}});
}

function search() {
	$("#area").attr("name", 11);
	def.paged(1);
}