$().ready(function() 

{

	if(!flashembed.isSupported([10,0]))

	{

		$("#main .header").html("You need at least Flash 10!"); 

		$("#main .content").html("Your Flash version does not meet the requirements. <a href='http://get.adobe.com/flashplayer/' target='_blank'>Download</a> it or if avaible below, upgrade it.."); 

		$("#flash_updater").css({'height': '200px'});

		$("#flash_updater").flashembed("http://static.3darmory.com/swf/expressinstall.swf");

	} 

	$('ul.sf-menu').superfish();

	$('ul.sf-menu').supersubs();

	$("tr:nth-child(odd)").addClass("odd"); 		

	 $.getScript("http://www.3darmory.com/servers", function(){

			$("#realm").autocomplete(servers);

	 });

});

	

		

function form_query()

{	

	if( jQuery.trim($("#realm").val())!="" && jQuery.trim($("#name").val())!="")

		window.location="http://" + document.domain + "/" + $("#zone").val() + "/" + $("#realm").val() + "/" + $("#name").val();

}



function l()

{

	$("#toonlink").select();

}



function show_screenshots()

{

	$("#show_screenshot_scene").load("/toon_screenshots",{id:t});

}





/* -- model rendering functions -- */



function render_toon_model(params) // render a toon model based on supplied equipment and model parameters

{			

	var movie="http://static.wowhead.com/modelviewer/ModelView.swf"; 

	if($.browser.msie) movie+="?" + Math.floor(Math.random()*1000); // internet explorer do have some problems when refreshing the flash movie

																	// so on every refresh, append a random number so ie will think it's a new movie

	

	var r="";

	r+="<object id='wowhead' width='200px' height='200px' type='application/x-shockwave-flash' data='" + movie +"'><param name='quality' value='high'/>";

	r+="<param name='allowscriptaccess' value='always'/>";

	r+="<param name='menu' value='false'/>";	

	r+="<param value='transparent' name='wmode'>";

	r+="<param name='flashvars' value='model=" + params["model"] +"&modelType=16&ha=" + params["ha"] + "&hc=" +  params["hc"] + "&fa=" + params["fa"] + "&sk=" + params["sk"] + "&fh=" + params["fh"] + "&fc=" + params["fc"] + "&contentPath=http://static.wowhead.com/modelviewer/&blur=1&equipList=" + params["equipment_list"] + "'/>";

	r+="<param name='movie' value='" + movie + "'>";

	r+="</object>";

	$("#model_scene").html(r);

}



function read_toon_customization() // read supplied toon customization json data

{

	$.each(eq,function(attr,val) // loop all toon equipment

	{

		if (this.id) 

		{

			var ivis=1;	

			if(!rack) // if there is no toon customization info defined, toggle visibility of the item

				ivis=1;

			else // if there is customization info

			{

				var i=Math.pow(2,this.sid); // find the slot_id bitfield

				if(rack & i) // if in toon customization data, slot_id is set

					ivis=1;  // make item visible 

				else

					ivis=0; // else non-visible

			}

			

			r="<a href='http://www.wowhead.com/?item=" + this.id + "' target='_blank' class='rack_imgs' id='" + attr + "' ivis='"+ ivis + "'><img src='http://static.wowhead.com/images/icons/small/" + this.ic + ".jpg'></a> ";

			$("#rack").append(r); // append the img for item on customization rack

			if (!ivis) {

				$("#" + attr).fadeTo(1, 0.2); // if the item is non-visible, fadeout the image

			}

		}

	}

	);

	

	$.each(customization,function(attr,val) // loop all visual customization 

	{

		if( $("#" + attr + " option").size()>val)

			$("#" + attr)[0].selectedIndex=val;  // make visual customization data selected 

	}

	);

	render_toon_model(get_toon_customization()); // render the loaded customization, also this makes the toon model rendered after the first page load

}



function get_toon_customization() // reads a toon customization on toon query page

{

	var col_hc=$("#hc :selected").attr("style");

	$("#hc").attr({style:col_hc});

	

	var equipment_list="";

	

	// loop through all equipment which are selected to be rendered on model

	// ivis=1 => render the item

	// ivis=0 => do not render

	

	$("a[ivis='1']").each( function() 

	{

		id=eval("(eq." + this.id + ".id)"); // read item id

		if(id) // if item exists

		{

			sid=eval("(eq." + this.id + ".sid)"); // read slot id

			did=eval("(eq." + this.id + ".did)"); // read display id

			equipment_list +="," + sid + "," + did;

		}

  	});

	equipment_list=equipment_list.substr(1); // remove the last ,

		

	var params = [];

	params["model"]=eq.wmodel; // toon model

	params["equipment_list"]=equipment_list; 

	

	if ($("#ha").is('*')) params["ha"]=$("#ha :selected").text(); else params["ha"]=0;

	if ($("#hc").is('*')) params["hc"]=$("#hc")[0].selectedIndex; else params["hc"]=0;

	if ($("#fa").is('*')) params["fa"]=$("#fa :selected").text(); else params["fa"]=0;

	if ($("#sk").is('*')) params["sk"]=$("#sk :selected").text(); else params["sk"]=0;				

	if ($("#fh").is('*')) params["fh"]=$("#fh :selected").text(); else params["fh"]=0;				

	if ($("#fc").is('*')) params["fc"]=$("#fc :selected").text(); else params["fc"]=0;					

	

	build_custom_toon_link(params); // build the customized toon url 

	

	return params;

}



function toogle_item(item) // toggles an item's visibility

{

	// attribute ivis=1 => item visible, ivis=0 => non-visible

	if (item != null) 

	{

		if ($("#" + item.id).attr("ivis") == "1") // if item is already visible

		{

			$("#" + item.id).fadeTo(100, 0.2); // fadeout 

			$("#" + item.id).attr(

			{

				ivis: "0" // set it non-visible on model

			});

		}

		else // if it's non visible

		{

			$("#" + item.id).fadeTo(100, 1); // again show it as solid

			$("#" + item.id).attr(

			{

				ivis: "1" // set it visible on model

			});

		}

	}

	render_toon_model(get_toon_customization()); // as visiblity of an item have been toggled, re-render the 3D Model

	return false; // we need to return a false value in order to prevent wowhead link navigating the url

}



function build_custom_toon_link() // build a customized link for toon with item visiblity and face,skin,hair customization

{

	var visible_slots=1;

	visible_items=$("a[ivis='0']").size(); // find non-visible items

	if (visible_items) // if there is non visible items

	{

		$("a[ivis='1']").each(function(){ // loop all visible items 

			id = eval("(eq." + this.id + ".id)");

			if (id) {

				sid = eval("(eq." + this.id + ".sid)");

				did = eval("(eq." + this.id + ".did)");

				visible_slots = (visible_slots | Math.pow(2, sid)); // set the slot_id to 1 (visible)

			}

		});

	}

	else

		visible_slots=""; // if all items are visible, then no need to filter out slots

	

	// toon hair,skin,face customization

	var ha;

	var hc;

	var fa;

	var sk;

	var fh;

	var fc;

	

	if ($("#ha").is('*')) ha = $("#ha")[0].selectedIndex; else ha = 0;

	if ($("#hc").is('*')) hc = $("#hc")[0].selectedIndex; else hc = 0;

	if ($("#fa").is('*')) fa = $("#fa")[0].selectedIndex; else fa = 0;

	if ($("#sk").is('*')) sk = $("#sk")[0].selectedIndex; else sk = 0;	

	if ($("#fh").is('*')) fh = $("#fh")[0].selectedIndex; else fh = 0;	

	if ($("#fc").is('*')) fc = $("#fc")[0].selectedIndex; else fc = 0;

			

	var visual_customization="";

	if(ha)visual_customization+="_ha" + ha;

	if(hc)visual_customization+="_hc" + hc;

	if(fa)visual_customization+="_fa" + fa;

	if(sk)visual_customization+="_sk" + sk;

	if(fh)visual_customization+="_fh" + fh;

	if(fc)visual_customization+="_fc" + fc;

		

	var customized_link="";

	

	if(visible_slots!="") // if there's non-visible items

		customized_link+=visible_slots; // write the visible slots on customized link

		

	if (visual_customization != "") // if there are visual customization

	{

		if(visible_slots=="") // if all of the items are visible already

			customized_link+="0"; // put 0 as default value for show all equipped items

		customized_link += visual_customization; // add visual customization part on the custom link 

	}		

	$("#toonlink").val("http://www.3darmory.com/"+  zone +"/" + realm + "/" + name +  "/" + customized_link);

}



function render_screenshot()

{

	params=get_toon_customization();

	var r="";

	r+="<center><applet  width='800' height='600' archive='http://download.java.net/media/applet-launcher/applet-launcher.jar,http://download.java.net/media/jogl/builds/archive/jsr-231-webstart-current/jogl.jar,http://download.java.net/media/gluegen/webstart/gluegen-rt.jar,http://download.java.net/media/java3d/webstart/release/vecmath/latest/vecmath.jar,http://static.3darmory.com/jar/toonviewer.jar' code='org.jdesktop.applet.util.JNLPAppletLauncher' id='toonviewer'>";

	r+="<param name='codebase_lookup' value='false' /><param name='cache_option' value='no' /><param name='subapplet.classname' value='toonviewer.ToonViewerApplet' /><param name='subapplet.displayname' value='3DArmory Toonviewer' />";

	r+="<param name='progressbar' value='true' /><param name='jnlpNumExtensions' value='1' /><param name='jnlpExtension1' value='http://download.java.net/media/jogl/builds/archive/jsr-231-webstart-current/jogl.jnlp' />";

	r+="<param name='model' value='" + params["model"] + "' />";

	r+="<param name='bgColor' value='#121212' />";

	r+="<param name='equipList' value='" + params["equipment_list"] + "' />";

	r+="<param name='modelType' value='16' />";

	r+="<param name='ha' value='" + params["ha"] + "' />";

	r+="<param name='hc' value='" + params["hc"] + "' />";

	r+="<param name='fa' value='" + params["fa"] + "' />";

	r+="<param name='sk' value='" + params["sk"] + "' />";

	r+="<param name='fh' value='" + params["fh"] + "' />";

	r+="<param name='fc' value='" + params["fc"] + "' />";

	r+="<param name='t' value='" + t +"' /></applet></center>";

	$("#fancy_div").html(r);

}	

