$(function() {	var active;
	$('.search input').focus(function() {
		this.value = '';
		this.style.color = "#423420";
	});

	$('.search input').blur(function() {
		if (this.value == '') {
			this.value = 'Search RuneVillage';
			this.style.color = "#555";
		}
	});

	$('input[type=text]').each(function() {
		$(this).addClass("input-text");
		$(this).hover(
			function() {
				$(this).addClass("input-text-hover");
			},
			function() {
				$(this).removeClass("input-text-hover");
			}
		);
		$(this).focus(function() {
			$(this).addClass("input-text-focus");
		});
		$(this).blur(function() {
			$(this).removeClass("input-text-focus");
		});
	});

	$('.sidemenu-items .sidemenu-list li ul.flyout').each(function() {
		$(this).next().addClass("expandable");
		$(this).parent().hover(function() {
			$(this).find('ul').show();
		}, function() {
			$(this).find('ul').hide();
		});
	});

	$('.expandcollapse').show().each(function() {
		var list = $(this).next().next();
		var button = $(this).children().children().get(0);
		if (cookieValueCheck('navigation', getCollapseValue(this))) {
			list.css('display', 'none');
			button.alt = '+';
			button.title = 'Expand';
			button.src = '/site/style/css/images/expandbutton.gif';
		}
	});
	$('.expandcollapse a').click(function() {
		var list = $(this).parent().next().next();
		var button = $(this).children().get(0);

		if (list.css('display') == 'none') {
			button.alt = '-';
			button.title = 'Collapse';
			button.src = '/site/style/css/images/collapsebutton.gif';
			$(list).slideDown();
			cookieValueRemove('navigation', getCollapseValue($(this).parent()));
		} else {
			button.alt = '+';
			button.title = 'Expand';
			button.src = '/site/style/css/images/expandbutton.gif';
			$(list).slideUp();
			cookieValueSet('navigation', getCollapseValue($(this).parent()));
		}
		return false;
	});			$('.news_expandcollapse a').click(function() {		var list = $(this).parent().next().next().children();		var button = $(this).children().get(0);		if (list.css('display') == 'none') {			button.alt = '';			button.title = '';			button.src = '';			$(list).slideDown();						active = $(list);			cookieValueRemove('main_news', getCollapseValue($(this).parent()));		} else {			button.alt = '+';			button.title = 'Expand';			button.src = '/site/style/css/images/expandbutton.gif';			$(list).slideUp();			cookieValueSet('main_news', getCollapseValue($(this).parent()));		}		return false;	});
	
	$('#selfclaim').click(function() {
		$('#otherAmnt').attr("checked","checked");
	});
});

function getCollapseValue(div) {
	var a = $(div).next().children().get(0);
	if ($(a).children().length == 0)
		return a.alt; // Favorite links doesn't have a link
	else
		return $(a).children().get(0).alt;
}

function bakeCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days*24*60*60*1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length, c.length);
		}
	}
	return null;
}

function cookieValueCheck(name, val) {
	var cookieVal = readCookie(name);
	if (cookieVal != null) {
		var ca = cookieVal.split('|');
		for (var i = 0; i < ca.length; i++)
			if (ca[i] == val) return true;
	}
	return false;
}

function cookieValueSet(name, val) {
	var cookieVal = readCookie(name);
	if (cookieVal != null) {
		cookieArr = cookieVal.split('|');
		cookieArr.push(val);
		cookieVal = cookieArr.join('|');
	} else {
		cookieVal = val;
	}
	bakeCookie(name, cookieVal, 120);
}

function cookieValueRemove(name, val) {
	var cookieVal = readCookie(name);
	if (cookieVal != null) {
		var ca = cookieVal.split('|');
		for (var i = 0; i < ca.length; i++)
			if (ca[i] == val) {
				ca.splice(i, 1);
				bakeCookie(name, ca.join('|'), 120);
				return;
			}
	}
}

function eatCookie(name) {
	bakeCookie(name, "", -1);
}


/******************************
* API: Add Legend to an Image *
******************************/
function RVImageLegend(img) {
	img.legend = this;
	this.img = $(img);
	this.categories = [];
	this.types = [];
	this.symbols = [];
	this.div = $('<div class="image-legend" style="display:none;"><div class="image-legend-content"><img src="/site/style/images/ImageMapLegendTop.png" alt="RV Maps" /></div></div>');
	this.content = $(this.div.children().get(0));
	this.img.before(this.div);
	this.img.load(function() {
		this.legend.display();
	});
	this.display();
}

RVImageLegend.prototype.display = function() {
	this.div.css({'display': 'block', 'margin-left': this.img.width() + 'px', 'height': IEHeight(this.img.height() - 2) + 'px'});
	this.content.css('height', IEHeight(this.img.height() - 6) + 'px');
}

RVImageLegend.prototype.addCategory = function(name) {
	this.categories[name] = new RVImageLegendCategory(this, name);
}

RVImageLegend.prototype.addType = function(category, name, opts) {
	this.types[name] = new RVImageLegendType(this, category, name, opts);
}

RVImageLegend.prototype.addSymbol = function(type, x, y, opts) {
	this.symbols[type].push(new RVImageLegendSymbol(this, type, x, y, opts));
}

function RVImageLegendCategory(legend, name) {
	this.legend = legend;
	this.name = name;
	this.div = $('<div><img src="/heading/0/10/cba86c/584f36/' + this.name.replace('/', '%2F') + '.gif" alt="' + this.name + '" /></div>').appendTo(legend.content);
}

function RVImageLegendType(legend, category, name, opts) {
	this.legend = legend;
	this.category = legend.categories[category];
	this.legend.symbols[name] = [];
	this.name = name;
	this.url = (opts && opts.url ? opts.url : 'undefined');
	this.link = $('<a href="javascript:void(0);"><img src="/site/images/dungeon-maps/symbols/' + this.name.replace(' ', '%20') + '.gif" alt="" /> ' + this.name + '</a>').hover(function() {
		$(this).css('background-color','#766949');
		for(var el in legend.symbols[name]) {
			var symbol = legend.symbols[name][el];
			symbol.div.addClass('imagemap-symbol-highlighted').css('z-index', '99').css('margin-left', Math.floor(symbol.x - symbol.div.width() / 2 - 2) + 'px').css('margin-top', Math.floor(symbol.y - symbol.div.height() / 2 - 2) + 'px');
		}
	}, function() {
		$(this).css('background-color','#584f36');
		for(var el in legend.symbols[name]) {
			var symbol = legend.symbols[name][el];
			symbol.div.removeClass('imagemap-symbol-highlighted').css('z-index', '5').css('margin-left', Math.floor(symbol.x - symbol.div.width() / 2) + 'px').css('margin-top', Math.floor(symbol.y - symbol.div.height() / 2) + 'px');
		}
	}).appendTo(this.category.div);
	$('<input type="checkbox" checked="checked" />').click(function() {
		if (this.checked)
			for (var el in legend.symbols[name])
				legend.symbols[name][el].div.show();
		else
			for (var el in legend.symbols[name])
				legend.symbols[name][el].div.hide();
	}).prependTo(this.link);
}

function RVImageLegendSymbol(legend, type, x, y, opts) {
	this.legend = legend;
	this.type = legend.types[type];
	this.name = (opts && opts.name ? opts.name : type);
	this.url = (opts && opts.url ? opts.url : (this.type.url == 'undefined' ? 'undefined' : this.type.url));
	this.x = x;
	this.y = y;
	this.div = $('<div class="imagemap-symbol"></div>').insertBefore(this.legend.img);
	this.img = $('<img src="/site/images/dungeon-maps/symbols/' + this.type.name.replace(' ', '%20') + '.gif" alt="' + this.name + '" title="' + this.name + '" />').load(function() {
		$($(this).parents('div').get(0)).css('margin-left', Math.floor(x - $(this).width() / 2) + 'px').css('margin-top', Math.floor(y - $(this).height() / 2) + 'px');
	});
	if (this.url != 'undefined')
		$('<a href="' + this.url + '"></a>').append(this.img).appendTo(this.div);
	else
		this.img.appendTo(this.div);
	this.div.css('margin-left', Math.floor(this.x - this.div.width() / 2) + 'px').css('margin-top', Math.floor(this.y - this.div.height() / 2) + 'px');
	this.div.hover(function() {
		$(this).addClass('imagemap-symbol-highlighted').css('z-index', '99').css('margin-left', Math.floor(parseInt($(this).css('margin-left')) - 2) + "px").css('margin-top', Math.floor(parseInt($(this).css('margin-top')) - 2) + "px");
	}, function() {
		$(this).removeClass('imagemap-symbol-highlighted').css('z-index', '5').css('margin-left', Math.floor(parseInt($(this).css('margin-left')) + 2) + "px").css('margin-top', Math.floor(parseInt($(this).css('margin-top')) + 2) + "px");
	});
}

function IEHeight(height) {
	if (navigator.appVersion.indexOf("MSIE") != -1)
		return height + 2;
	return height;
}
