var DialogOpen = false;
var useModal = false;
var worn = "";
var compare = "";
var openGemColor = "";
var gemColors = new Array("red","blue","yellow","purple","green","orange","meta","prismatic");
var gemString = "<p>Select Gem Color:</p><select onchange='showGems(this.value)' name='gemColorSelect'>";
socketing = "none";
for (var j=0;j<gemColors.length;j++)
{
	gemString += "<option value='" + gemColors[j] +  "'>" + gemColors[j].substr(0,1).toUpperCase() + gemColors[j].substr(1,gemColors[j].length-1) + "</option>";	
}
gemString += "</select>";
$(document).ready(function(){
	$("select[name=whichSlot]").val(1);
	$("#overlay").hide();
	$("#benefits").hide();
	$("#dialogTest").dialog({
                    autoOpen: false,
                    resizable: false,
                    draggable: true,
                    modal: false,
                    title: "",
                    width: 530,
                    height: 'auto',
                    close: onClose
                });
	jQuery(function() {
		jQuery("#suggest").suggest("model/ajaxSearch.php",{
		onSelect: function() {getItem(this.value)}});
	});
	
	for (var i=0;i<gemColors.length;i++)
	{
		$("#" + gemColors[i] + "Gems").hide();    
		getGems(gemColors[i]);
	}
});
function onClose()
{
	DialogOpen = false;
}
function getItem(item)
{
	var myurl = "model/ajaxItem.php";
	var modurl = "item=" + item;
	
	$.ajax({
			url: myurl,
			data: modurl,
			async: false,
			success: function(msg) {
				var x = $("select[name=whichSlot]").val();
				var replaceSTR = "<input class='details" + x + "'";				
				$("#results" + x).html(msg.replace(/<input/g,replaceSTR).replace(/<div/g,"<div id='benefits" + x + "'"));
				if (x == 1)
				{
					worn = item;
					$("select[name=whichSlot]").val(2);
				}
				else
					compare = item;
				
				$("#suggest").val("");
				$("#suggest").focus();
				var r1 = $("#results1").html().length;
				var r2 = $("#results2").html().length;
				if ( (r1 > 1) && (r2 > 1))
					Compare();		
			}
	});	
}
function Compare()
{
	detail1STR = "";
	comparedArr = "";
	$("#benefits").html('');
	$(".details2").each(function(){
		var foundName = 0;
		var name1 =$(this).attr("name");
		var value1 = $(this).attr("value");
		value1 = $.trim(value1);
		$(".details1").each(function(){
			var name2 =$(this).attr("name");			
			var value2 = $(this).attr("value");
			
			if (name2 == name1)
			{
				foundName =1;
				if (comparedArr.length > 0)
					comparedArr += ",";
				comparedArr += name2;
				
				var diff = value1 - value2;
				diff = diff.toFixed(1);
				diff = diff.replace(".0","");
				if (diff > 0)
					detail1STR += "<li style='color:green;'>+" + diff + " " + name1 + "</li>";
				else if (diff == 0)
				{	
					//Do Nothing
				}
				else
					detail1STR += "<li style='color:red;'>" + diff + " " + name1 + "</li>";
			}
			
		});
		
		if (!foundName)
		{
			detail1STR += "<li style='color:green;'>+" + value1 + " " + name1 + "</li>";	
		}
	});
	$(".details1").each(function(){
		var newName =$(this).attr("name");
		var newValue = $(this).attr("value");
		newValue = $.trim(newValue);
		if (comparedArr.indexOf(newName) == -1 && newValue != 0)
			detail1STR += "<li style='color:red;'>-" + newValue + " " + newName + "</li>";
	})
	
	detail1STR += "</ul>";
	
	if (detail1STR != "</ul>")
	{
		$("#dialogTest").html(detail1STR)
		$("#benefits").html("<fieldset><legend>Differences With Item 2:</legend>" + detail1STR + "</fieldset>")
		if (!useModal)
			$("#benefits").show();
	}
	if (!DialogOpen && useModal)
	{
		$("#dialogTest").dialog("open");
		DialogOpen = true;
	}
	if (worn.length > 0 && compare.length > 0)
		$("#linkTo").html("Link to this comparison: <a href=\"http://oneroguesjourney.com/itemCompare/?worn=" + worn + "&compare=" + compare + "\">http://oneroguesjourney.com/itemCompare/?worn=" + worn + "&compare=" + compare + "</a>");
	setSockets();
}
function switchModal()
{
	if ($("input[name=modalCheck]").attr("checked"))
	{
		useModal = true;
		$("#benefits").hide();
		if (!DialogOpen)
		{
			$("#dialogTest").dialog("open");
			DialogOpen = true;
		}
	}
	else
	{
		useModal = false;
		$("#benefits").show();
		if (DialogOpen)
		{
			$("#dialogTest").dialog("close");
			DialogOpen = false;
		}
	}		
	
}
function getGems(color)
{
	var myurl = "model/ajaxGems.php";
	var modurl = "color=" + color;
	
	$.ajax({
			url: myurl,
			data: modurl,
			success: function(msg) {
				$("#" + color + "Gems").html(gemString + $.trim(msg));
			}
	});	
}
function showGems(color)
{
	$("#" + openGemColor + "Gems").hide();	
	if (color != '')
	{
		$("select[name=gemColorSelect]").val(color);
		$("#" + color + "Gems").show();
		openGemColor = color;
	}
}
function setSockets()
{
	var x=1;
	$(".socket-yellow").each(function(){		
			if ($(this).attr("class").search(/socketed/) == -1)
			{
				$(this).unbind().click(function(){
					showGems('yellow');
					socketing = $(this);
				}).addClass("yellow" + x);
				x++;
			}				
	});
	x = 1;
	$(".socket-red").each(function(){
		if ($(this).attr("class").search(/socketed/) == -1)
		{
			$(this).unbind().click(function(){
				showGems('red');
				socketing = $(this);
			}).addClass("red" + x);
			x++;
		}
	});
	x = 1;
	$(".socket-blue").each(function(){
		if ($(this).attr("class").search(/socketed/) == -1)
		{
			$(this).unbind().click(function(){
				showGems('blue');
				socketing = $(this);
			}).addClass("blue" + x);
			
			x++;
		}
	});
	x = 1;
	$(".socket-meta").each(function(){
		if ($(this).attr("class").search(/socketed/) == -1)
		{
			$(this).unbind().click(function(){
				showGems('meta');
				socketing = $(this);
			}).addClass("meta" + x);
			x++;
		}
	});
}
function CheckForSocketBonus(adjust)
{
	var fullSocket = true;
	var addSuccess = false;
	$("#results" + adjust + " .socket-red").each(function(){
		if ($(this).attr("class").search(/socketed/) != -1)
		{
			//Check For Proper Socket
			var color = $(this).attr("gemColor");
			if ( !((color.search(/red/) != -1) || (color.search(/orange/) != -1) || (color.search(/purple/) != -1) || (color.search(/prismatic/) != -1))) 
				fullSocket = false;				
		}
		else
		{
			fullSocket = false;
		}
	});
	$("#results" + adjust + " .socket-blue").each(function(){
		if ($(this).attr("class").search(/socketed/) != -1)
		{
			//Check For Proper Socket
			var color = $(this).attr("gemColor");
			if ( !((color.search(/blue/) != -1) || (color.search(/green/) != -1) || (color.search(/purple/) != -1) || (color.search(/prismatic/) != -1))) 
				fullSocket = false;
		}
		else
		{
			fullSocket = false;
		}
	});
	$("#results" + adjust + " .socket-yellow").each(function(){
		if ($(this).attr("class").search(/socketed/) != -1)
		{
			//Check For Proper Socket
			var color = $(this).attr("gemColor");
			if ( !((color.search(/yellow/) != -1) || (color.search(/orange/) != -1) || (color.search(/green/) != -1) || (color.search(/prismatic/) != -1))) 
				fullSocket = false;
		}
		else
		{
			fullSocket = false;
		}
	});
	//alert(fullSocket);
	if (fullSocket)
	{
		$("#results" + adjust + " .q0").each(function(){
			if ($(this).html().search(/Socket Bonus/) != -1)
			{
				//alert("Adding Socket Bonus");
				var bonus = $(this).html().split("Bonus: ");
				bonus = bonus[1].split(" ");
				//alert(bonus.length);
				if (bonus.length > 2)
				{
					//alert("hi");
					for(var i=2;i<bonus.length;i++)
					{
						//alert(bonus[1]);
						bonus[1] += " " + bonus[i];
					}
				}
				$(".details" + adjust).each(function(){						
					if ($.trim($(this).attr("name").toLowerCase()) == $.trim((bonus[1].toLowerCase())))
					{
						$(this).val(parseInt($(this).val()) + parseInt(bonus[0].replace("+","")));
						addSuccess = true;
					}
				});
				if (!addSuccess)
				{
					alert("here" + bonus[1]);
					var newStat = "<input type=\"hidden\" class=\"details" + adjust + "\" name=\"" + $.trim((bonus[1].toLowerCase())) + "\" value=\"" + parseInt(bonus[0].replace("+","")) + "\"/>";							
					$("#results" + adjust).append(newStat);
				}
				addSuccess = false;
				$(this).addClass("boldSocket");
			}
		});
	}
	else
	{
		$("#results" + adjust + " .q0").each(function(){
			if ($(this).html().search(/Socket Bonus/) != -1)
			{
				if ($(this).attr("class").search(/boldSocket/) != -1)
				{
					//alert("Removing Socket Bonus");
					var bonus = $(this).html().split("Bonus: ");
					bonus = bonus[1].split(" ");
					//alert(bonus.length);
					if (bonus.length > 2)
					{
						//alert("hi");
						for(var i=2;i<bonus.length;i++)
						{
							//alert(bonus[1]);
							bonus[1] += " " + bonus[i];
						}
					}
					$(".details" + adjust).each(function(){						
						
						if ($.trim($(this).attr("name").toLowerCase()) == $.trim((bonus[1].toLowerCase())))
						{
							$(this).val(parseInt($(this).val()) - parseInt(bonus[0].replace("+","")));
						}
					});
					$(this).removeClass("boldSocket");	
				}				
			}
		});
	}
/*	$("#results" + adjust + " .socketed").each(function(){
		alert($(this).attr("class"));
	});*/
}	
function addGem(id,color)
{
	var myurl = "model/ajaxGemInfo.php";
	var modurl = "item=" + id;
	var addSuccess = false;
	$("#overlay").show()
	$("#messages").html("<img src='images/busy.gif'/> Gemming...");
	showGems('');
	$.ajax({
			url: myurl,
			data: modurl,
			async: true,
			success: function(msg) {
				msg = msg.replace("</span>","");
				var aResp = msg.split("--");
				var parentsOf = socketing.parents().map(function(){
					return this.id;
				}).get().join(", ");
				if (parentsOf.search(/results1/) != -1)
				{
					var adjust = 1;
				}
				else
				{
					var adjust = 2;
				}
				
				socketing.html("<a onclick=\"return false\" href=\"http://www.wowhead.com/?item=" + id + "\">" + aResp[0] + "</a>").addClass("socketed");
				socketing.attr("gemColor",color);
					for(var i=1;i<aResp.length;i++)
					{						
						$(".details" + adjust).each(function(){
							var holder = aResp[i].split("*");							
							if ($.trim($(this).attr("name").toLowerCase()) == $.trim((holder[1].toLowerCase())))
							{
								$(this).val(parseInt($(this).val()) + parseInt(holder[0].replace("+","")));
								addSuccess = true;
							}
						});
						if (!addSuccess)
						{
							var holder = aResp[i].split("*");
							var newStat = "<input type=\"hidden\" class=\"details" + adjust + "\" name=\"" + $.trim((holder[1].toLowerCase())) + "\" value=\"" + parseInt(holder[0].replace("+","")) + "\"/>";							
							$("#results" + adjust).append(newStat);
						}
						addSuccess = false;
					}					
				CheckForSocketBonus(adjust);
				Compare();				
				socketing.unbind().addClass("gem" + id).click(function(){
					removeGem(id, $(this));
				});						
				
				$("#overlay").hide();
				$("#messages").html('');
								
			}
			
	});
	
	return false;
}
function removeGem(id,which)
{
	var myurl = "model/ajaxGemInfo.php";
	var modurl = "item=" + id;
	$("#overlay").show()
	$("#messages").html("<img src='images/busy.gif'/> Removing Gem...");
	$.ajax({
			url: myurl,
			data: modurl,
			async: true,
			success: function(msg) {
				msg = msg.replace("</span>","");
				var aResp = msg.split("--");
				
				var parentsOf = which.parents().map(function(){
					return this.id;
				}).get().join(", ");
				if (parentsOf.search(/results1/) != -1)
				{
					var adjust = 1;
				}
				else
				{
					var adjust = 2;
				}
				which.removeClass("socketed")
				var holderClass = which.attr("class");
				if (holderClass.search(/yellow/) != -1)
				{
					which.html('Yellow Socket').removeClass("gem" + id);
				}
				else if (holderClass.search(/red/) != -1)
				{
					which.html('Red Socket').removeClass("gem" + id);
				}
				else if (holderClass.search(/blue/) != -1)
				{
					which.html('Blue Socket').removeClass("gem" + id);
				}
				else if (holderClass.search(/meta/) != -1)
				{
					which.html('Meta Socket').removeClass("gem" + id);
				}
				which.removeAttr("gemColor");
				
				
					for(var i=1;i<aResp.length;i++)
					{
						$(".details" + adjust).each(function(){
							var holder = aResp[i].split("*");
							
							if ($.trim($(this).attr("name").toLowerCase()) == $.trim((holder[1].toLowerCase())))
							{
								$(this).val(parseInt($(this).val()) - parseInt(holder[0].replace("+","")));
							}
						});
					}
					CheckForSocketBonus(adjust);
					Compare();
					
					$("#overlay").hide();
					$("#messages").html('');				
			}
	});	
	return false;
}

