/* JavaScript Assests */

/* Google Api Framework Loading */
/*
google.load('jquery', '1.3.1');  
google.load('jqueryui', '1.5.3');
google.load('swfobject', '2.1');
*/

/*
	Using jQuery with Other Libraries:
	http://docs.jquery.com/Using_jQuery_with_Other_Libraries
*/
//var $j = jQuery;
var $j = jQuery.noConflict();

/**
 *	@desc		EXTERNAL LINKS - traps all links with rel="external" or class="external" or those that link to external sources
 *	@author		Stephen Blignault
 *	@version	2009/05/18
 */
function external_links()
{
	// turn rel="external" attributes into new window popups
	$j('a[rel^="external"]').click( function() {
		window.open( $j(this).attr('href') );
		return false;
	});
	// turn "external" classes into new window popups
	$j('a.external').click( function() {
		window.open( $j(this).attr('href') );
		return false;
	});
	// find all external links on a site and make new window popups
	$j("a[href^='http://']").not("a[href^='http://ownaship.co.nz']").not("a[href^='http://www.ownaship.co.nz']").not("a[href^='http://bookings.ownaship.co.nz']").not("a[href^='http://ownashipcms.corecms.co.nz']").not('a[rel^="external"]').not('a.external').click(function(){
		window.open(this.href,'external');
		return false;
	});
};

function enable_sliders()
{
	if ( $j('#flash').length )
	{
		$j('#flash').cycle(
			{
				fx:'fade', 
				speed:	3000,
				timeout:3000,
				random:1
			}
		);
	}
	if ( $j('#flashSlideshow').length )
	{
		$j("#flashSlideshow li").each(
			function(index) // return index for reference later on
			{
				var url = $j("#flashSlideshow li:eq("+index+") a").attr("href");
				$j("#flashSlideshow li:eq("+index+") a").remove();
				$j(this).wrapInner($j("<a href='"+url+"' title=''></a>"));
			}
		);
		
		$j('#flashSlideshow').cycle(
			{
				fx:'fade', 
				speed:	2000,
				timeout:4000,
				random:1
			}
		);
	}
}

function enable_swfobjects()
{
	if ( $j("#logo").length )
	{
		var flashvars = {};var params = {};var attributes = {};
		params.wmode  = "transparent";
		swfobject.embedSWF("http://www.ownaship.co.nz/logo.swf", "logo", "360", "120", "9.0.0", "/js/plugins/expressInstall.swf",flashvars,params,attributes);
	}
}

function boats_webapp()
{
	// Steve - collect clickthrough link, remove old link, wrap all inner html with clickthrough link
	if ( $j(".boatListings").length )
	{
		$j(".boatListings li").each(
			function(index) // return index for reference later on
			{
				var url = $j(".boatListings li:eq("+index+") a").attr("href");
				var ttl = $j(".boatListings li:eq("+index+") strong").text();
				$j(".boatListings li:eq("+index+") a").remove();
				$j(this).wrapInner($j("<a class='clearfix' href='"+url+"' title='"+ttl+"'></a>"));
			}
		);
	}
	
	if ( $j("#boatLeadin #gallery").length )
	{
		$j("#boatLeadin #gallery li").each(
			function(index) // return index for reference later on
			{
				if ( $j("#boatLeadin #gallery li:eq("+index+") img").attr("src") == "" )
				{
					$j("#boatLeadin #gallery li:eq("+index+") img").remove();
					$j("#boatLeadin #gallery li:eq("+index+") a").remove();
					$j("#boatLeadin #gallery li:eq("+index+")").css("display","none");
				}
			}
		);
	}
	
	// remove img tag from Boats Overview page
	if ( $j("ul.overview").length )
	{
		$j("ul.overview li").each(
			function(index) // return index for reference later on
			{
				$j("ul.overview li:eq("+index+") img").css("display","none");
			}
		);
	}
	
	// add a link to the img tag in Details page
	if ( $j("ul.details").length )
	{
		$j("ul.details li").each(
			function(index) // return index for reference later on
			{
				var url = $j("ul.details li:eq("+index+") a").attr("href");
				var ttl = $j("ul.details li:eq("+index+") a").text();
				
				$j("ul.details li:eq("+index+") img").wrap($j("<a href='"+url+"' title='"+ttl+"'></a>"));
			}
		);
	}
	
	// add boat name to boats enquiry form.
	// add a link to the img tag in Details page
	if ( $j("#boats-enquiry").length )
	{
		// CAT_Custom_107481 is the boat name field.
		if ( $j("#CAT_Custom_107481").val( $j("h1").text() ) )
		{
			$j("#CAT_Custom_107481").attr("readOnly",true);
		}
	}
	
}

function ownersComments_webapp()
{
	if ( $j("#testimonials").length )
	{
		$j("#testimonials li").each(
			function(index) // return index for reference later on
			{
				var url = $j("#testimonials li:eq("+index+") .more a").attr("href");
				$j("#testimonials li:eq("+index+") .more").css("display","none");
				
				$j("#testimonials li:eq("+index+") .intro").wrapInner($j("<a href='"+url+"' title='View more details'></a>"));
			}
		);
	}
}

function enable_nav_youarehere()
{
	if ( $j("#boatsDetails").length )
	{
		$j("#nav ul li:eq(3)").addClass("selected");
	}
	
	if ( $j("#faqDetails").length )
	{
		$j("#nav ul li:eq(2)").addClass("selected");
	}
}

function make_boats_dropdown()
{
	if ( $j("#boatsList").length && $j("#CAT_Custom_109150").length )
	{
		$j("#boatsList li").each(
			function(index) // return index for reference later on
			{
				var text = $j("#boatsList li:eq("+index+") a").text();
				$j("#CAT_Custom_109150").append('<option value="'+text+'">'+text+'</option>');
			}
		);
	}
	
	// now remove the redundant list
	if ( $j("#boatsList").length )
	{
		$j("#boatsList").css("display","none");
	}
}

$j(function()
{
	enable_sliders();
	enable_swfobjects();
	enable_nav_youarehere();
	external_links();
	
	make_boats_dropdown();
	
	boats_webapp();
	//ownersComments_webapp();
	
	if ( $j("#boatLeadin").length ) {
		$j("#boatLeadin a[rel^='lightbox']").prettyPhoto({theme:'facebook'});
	}
});
