
/*
 * Lord of the Rings Online section
 * http://chordian.net (II)
 *
 * The following jQuery plugins/snippets were used:
 *
 * tooltip:     Snippet by Alen Grakalic (http://cssglobe.com)
 * lightBox:    Plugin by Leandro Vieira Pinho (http://leandrovieira.com)
 * getUrlParam: Plugin by Mathias Bank (http://www.mathias-bank.de)
 * cookie:		Plugin by Klaus Hartl (https://github.com/carhartl/jquery-cookie)
 *
 */

var hubs = new Array();
var merchants = new Array();

var modeData = new Array();

modeData[M_BARTER] = {
	shell:			"bshell",
	divCont:		"blcont",
	areaID:			"#a",
	list:			"#blist",
	useMap:			"#bmap",
	rowHeight:		65,
	rowAdjust:		8,
	eyeX:			287,
	eyeY:			16,
	floater:		"#f",
	tooltipX:		["311px","357px","403px"],
	searchBorder:	"#98979d"
};
modeData[M_WIDE] = {
	shell:			"wshell",
	divCont:		"wlcont",
	areaID:			"#w",
	list:			"#wlist",
	useMap:			"#wmap",
	rowHeight:		50.74,
	rowAdjust:		5.4,
	eyeX:			287,
	eyeY:			16,
	floater:		"#w",
	tooltipX:		["312px","369px","430px"],
	searchBorder:	"#98979d"
};
modeData[M_SHOP] = {
	shell:			"shshell",
	divCont:		"shcont",
	areaID:			"#sh",
	list:			"#shlist",
	useMap:			"#shmap",
	rowHeight:		64,
	eyeX:			16,
	eyeY:			42,
	eyeTab:			183,
	searchBorder:	"#7c86a6"
};

var MAX_ROWS = 190;

var bToggle = 0;
var iToggle = 0;
var mToggle = 0;
var nToggle = 0;
var wToggle = 0;
var sToggle = 0;

var results = new Array();
var appArray = new Array();
var ax,ay,hub,hubNew,mode,name,npc,item,$onpc,$tnpc,timnpc,timwl,timwr,dtarget,dhref,dindex,alias,first,resPos,ftitle,starthub,tooltipPrefix;
var startnpc=0,startitem,iconAction=false,mapfold=false,npcValue,itemAmount,objLoad=O_ROW,$prevFloater=false,$newFloater=false;

var $ = jQuery.noConflict();

$(function(){ // DOM ready

	var areaInsert = "<map name='wmap'>";													// ***** Fill M_WIDE list with areas and floaters
	var floaterInsert = "";
	for (i=0; i<MAX_ROWS; i++) {
		areaInsert += "<area id='w"+i+"' shape='rectangle' coords='2,"+parseInt(i*modeData[M_WIDE].rowHeight+1)+",468,"+parseInt(i*modeData[M_WIDE].rowHeight+48)+"' href=''></area>";
		floaterInsert += "<div id='wa"+i+"' class='floater'></div><div id='wb"+i+"' class='floater'></div><div id='wc"+i+"' class='floater'></div>";
	}
	$("div.wlcont").append(areaInsert+"</map>"+floaterInsert);

	var xOffset = 10; // Tooltip distance
	var yOffset = 25;

	$(".floater").hover(function(e){														// ***** Hover mouse on "Items to Trade" icons
		ftitle = this.title;
		this.title= "";
		$("body").append("<p id='tooltip'><img src='"+ ftitle +"' alt='Tooltip' /></p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
	},
	function(){
		this.title = ftitle;
		$("#tooltip").remove();
	});

	$(".floater").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});

	$(".floater").click(function(){															// ***** Click "Items to Trade" icon
		$newFloater = $(this);
		$(modeData[mode].areaID+this.id.substring(2))
			.trigger("click",["float",ftitle,$(this).attr("info")]);
	});

	$("div.bshell area").click(function(e,msg,p1,p2){										// ***** Click row in barter list image map

		FloaterOutline();

		clearTimeout(timwr);

		$("#bsel" + ((bToggle%=2)?0:1))
			.fadeOut("fast");

		var line = $(this).attr("coords").split(',')[1] * 1;
		$("#bsel" + (bToggle++?1:0))
			.css("top",(line - 1) + "px")
			.fadeIn("fast");

		ShowItem(this,e,msg,p1,p2);

		return false; // Ignore href in area
	});

	$("div.wshell area").click(function(e,msg,p1,p2){										// ***** Click row in wide list image map

		FloaterOutline();

		clearTimeout(timwr);

		$("#wsel" + ((wToggle%=2)?0:1))
			.fadeOut("fast");

		var line = $(this).attr("coords").split(',')[1] * 1;
		$("#wsel" + (wToggle++?1:0))
			.css("top",(line - 1) + "px")
			.fadeIn("fast");

		ShowItem(this,e,msg,p1,p2);

		return false; // Ignore href in area
	});

	$("div.shshell area").click(function(e,msg,p1,p2){										// ***** Click row in shop list image map

		clearTimeout(timwr);

		$("#shsel" + ((sToggle%=2)?0:1))
			.fadeOut("fast");

		var col = $(this).attr("coords").split(',')[0] * 1;
		var line = $(this).attr("coords").split(',')[1] * 1;
		$("#shsel" + (sToggle++?1:0))
			.css("left",(col - 1) + "px")
			.css("top",(line - 2) + "px")
			.fadeIn("fast");

		ShowItem(this,e,msg,p1,p2);

		return false; // Ignore href in area
	});

	$("img.selector").click(function(e){													// ***** Click same barter or shop row again

		FloaterOutline();

		$("img.itemextra").attr("src","../images/dot.gif");
		clearTimeout(timwr);

		$("img.waitr").show();
		if (e.ctrlKey || e.shiftKey) {
			objLoad = O_APP;
			ClearIfMultiApp();
			$("#itemimage").attr("src",dtarget);
			if ($("#itemimage")[0].complete && $.browser.safari == true) {
				$("img.waitr").hide(); // Webkit loaded it from cache
				MultiApp(); // Force load of multiple images
			}
		} else {
			objLoad = O_ROW;
			ClearIfMultiApp();
			$("#itemimage").attr("src",dhref);
			if ($("#itemimage")[0].complete && $.browser.safari == true) {
				$("img.waitr").hide(); // Webkit loaded it from cache
			}
		}

		timwr = setTimeout(function(){
			$("img.waitr").hide();
		},3000);
	});

	$("#itemimage").load(function(){														// ***** When item/appearance image has loaded

		$("img.waitr").hide();
		$("#itemimage").show();

		if ($.browser.msie && $.browser.version=="6.0") {
			// Stupid IE6 hack for correct image size
			$("#itemimage")
				.css("width","0px")
				.css("height","0px")
				.css("width","auto")
				.css("height","auto");
		}

		$("img.itemextra").hide();

		if (objLoad != O_FLOAT) { $("div.appinfo").hide(); }

		if (objLoad == O_APP) {

			if (merchants[hub][npcValue].appInfo != undefined) {
				item = merchants[hub][npcValue].appInfo[dindex];
				if (item != "" && item != undefined) {
					$("div.appinfo").html(item).show();
				} else {
					$("div.appinfo").hide();
				}
			}

			MultiApp();
		}
	});

	for (i=0; i<9; i++) {																	// ***** Click blue/brown icon

		$("map[name=smap" + i + "] area").click(function(){
			ClickBarterIcon(this);
			iconAction = true;
			$("select.selnpc")
				.val(this.target)
				.trigger("change");
			return false;
		});
	}

	$("#umdots area").click(function(){														// ***** Click red hub dot on zone map

		$("#isel0,#isel1").hide();

		hubNew = this.target;
		if (mapfold == false) {
			$("#ssel"+hubNew).show(); // Below so still hidden
		}
		$("#ssel"+hub).fadeOut(600,function(){
			$(this).css("z-index","0");
			$("#ssel"+hubNew).css("z-index","1");
		});
		hub = hubNew;

		$("img.show").attr("src","../images/lotro/" + folder +"/thumb_" + hubs[hub].hubName + ".jpg");
		$(".mapline a").attr({
			href: "../images/lotro/" + folder +"/show_" + hubs[hub].hubName + ".jpg",
			title: hubs[hub].prettyName
		});

		$("select.selhub").val(hub);

		for (i=0; i<11; i++) {
			$("select.selnpc option[value=" + i + "]").remove();
		}
		for (i=0; i<hubs[hub].amount; i++) {
			$("select.selnpc").append("<option value=" + i + " >" + hubs[hub].npc[i] + "</option>");
		}

		$("#msel" + ((mToggle%=2)?0:1))
			.fadeOut("fast");

		ax = $(this).attr("coords").split(',')[0] * 1;
		ay = $(this).attr("coords").split(',')[1] * 1;
		$("#msel" + (mToggle++?1:0))
			.css("left",ax - 8 + "px")
			.css("top",ay - 8 + "px")
			.fadeIn("fast");

		$("select.selnpc")
			.val(startnpc)
			.trigger("change");

		startnpc = 0;
		return false;
	});

	$("select.selhub").change(function(){													// ***** Select item in HUB drop-down box
		$("#umdots area[target=" + this.value + "]").trigger("click");
	});

	$("select.selnpc").change(function(){													// ***** Select item in NPC drop-down box

		FloaterOutline();

		npcValue = this.value;

		if (first == true) {
			first = false;
		} else {
			$("div.firstcont").animate({height:"0", marginBottom:"0"},400,function(){ $(this).hide() });
		}

		objLoad = O_ROW;
		appArray = [];
		$("img.itemextra,div.appinfo").hide();

		clearTimeout(timnpc);
		clearTimeout(timwl);

		mode = merchants[hub][this.value].type;
		halfIfIe6 = ($.browser.msie && $.browser.version=="6.0") ? 2 : 1;
		switch (mode)
		{
			case M_BARTER: {
				$("div.shshell,div.wshell").hide();
				$("div.bshell").fadeIn("fast");
				$("div.ishell").css("margin-right", 22/halfIfIe6+"px"); break }
			case M_WIDE: {
				$("div.shshell,div.bshell").hide();
				$("div.wshell").fadeIn("fast");
				$("div.ishell").css("margin-right",10/halfIfIe6+"px"); break }
			case M_SHOP: {
				$("div.bshell,div.wshell").hide();
				$("div.shshell").fadeIn("fast");
				$("div.ishell").animate({marginRight:58/halfIfIe6+"px"},"slow"); break }
		}

		resPos = 0;
		results = [];
		$("div."+modeData[mode].shell+" input.isearch").val("").blur().css("background-color","#fff").css("border-color",modeData[mode].searchBorder);
		$("div.inext").css("background-position","-50px").css("cursor",""); // Disabled arrows
		$("div.iprev").css("background-position","-25px").css("cursor","");

		for (i=0; i<MAX_ROWS; i++) {
			$("#eye"+i).remove();
		}

		alias = merchants[hub][this.value].aliasFolder;
		if (alias == "") {
			alias = folder;
			npc = merchants[hub][this.value].name;
		} else {
			npc = merchants[hub][this.value].aliasName;
		}

		itemAmount = merchants[hub][this.value].amount;

		$("img.waitl").show();
		$(modeData[mode].list).css("visibility","hidden");
		$("div."+modeData[mode].divCont).scrollTop(0);
		$(modeData[mode].list)
			.unbind()
			.hide()
			.attr("src","../images/lotro/" + alias +"/list_" + npc + ".png")
			.load(function(){
				$(this).css("visibility","visible").fadeIn("fast");
				$("img.waitl").hide();
				$("div.eye").fadeIn("fast"); // Prepared below
				AutoShowRow();
			});
		timwl = setTimeout(function(){
			$("img.waitl").hide();
			$(modeData[mode].list).css("visibility","visible").show();
			$("div.eye").show();
			AutoShowRow();
		},hub==0?12000:3000); // Long delays in skirmish hub (old domain)

		$onpc = $("#npc" + ((nToggle%=2)?0:1));
		$tnpc = $("#npc" + (nToggle++?1:0));
		$tnpc
			.unbind()
			.attr("src","../images/lotro/" + folder +"/npc_" + merchants[hub][this.value].name + ".jpg")
			.load(function(){
				if (mapfold == false) {
					$tnpc.show(); // Behind so still hidden
					$onpc.fadeOut(600,function(){
						$onpc.css("z-index","0");
						$tnpc.css("z-index","1");
					});
				}
			});
		if ($tnpc[0].complete && $.browser.safari == true) {
			// Webkit loaded NPC from cache
			if (mapfold == false) {
				$tnpc.show(); // Behind so still hidden
				$onpc.fadeOut(600,function(){
					$onpc.css("z-index","0");
					$tnpc.css("z-index","1");
				});
			}
		}
		timnpc = setTimeout(function(){
			if (mapfold == false) {
				$tnpc.show(); // Behind so still hidden
				$onpc.fadeOut(600,function(){
					$onpc.css("z-index","0");
					$tnpc.css("z-index","1");
				});
			}
		},1500);

		$(modeData[mode].list).attr("usemap",itemAmount?modeData[mode].useMap:""); // No clicks

		for (i=0; i<itemAmount; i++) {
			item = merchants[hub][this.value].item[i];
			if (item.charAt(0) == "_") { name = "shared" } else { name = npc+"_" };
			$(modeData[mode].areaID+i).attr("href","../images/lotro/" + alias +"/item_" + name + item + ".png");

			item = merchants[hub][this.value].appearance[i];
			if (item == "" || item == undefined) {
				$(modeData[mode].areaID+i).attr("target","../images/lotro/app_none.png");
			} else {
				if (item.indexOf("http://") == -1) {
					item = "../images/lotro/" + alias +"/app_" + item + ".jpg"; // Internal image
				}
				$(modeData[mode].areaID+i).attr("target",item);

				$("div."+modeData[mode].divCont).append("<div class='eye' id='eye"+i+"' title='Click here to see an appearance image for this item.'></div>");
				if (mode == M_SHOP) { // Handling left and right column
					$("#eye"+i).css("top",parseInt(i/2)*modeData[mode].rowHeight+modeData[mode].eyeY + "px")
						.css("left",modeData[mode].eyeX+(i%2?modeData[mode].eyeTab:0) + "px");
				} else {
					$("#eye"+i).css("top",i*modeData[mode].rowHeight+modeData[mode].eyeY + "px")
						.css("left",modeData[mode].eyeX + "px");
				}
			}

			if (mode == M_BARTER || mode == M_WIDE) {
				item = merchants[hub][this.value].tooltip[i*3];
				if (item.substr(0,5) == "item_") { tooltipPrefix = "/" } else { tooltipPrefix = "/tooltip_" };
				if (item == "") { $(modeData[mode].floater+"a"+i).hide() } else {
					$(modeData[mode].floater+"a"+i)
						.attr("title","../images/lotro/" + alias + tooltipPrefix + item + ".png")
						.attr("info",merchants[hub][this.value].toolInfo[i*3])
						.css("top",i*modeData[mode].rowHeight+modeData[mode].rowAdjust + "px")
						.css("left",modeData[mode].tooltipX[0])
						.show();
				}
				item = merchants[hub][this.value].tooltip[i*3+1];
				if (item.substr(0,5) == "item_") { tooltipPrefix = "/" } else { tooltipPrefix = "/tooltip_" };
				if (item == "") { $(modeData[mode].floater+"b"+i).hide() } else {
					$(modeData[mode].floater+"b"+i)
						.attr("title","../images/lotro/" + alias + tooltipPrefix + item + ".png")
						.attr("info",merchants[hub][this.value].toolInfo[i*3+1])
						.css("top",i*modeData[mode].rowHeight+modeData[mode].rowAdjust + "px")
						.css("left",modeData[mode].tooltipX[1])
						.show();
				}
				item = merchants[hub][this.value].tooltip[i*3+2];
				if (item.substr(0,5) == "item_") { tooltipPrefix = "/" } else { tooltipPrefix = "/tooltip_" };
				if (item == "") { $(modeData[mode].floater+"c"+i).hide() } else {
					$(modeData[mode].floater+"c"+i)
						.attr("title","../images/lotro/" + alias + tooltipPrefix + item + ".png")
						.attr("info",merchants[hub][this.value].toolInfo[i*3+2])
						.css("top",i*modeData[mode].rowHeight+modeData[mode].rowAdjust + "px")
						.css("left",modeData[mode].tooltipX[2])
						.show();
				}
			}
		}

		if ($(modeData[mode].list)[0].complete && $.browser.safari == true) {
			$(modeData[mode].list).css("visibility","visible").fadeIn("fast");
			$("img.waitl").hide(); // Webkit loaded the list from cache
			$("div.eye").fadeIn("fast");
		}

		if (mode == M_BARTER || mode == M_WIDE) {
			for (i; i<MAX_ROWS; i++) {
				$(modeData[mode].floater+"a"+i).hide();
				$(modeData[mode].floater+"b"+i).hide();
				$(modeData[mode].floater+"c"+i).hide();
			}
		} else {
			if (itemAmount%2) { // Uneven list
				$("div.patch")
					.css("top",((itemAmount-1)/2)*modeData[mode].rowHeight+"px")
					.show();
			} else {
				$("div.patch").hide();
			}
		}

		$("span.title").text(hubs[hub].npc[this.value] + " (" + hubs[hub].job[this.value] + ")");

		$("img.selector").hide();
		$("#itemimage").attr("src","../images/lotro/item_clickrow.png");

		if (merchants[hub][this.value].intro != undefined) {
			$("div.isintro")
				.html(merchants[hub][this.value].intro)
				.show();
		} else {
			$("div.isintro").hide();
		}

		if (iconAction==false) {
			ClickBarterIcon($("map[name=smap" + hub + "] area[target=" + this.value + "]"));
		} else {
			iconAction = false;
		}

		$("div.eye").click(function(){														// ***** Click an "eye" icon (inside)
			$(modeData[mode].areaID+this.id.substring(3))
				.trigger("click",["app"]);
		});

		$("div.eye").hover(function(){														// ***** Hover on an "eye" icon (inside)
			$(this).css("background","url(../images/lotro/app_available_focus.png) no-repeat");
		},
		function(){
			$(this).css("background","url(../images/lotro/app_available.png) no-repeat");
		});

	});

	$("img.show").hover(function(){															// ***** Hover on thumbnail animates it
		$(this).filter(":not(:animated)").animate({
			width:'70px',
			height:'50px'},200);
	},
	function(){
		$(this).animate({
			width:'48px',
			height:'35px'},400);
	});

	$("div.mapline a").lightBox();															// ***** Thumbnail clicked shows lightbox

	$("img.mapfold").click(function(){														// ***** Click plus/minus corner button

		if (mapfold == false) {

			// Set cookie for 7 days
			$.cookie("chordian_mapfold","true",{expires:7});

			mapfold = true;
			$(this).attr("src","../images/icon_plus.gif");

			$("div.mapline span.hubdesc,div.mapline span.npcdesc,div.mapline select.selhub").fadeIn(500);
			$("div.mapline img.show").fadeOut("fast");
			$("div.mapline img.map").fadeOut(500);
			$("#ssel"+hub).fadeOut(500);
			$("#npc" + ((nToggle%2)?0:1)).fadeOut(500);

			$("div.mapline").animate({height:"22px"},400,function(){
				$("div.mapline img.show").hide(); // To be sure
			});

		} else {

			// Delete cookie
			$.cookie("chordian_mapfold",null);

			mapfold = false;
			$(this).attr("src","../images/icon_minus.gif");

			$("div.mapline span.hubdesc,div.mapline span.npcdesc").fadeOut(500);
			$("div.mapline select.selhub").fadeOut("fast");
			$("div.mapline img.map").fadeIn(500);
			$("#ssel"+hub).fadeIn(500);
			$("#npc" + ((nToggle%2)?0:1)).fadeIn(500).css("z-index","1");

			$("div.mapline").animate({height:"272px"},400,function(){
				$("div.mapline img.show").fadeIn(200);
			});
		}
	});

	$("input.isearch").keypress(function(e){												// ***** Hitting ENTER in local search box
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			$("div.ibutton").trigger("click");
			return false;
		}
	});

	$("div.ibutton").click(function(){														// ***** Clicking local search button

		var condensed,found=resPos=0;
		var words = [];
		results = [];

		var $input = $("div."+modeData[mode].shell+" input.isearch");
		if ($input.val() == "") { // Empty
			$input.blur()
				.css("background-color","#fff")
				.css("border-color",modeData[mode].searchBorder);
			return false;
		}

		words = $input.val()
			.replace(/[áéóúíÁÉÓÚÍ]/g,function(match){return translate[match]})
			.replace(/[^\w\s]/g,"")
			.toLowerCase();
		for (var a in adapt) {
			if (words.indexOf(a) != -1) { words+=" "+adapt[a]; }
		}
		words = words.split(" "); // Put search in array
		words = $.grep(words,function(w){return w.length>2});
		for (item=0; item<itemAmount; item++) {
			priority = 10;
			condensed = merchants[hub][npcValue].item[item];
			for (var w in words) {
				if (condensed.search(words[w]) != -1 && priority) {
					priority--;
				}
			}
			if (priority < 10) { // Lower item values must have leading zeroes for sort to work
				results[found++] = priority+"%"+((item.toString().length<3)?("000"+item.toString()).slice(-3):item);
			}
		}

		results.sort(); // Best is now first (priority value smallest)
		if (results.length < 1) { // Make input red
			$input.css("background-color","#faa").css("border-color","#98676d");
		} else {
			$input.css("background-color","#fff").css("border-color",modeData[mode].searchBorder);
			AutoShowRow(results[resPos].split("%")[1]*1);
		}

		if (results.length > 1) { // Several results
			$("div.inext")
				.css("background-position","-100px")			// Enabled
				.css("cursor","pointer");
		} else {
			$("div.inext")
				.css("background-position","-50px")				// Disabled
				.css("cursor","");
		}
		$("div.iprev")
			.css("background-position","-25px")					// Disabled
			.css("cursor","");

	});

	$("div.iprev").hover(function(){														// ***** Handling PREVIOUS search arrow
		if (results.length > 1 && resPos) {
			$(this).css("background-position","-125px");		// Hover
		}
	},
	function(){
		if (results.length > 1 && resPos) {
			$(this).css("background-position","-75px");			// Enabled
		}
	}).mousedown(function(){
		if (results.length > 1	&& resPos) {
			$(this).css("background-position","-175px");		// Pressed
		}
	}).mouseup(function(){
		if (results.length > 1 && resPos) {
			$(this).css("background-position","-125px");		// Hover
		}
	}).click(function(){
		if (resPos) {
			resPos--;
			AutoShowRow(results[resPos].split("%")[1]*1);
			if (resPos == 0) {
				$(this)
					.css("background-position","-25px")			// Disabled
					.css("cursor","");
			}
			$("div.inext")
				.css("background-position","-100px")			// Enabled
				.css("cursor","pointer");
		}
	});

	$("div.inext").hover(function(){														// ***** Handling NEXT search arrow
		if (results.length > 1 && resPos + 1 < results.length) {
			$(this).css("background-position","-150px");		// Hover
		}
	},
	function(){
		if (results.length > 1 && resPos + 1 < results.length) {
			$(this).css("background-position","-100px");		// Enabled
		}
	}).mousedown(function(){
		if (results.length > 1	&& resPos + 1 < results.length) {
			$(this).css("background-position","-200px");		// Pressed
		}
	}).mouseup(function(){
		if (results.length > 1 && resPos + 1 < results.length) {
			$(this).css("background-position","-150px");		// Hover
		}
	}).click(function(){
		if (resPos + 1 < results.length) {
			resPos++;
			AutoShowRow(results[resPos].split("%")[1]*1);
			if (resPos + 1 >= results.length) {
				$(this)
					.css("background-position","-50px")			// Disabled
					.css("cursor","");
			}
			$("div.iprev")
				.css("background-position","-75px")				// Enabled
				.css("cursor","pointer");
		}
	});

	$(window).load(function(){																// ***** When entire page has loaded

		$("#sselanim,#npcanim").hide();
		$("img.show")
			// Set size needed in PHP environment
			.css({width:'48px',height:'35px'})
			.show();
		$("select.selnpc").removeAttr("disabled");

		first = true;
		var paramhub = $(document).getUrlParam("hub");
		if (paramhub != null) {
			starthub = paramhub;
		}
		var paramnpc = $(document).getUrlParam("npc");
		if (paramnpc != null) {
			startnpc = paramnpc;
		}
		var paramitem = $(document).getUrlParam("item");
		if (paramitem != null) {
			startitem = paramitem;
		}

		if ($(document).getUrlParam("mapfold") == "true" || $.cookie("chordian_mapfold") == "true") {

			mapfold = true;
			$("img.mapfold").attr("src","../images/icon_plus.gif");

			$("div.mapline span.hubdesc,div.mapline span.npcdesc,div.mapline select.selhub").show();
			$("div.mapline img.show,div.mapline img.map").hide();
			$("#ssel"+hub).hide();
			$("#npc0,#npc1,#ssel99").hide();
			$("div.mapline").css("height","22px");
		}

		hub = 99; // Fades down "#ssel99" (blank)
		$("#umdots area[target="+starthub+"]")												// ***** Clicking red dot to initialize page
			.trigger("click");
	});

});



/***** Functions *****/

function repeat(str,num) {
	return new Array(num+1).join(str+",").split(",").slice(0,num);
	// repeat(T_MM+",,",18*3) means T_MM,"","" 18 times
}

function ClickBarterIcon(t) {

	$("#isel" +	((iToggle%=2)?0:1))
		.fadeOut("fast");

	ax = $(t).attr("coords").split(',')[0] * 1;
	ay = $(t).attr("coords").split(',')[1] * 1;
	$("#isel" + (iToggle++?1:0))
		.css("left",ax + 371 + "px")
		.css("top",ay - 1 + "px")
		.fadeIn("fast");
}

function ShowItem(t,e,msg,p1,p2) {

	dhref = t.href;
	dindex = t.id.replace(modeData[mode].areaID.substr(1),"");

	dtarget = t.target;
	if (dtarget.indexOf("&") != -1) {
		// Multiple appearance images
		appArray = dtarget.split('&');
		dtarget = appArray[0]+appArray[1];
	} else {
		appArray = [];
	}

	$("img.itemextra").attr("src","../images/dot.gif");
	$("div.firstcont")
		.css("height","0")
		.css("margin-bottom","0")
		.hide();
	$("div.isintro").hide();

	$("img.waitr").show();
	if (e.ctrlKey || e.shiftKey || msg == "app") {
		objLoad = O_APP;
		ClearIfMultiApp();
		$("#itemimage").attr("src",dtarget);
		if ($("#itemimage")[0].complete && $.browser.safari == true) {
			$("img.waitr").hide(); // Webkit loaded it from cache
			MultiApp(); // Force load of multiple images
		}
	} else if (msg == "float") {
		objLoad = O_FLOAT;
		ClearIfMultiApp();
		if (p2 != "" && p2 != undefined) {
			$("div.appinfo").html("<p><img src='"+p1+"' alt=''></p><div style='text-align:left;'>"+p2+"</div>").show();
		} else if (p2 == "") {
			$("div.appinfo").html("<p><img src='"+p1+"' alt=''></p><i>Information not available for this barter item.</i>").show();
		} else {
			$("div.appinfo").hide();
		}
		$("#itemimage").attr("src","../images/dot.gif");
		if ($("#itemimage")[0].complete && $.browser.safari == true) {
			$("img.waitr").hide(); // Webkit loaded it from cache
		}
	} else {
		objLoad = O_ROW;
		ClearIfMultiApp();
		$("#itemimage").attr("src",dhref);
		if ($("#itemimage")[0].complete && $.browser.safari == true) {
			$("img.waitr").hide(); // Webkit loaded it from cache
		}
	}

	timwr = setTimeout(function(){
		$("img.waitr").hide();
	},3000);
}

function AutoShowRow(index) {

	if (index != undefined) {
		startitem = index;
	}

	if (startitem != null) {
		if (isNaN(startitem)) {
			for (i=0; i<itemAmount; i++) {
				if (merchants[hub][npcValue].item[i].indexOf(startitem) != -1) {
					startitem = i;
					break;
				}
			}
			if (isNaN(startitem)) {
				startitem = null;
				return false;
			}
		} else if (startitem >= itemAmount) {
			startitem = itemAmount - 1;
		}
		$("div."+modeData[mode].divCont)
			.animate({scrollTop:(modeData[mode].rowHeight*(mode==M_SHOP?Math.floor(startitem/2):startitem))},400,function(){
				$(modeData[mode].areaID+startitem).trigger("click");
				startitem = null;
			});
	}
}

function MultiApp() {

	for (var a in appArray) { // Multiple images
		if (a > 1) {
			$("#itemextra"+(a-2))
				.attr("src",appArray[0]+appArray[a])
				.show();
		}
	}
}

function ClearIfMultiApp() {
	if ($("#itemextra0").is(":visible")) {
		$("div.appinfo,#itemimage").hide();
	}
}

function FloaterOutline() {
	if ($prevFloater != false) {
		$prevFloater.css("outline","");
	}
	if ($newFloater != false) {
		$newFloater.css("outline","2px solid #a4be4f");
		$prevFloater = $newFloater;
		$newFloater = false;
	}
}

