// Host
var host = window.location.protocol + "//" + window.location.host;

// Resource Path
var domainResourcesPath = window.location.host.replace("www.", "");
domainResourcesPath = domainResourcesPath.replace("WWW.", "");
domainResourcesPath = host+"/resources/medtravel.com";

$(document).ready(function(){
	$("div#nav ul.sf-menu").superfish({
		delay: 600,
		animation: {opacity: 'show'},
		speed: 150,
		autoArrows: true,
		dropShadows: false
	});

	/* Treatment Search */
	$('form#form_treatment_search').submit(function () {
		var form = $(this);

		form.find('fieldset').fadeOut(150, function () {
			form.find('div.thanks').fadeIn(150);
		});
		//form.find('div.loading').show();

		setTimeout(function () {
			form.find('div.thanks').fadeOut(150, function () {
				form.find('fieldset').fadeIn(150);
			});
		}, 5000);

		$.post($(this).attr('action'), $(this).serialize(), function(data) {

		});

		return false;
	});

	// Cool field labels!
	$("form div.field_container input").focusin(function () {
		$(this).prev('label').fadeOut(100);
	});
	$("form div.field_container input").focusout(function () {
		if($(this).val().length == 0)
		{
			$(this).prev('label').fadeIn(100);
		}
	});
	$("form div.field_container select").focusin(function () {
		$(this).prev('label').fadeOut(100);
	});
	$("form div.field_container select").focusout(function () {
		if($(this).val().length == 0)
		{
			$(this).prev('label').fadeIn(100);
		}
	});

	/* Quick Registration */
	$('form#quick_reg').submit(function () {
		var qr_form = $(this);

		qr_form.find('fieldset').hide();
		qr_form.find('div.loading').fadeIn(50);
		
		$.post($(this).attr('action'), $(this).serialize(), function(data) {
			qr_form.find('div.loading').hide();
			
			if(data.errors.length > 0)
			{
				qr_form.find('fieldset').fadeIn(150);

				$.each(data.errors, function (i,j) {
					qr_form
							.find('input[name="'+j+'"]')
							.prev('label')
							.addClass('error');
					qr_form
							.find('input[name="'+j+'"]')
							.prev('label')
							.fadeIn(100);

					setTimeout(function () {
						qr_form
								.find('input[name="'+j+'"]')
								.prev('label')
								.removeClass('error');
						if(qr_form.find('input[name="'+j+'"]').val().length > 0)
						{
							qr_form
									.find('input[name="'+j+'"]')
									.prev('label')
									.fadeOut(100);
						}
					}, 4500);
				});
			}
			else
			{
				if (data.pixels.length > 0)
				{
					qr_form.append(data.pixels);
				}
				qr_form.find('fieldset').fadeOut(50, function () {
					qr_form.find('div.thanks').fadeIn(150);
				});
			}
		});

		return false;
	});
});

/**
 * Fix Flash z-index
 * @return
 */
function fix_flash()
{
	// loop through every embed tag on the site
	var embeds = document.getElementsByTagName('embed');
	for (i = 0; i < embeds.length; i++) {
		embed = embeds[i];
		var new_embed;
		// everything but Firefox & Konqueror
		if (embed.outerHTML) {
			var html = embed.outerHTML;
			// replace an existing wmode parameter
			if (html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
				new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i, "wmode='transparent'");
			// add a new wmode parameter
			else
				new_embed = html.replace(/<embed\s/i, "<embed wmode='transparent' ");
			// replace the old embed object with the fixed version
			embed.insertAdjacentHTML('beforeBegin', new_embed);
			embed.parentNode.removeChild(embed);
		} else {
			// cloneNode is buggy in some versions of Safari & Opera, but works fine in FF
			new_embed = embed.cloneNode(true);
			if (!new_embed.getAttribute('wmode') || new_embed.getAttribute('wmode').toLowerCase() == 'window')
				new_embed.setAttribute('wmode', 'transparent');
			embed.parentNode.replaceChild(new_embed, embed);
		}
	}
	// loop through every object tag on the site
	var objects = document.getElementsByTagName('object');
	for (i = 0; i < objects.length; i++) {
		object = objects[i];
		var new_object;
		// object is an IE specific tag so we can use outerHTML here
		if (object.outerHTML) {
			var html = object.outerHTML;
			// replace an existing wmode parameter
			if (html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
				new_object = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i, "<param name='wmode' value='transparent' />");
			// add a new wmode parameter
			else
				new_object = html.replace(/<\/object\>/i, "<param name='wmode' value='transparent' />\n</object>");
			// loop through each of the param tags
			var children = object.childNodes;
			for (j = 0; j < children.length; j++) {
				try {
					if (children[j] != null) {
						var theName = children[j].getAttribute('name');
						if (theName != null && theName.match(/flashvars/i)) {
							new_object = new_object.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i, "<param name='flashvars' value='" + children[j].getAttribute('value') + "' />");
						}
					}
				}
				catch (err) {
				}
			}
			// replace the old embed object with the fixed versiony
			object.insertAdjacentHTML('beforeBegin', new_object);
			object.parentNode.removeChild(object);
		}
	}
}

