function preloadImg(img)
{
	var new_img = new Image();
	new_img.src = img;
}

function flashGen(swfName, code, width, height)
{
	return '<embed src="'+swfName+'" width="'+width+'" wmode="transparent" height="'+height+'" flashvars=\'val='+code+'\' quality="high" name="copy" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
}

$(document).ready(function()
{
	$("#mainmenu>li>a").click(function()
	{
		$(this).next().slideToggle("fast");
		if($(this).next("ul").length != 0)
			$(this).css(
			{
				//background:'url(img/arrow' + (/_down/.test($(this).css('background')) ? '' : '_down') + '.gif) left center no-repeat'
			});
		this.blur();
		return false;
	});
	
	$("div.menu img").each(function()
	{
		preloadImg(this.src.substr(0, this.src.lastIndexOf("."))+"_over.gif");
	});
	
	$("#main_content img[src*=gif]").each(function()
	{
		preloadImg(this.src.substr(0, this.src.lastIndexOf("."))+"_over.gif");
	});
	
	$("div.menu img").mouseover(function()
	{
		this.src = this.src.substr(0, this.src.lastIndexOf("."))+"_over.gif";
	});
	
	$("div.menu img").mouseout(function()
	{
		this.src = this.src.replace("_over", "");
	});
	
	$("#mainmenu a").click(function()
	{
		var href = this.href;
		if($(this).next("ul").length == 0)
		{
			$("#mainmenu a").each(function()
			{
				this.className = (this.href == href ? 'selected' : '');
			});
			$.ajax(
			{
				method: 'get',
				url: this.href,
				beforeSend: function()
				{
					$("#loadbar").css(
					{
						visibility: "visible"
					});
				},
				success: function(data)
				{
					$("#main_content").html(data);
					$("#loadbar").css(
					{
						visibility: "hidden"
					});
					//if(/layouts/.test(href) && $('div.layout_dialog').length != 0)
					/*$('div.layout_dialog[id!="about_dialog"]').dialog({
							autoOpen: false,
							modal: true,
							width: 600,
							buttons: {
							"Done": function() {
								$(this).dialog("close");
							},
							"Cancel": function() {
								$(this).dialog("close");
							}
						}
						});*/

						// Dialog Link
						/*$('a.copy').click(function(){
							var id = /_\d+/.exec(this.id).toString().replace('_', '');
							$("#dialog_"+id).dialog('open');
							return false;
						});*/
				},
				error: function(xhr, textStatus, errorThrown)
				{
					alert("Error: "+textStatus);
				}
			});
		}
		return false;
	});
	
	$("#about_dialog").dialog({
		title: "About Profile Pimp",
		autoOpen: false,
		modal: true,
		width: 393,
		position: "center",
		buttons: {  
		  "Done": function() {
			$(this).dialog("close");
		  }
		}  
	});
	
	$("#about_dialog").next().html($("#about_dialog").next().html() + "<span id='copyright'>Copyright &copy; 2009 W3i, LLC. All Rights Reserved</span>");
	$('button', $("#about_dialog").next()).click(function()
	{
		$("#about_dialog").dialog('close');
	});
	
	$('div.footer div a').click(function()
	{
		$("#about_dialog").dialog('open');
	})
});
