$(document).ready(function()
{
	init();
	
	swfobject.embedSWF("./swf/malibu_marquee.swf", "marquee", "1038", "353", "9.0.0", "./swf/expressInstall.swf", {}, { wmode:"transparent" });
});

function init()
{
	window._uds_vbw_donotrepair = true;
	
	var input = $("input[type='text']")
		.addClass("idleField")
     	.focus(function() 
		{
     		$(this).removeClass("idleField").addClass("focusField");
  		    if (this.value == this.defaultValue)
			{ 
  		    	this.value = '';
			}
			else
			{
   				this.select();
   			}
  		})
		.blur(function() 
		{
  			$(this).removeClass("focusField").addClass("idleField");
  		    if ($.trim(this.value) == '')
			{
	    		this.value = (this.defaultValue ? this.defaultValue : '');
			}
  		});
	
	setupVideoPlayer();
	setupFlickrFeed();
	setupTwitterFeed(true);
	setupMarquee();
	setupBoatNav();
	setupModalWindows();
}

var currentDropdown;
function setupBoatNav()
{
	var dropdowns = $("#boatDropdowns").children();
	$(dropdowns).hide();
	
	var nav = $("#mainNav li");
	nav.mouseover(function(e){
		$(dropdowns).fadeOut(0);
		if(e.target !== nav[0])
		{
			for(var i = 1; i < nav.length; i ++)
			{
				if(e.target === nav[i])
				{
					$(dropdowns).stop();
					$(dropdowns[i-1]).fadeIn(0);
					currentDropdown = $(dropdowns[i-1]);
				}
			}
		}
	})
	.mouseout(function(e){
		$(dropdowns).delay(1000).fadeOut(0);
	});
	
	$(dropdowns).mouseover(function(e){
		
		$(dropdowns).stop(true);
		$(currentDropdown).fadeIn(0);
		
	})
	.mouseout(function(e){
		$(currentDropdown).fadeOut(0);
	});
	
	
}

function setupModalWindows(){
	
	$(".contact").fancybox({
		padding: 30,
		titleShow: false
	});
	
	$(".demo").fancybox({
		padding: 30,
		titleShow: false
	});
	
	$("#pricing").fancybox({
		padding: 30,
		titleShow: false
	});
	
	$("#brochure").fancybox({
		padding: 30,
		titleShow: false
	});
	
	$("#newsletter").fancybox({
		padding: 30,
		titleShow: false
	});
	
	$("#newsletterTop").fancybox({
		padding: 30,
		titleShow: false
	});
	
	$("#military_discount").fancybox({
		padding: 20,
		titleShow: false
	});
}

function setupMarquee()
{
	var marquee = $("#marquee");
	marquee.cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		timeout: 8000
	});
	
	marquee.click(function(){
		$(this).cycle("next");
	});
	
}


function setupVideoPlayer()
{
	function LoadVideoBar() 
	{
		var videoBar;
		var options = 
		{
		    largeResultSet : !true,
		    horizontal : true,
		    autoExecuteList : 
			{
		      cycleTime : GSvideoBar.CYCLE_TIME_MEDIUM,
		      cycleMode : GSvideoBar.CYCLE_MODE_LINEAR,
		      executeList : ["ytchannel:malibuMEDIA"]
		    }
		}
		
		videoBar = new GSvideoBar(document.getElementById("videoBar-bar"),
		                          GSvideoBar.PLAYER_ROOT_FLOATING,
		                          options);
	}
	// arrange for this function to be called during body.onload
	// event processing
	GSearch.setOnLoadCallback(LoadVideoBar);
}


function setupFlickrFeed()
{
	$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=14285097@N07&lang=en-us&format=json&jsoncallback=?", function(data)
	{
		$.each(data.items, function(i, item)
		{
			var bigImage = item.media.m.replace("_m.", ".");
			var smallImage = item.media.m.replace("_m.", "_s.");
			var mediumImage = item.media.m;
			var tag = $("<img/>");
			if( i >= 0 && i <= 4)
			{
				tag.attr("src", mediumImage)
					.attr("style", "margin: -10px 0px 0px -10px")
					.appendTo("#images")
					.wrap("<a rel='flickr' class='images' href='" + bigImage + "'></a>")
					.wrap("<div class='other_images' />");
			}
			else
			{
				tag.attr("src", mediumImage)
					.appendTo("#images")
					.wrap("<a rel='flickr' class='images' href='" + bigImage + "'></a>")
					.wrap("<div class='hidden_images' />");
			}
		});
		$(".images").fancybox({
			padding: 50
		});
		$("#images").wrap("<div class=images />");		
	});
	
}

function setupTwitterFeed(useTweet)
{
	if(useTweet)
	{
		$(".tweet").tweet({
			username: "malibuboats",
			join_text: "auto",
			avatar_size: 0,
			count: 4,
			auto_join_text_default: "", 
	     	auto_join_text_ed: "",
	        auto_join_text_ing: "",
	        auto_join_text_reply: "",
	        auto_join_text_url: "",
			loading_text: "loading tweets..."
		});
	}
}
