function check_email(e) 
{
	e = e||"";
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	
	for(i=0; i < e.length ;i++)
	{
		if(ok.indexOf(e.charAt(i))<0)
		{ 
			return (false);
		}
	}
	
	if (document.images)
	{
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two)) 
		{
			return (-1);
		}
	}
}

function storeOpenMenus()
{
	var opened_cats = new Array();
	$("div#right-column-menu ul:visible").each( function(){ opened_cats.push(this.id); } );
	
	$.cookie("opened_menus", opened_cats.join(",") , { height: '/' ,expires : 7 } );
}

function restoreOpenMenus()
{
	var cookies = $.cookie("opened_menus");
	var opened_cats = new Array();
	
	if( cookies && (opened_cats = cookies.split(",")).length > 0 )
	{
		for( i in opened_cats)
		{
			$("div#right-column-menu ul#"+opened_cats[i]).show();
		}
	}
}

$(
function()
{
	$(".nav")
	.superfish({
		delay     : 300,
		animation : { height: "show"  },
		speed     : "fast"
	})
	.find(">li[ul]")
		.mouseover(function(){
			$("ul", this).bgIframe({opacity:false});
		})
		.find("a")
			.focus(function(){
				$("ul", $(".nav>li[ul]")).bgIframe({opacity:false});
			});
	
	var body_width = $("body").width();
	
	var width = (body_width < $(".nav").width()) ? body_width : $(".nav").width();
	
	
	var topMenu = $(".nav > li > a");
	var totalWidth = 0;
	
	var paddingTop = "0px";
	var paddingBottom = "0px";
	//var classLi = "";
	//var classLink = "";
	
	topMenu.each( 
		function()
		{
			totalWidth += $(this).width();
			
			if ($.browser.safari)
			{
				if (paddingTop == "0px")
					paddingTop = $(this).css("padding-top");
				
				if (paddingBottom == "0px")
					paddingBottom = $(this).css("padding-bottom");
				
				//if (classLi == "")
					//classLi = $(this).parent().attr("class");
				
				//if (classLink == "")
					//classLink = $(this).attr("class");
			}
		}
	);
	
	// Equal Padding
	var pad = ( width - totalWidth ) / topMenu.length;
	
	var new_width_total = 0;
	var nav_count = 0;
	var beforeWidth = 0;
	
	$(".nav > li").each(
		function()
		{
			var new_width = Math.round($(this).children("a").width() + pad - 1);
			
			new_width_total += new_width;
			nav_count++;
			
			if ( $(this).parent().find(">li").size() == nav_count)
			{
				var add_width = width - new_width_total - nav_count + 1;
				new_width += add_width;
				new_width_total += add_width;
			}
			
			$(this).width( new_width );
			$(this).children("a").width( new_width );
			
			if ($.browser.safari)
			{
				$(this).children("a").css("padding-top", paddingTop);
				$(this).children("a").css("padding-bottom", paddingBottom);
				//$(this).attr("class", classLi);
				//$(this).children("a").attr("class", classLink);
			}
			
			if ($(this).children("ul").size())
			{
				var beforeWidthWithSub = $(this).children("ul").width() + beforeWidth;
				
				if (width < beforeWidthWithSub)
				{
					$(this).children("ul").css("left", $(this).width() - $(this).children("ul").width());
				}
			}
			
			beforeWidth += $(this).width();
		}
	);
	
	var featureMenu = $("div#content div#body div#right-column .feature");
	
	featureMenu.each( 
		function()
		{
			$(this)
				.mouseover( function(){ $(this).addClass("feature-hover"); } )
				.mouseout( function(){ $(this).removeClass("feature-hover"); } );
		}
	);
	
	var leftH1 = $('div#content div#body div#left-column div#left-column-header h1');
	
	leftH1.flash(
		{ 
			src: webroot +'fonts/'+sub_header_font+'.swf', 
			
			flashvars: { 
				css: '* { color: '+convertToHexColor( leftH1.css("color"))+'; }'
			},
			wmode : "transparent"
		},
		{ version: 8 },
		converToFlashText
	);
	
	var rightH1 = $('div#right-column h1');
	
	rightH1.flash(
		{ 
			src: webroot+'fonts/'+sub_header_font+'.swf', 
			
			flashvars: { 
				css: '* { color: '+convertToHexColor( rightH1.css("color") )+'; }'
			},
			wmode : "transparent"
		},
		{ version: 8 },
		converToFlashText
	);
	
	$("#EmailFormSubmit").ready( function() {
		$("#EmailFormRequestResult").find("a").attr("href", location.href);
	} );
	
	
	
	$("#EmailFormSubmit").click( function() {
		
		var errors = new Array();
		
		
		$("#EmailFormError").hide();
		
		function getFieldClearName(name)
		{
			return name.match(/EmailForm\[(.*)\]/)[1].replace("_", " ");
		}
		
		function getFieldName(obj)
		{
			return $.trim($($(obj).parent().parent().find("td").get(0)).html().replace(/^\*+|\:+$/g, ""));
		}
		
		$("#EmailForm").find(".requiredField").each( function() {
		
			if ($(this).attr("class").match(/requiredEmail/i))
			{
				if( !check_email( $(this).val() ) )
					errors.push("Please fill in " + getFieldName(this));
			}
			else if( !$(this).val() )
			{
				errors.push("Please fill in " + getFieldName(this));
			}
		});
		
		if (errors.length > 0)
		{
			$("#EmailFormError").show();
			$("#EmailFormError").html(errors.join("<br />"));
			
			$("#EmailFormError").fadeIn("slow");
			return;
		}
		
		$("#EmailFormError").html('');
		$("#EmailFormBox").hide();
		$("#EmailFormRequestProcessing").show();
		$("#EmailFormRequestResult").hide();
		
		var dateObj = new Date();
		var request_data = "";
		
		$("#EmailForm").find(".EmailFormData").each( function() {
				request_data += $(this).attr("name") + "=" + $(this).val() + "&";
			}
		);
		
		$.ajax({
			type: "POST",
			url: host + "/blog/email_send_to_friend"+'/'+$("#type").val(),
			data: request_data + "t=" + dateObj.getTime(),
			success: function(msg)
				{
					$("#EmailFormRequestProcessing").hide();
					$("#EmailFormRequestResult").show();
				}
		});
		return false;
	} );
	
	restoreOpenMenus();
	
	$("div#right-column-menu a.category-link").click( function(){ $(this).next("ul").slideToggle("fast" , storeOpenMenus );  return false; } );
	var leftColumn = $("div#left-column");
	
	if( parseInt(leftColumn.height()) < parseInt( leftColumn.css("min-height")) )
	{
		leftColumn.height( leftColumn.css("min-height") );
	}
	
	$("div.featureLink").children().each(function() { $(this).css("cursor", "pointer"); $(this).click( function() {  $(this).parent().click(); } ); });
	
	$("#EmailFormSubmit").ready( function() {
		$("#EmailFormRequestResult").find("a").attr("href", location.href);
	} );
	/*
	$(".image-link-line").each(
		function(){
			var max_h = 0;
			
			$(this).find(".image-link-item").each(
				function(){
					if (max_h < $(this).height()) max_h = $(this).height();
				}
			);
			$(this).find(".image-link-item").height(max_h);
		}
	);*/
}
);

function getUnavailableDates(item_id, people_num, days_num, date_start)
{
	var dateArray = new Array();
	
	var dateObj = new Date();
	
	$.ajax({
		type: "GET",
		url: host + "/booking/get_unavailable_dates_xml/" + item_id + "/" + people_num + "/" + days_num + "/" + date_start + "/" + dateObj.getTime(),
		data: "",
		async: false,
		success: function(xml)
			{
				var items = xml.getElementsByTagName('date');
				
				for (i = 0; i < items.length; i++)
				{
					var data = items[i];
					dateArray[i] = unescape(data.firstChild.data);
				}
			}
	});
	return dateArray;
}