$(function() {
	var products = new Array();
	$("#item-table form[name^=form] input[type=hidden][name=item_id]").each(function(i) { products.push($(this).val()); });
	if ($("#realm_selector").find("option:selected").val() == '') {
		$("#item-table form[name^=form]").submit(function(event) {
			alert('Please select a realm first.');
			event.preventDefault();
		});
		$("#item-table form[name^=form] input[name=submit]").show();
		var prevRealm = ''; 
	} else { var prevRealm = $("#realm_selector").find("option:selected").text(); }
	$("#realm_selector").change(function() {
		if ($("#realm_selector").find("option:selected").val() != '') {
			$("#realm_selector_indicator").show();
			if (prevRealm != '') {
				var newRealm = $("#realm_selector").find("option:selected").text();
				var newRealmLinkName = newRealm.replace(/ /g,'-').toLowerCase();
				var prevRealmLinkName = prevRealm.replace(/ /g,'-').toLowerCase();
				var prevRealmList = $("a[href*="+prevRealmLinkName+"]");
				var prevRealmList2 = $("a[href*="+prevRealmLinkName+"]:contains('"+prevRealm+"'),p:contains('"+prevRealm+"')");
				var newRealmList = $("a[href*="+ newRealmLinkName +"]");
				var newRealmList2 = $("a[href*="+ newRealmLinkName +"]:contains('"+newRealm+"'),p:contains('"+newRealm+"')");
				prevRealmList.each(function (i) { $(this).attr("href", $(this).attr("href").replace(prevRealmLinkName, newRealmLinkName)); });
				prevRealmList2.each(function (i) { $(this).html($(this).html().replace(prevRealm, newRealm)); });
				newRealmList.each(function (i) { $(this).attr("href", $(this).attr("href").replace(newRealmLinkName, prevRealmLinkName)); });
				newRealmList2.each(function (i) { $(this).html($(this).html().replace(newRealm, prevRealm)); });
				prevRealm = newRealm;
			}
			$("#item-table form[name^=form]").unbind("submit").submit(function(event) {
				alert("Loading prices...");
				event.preventDefault();
			});
			$("#item-table span[id^=item_id]").empty()
			$("#item-table span[id^=item_price]").empty();
			var dataString = "to_realm="+$("#realm_selector").find("option:selected").val()+"&products_id="+products;
			$.ajax({  
				type: "POST",  
				url: "/includes/change_realm_price.php",  
				data: dataString, 
				dataType: "json", 
				cache: false,
				success: function(data) {
					$("#item-table form[name^=form]").unbind("submit").submit(function(event) {
						alert('Item is not available in this realm.');
						event.preventDefault();
					});
					$("#item-table form input[name=custom]").val(data.realm);
					for (p in data.products) {
						if (prevRealm != '') { // update old item links
							prevRealmList.each(function (i) { $(this).attr("href", $(this).attr("href").replace($("#form"+data.products[p].from_products_id+"_item_id").val(), data.products[p].to_products_id)); });
						}					
						$("#item_id"+data.products[p].from_products_id).text(data.products[p].to_products_id);						
						$("#item_price"+data.products[p].from_products_id).html(data.products[p].products_price);
						$("#form"+data.products[p].from_products_id+"_item_id").val(data.products[p].to_products_id);
						$("#form"+data.products[p].from_products_id).unbind("submit");						
					}
					$("#item-table span[id^=item_price]:empty").text("Not Available");
					$("#realm_selector_indicator").hide();
				}  
			});
		}
	});
});
