// JavaScript Document

//var $j = jQuery;
var $j = jQuery.noConflict();
/*document.writeln('<script type="text/javascript" src="/js/jquery.cycle.lite.min.js"></script>')*/;
document.writeln('<script type="text/javascript" src="/js/jquery.cycle.min.js"></script>');


/**
 *	@desc		EXTERNAL LINKS - traps all links with rel="external" or class="external" or those that link to external sources
 *	@author		Stephen Blignault
 *	@version	2010/05/05
 */
function external_links( coreCMS, urlName )
{
	// 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://"+urlName+"']").not("a[href^='http://www."+urlName+"']").not("a[href^='http://"+coreCMS+"']").not('a[rel^="external"]').not("a[href^='http://businesscatalyst.com']").click(function(){
		window.open(this.href,'external');
		return false;
	});
};

/**
 *	@author		Stephen Blignault
 *	@version	2010/05/05
 *	@reference	http://code.google.com/p/swfobject/wiki/documentation
 */
function run_flash( filename, content, width, height )
{
	var flashvars = {};
	var params = {scale:"noscale",wmode:"transparent"};
	var attributes = {};
	
	if ( $j( "#"+content ).length )
	{
		swfobject.embedSWF( filename, content, width, height, "9.0.0", "/expressInstall.swf", flashvars, params, attributes );
	}
}

function productTable_routines()
{
	if ( $j("#productsTable").length )
	{
		var count = 0;
		var theURL = theTitle = "";
		$j("#productsTable tr").each( function()
		{
			theURL = theTitle = "";
			if ( $j("td.viewMore a", this).length )
			{
				theURL = $j("td.viewMore a", this).attr("href");
				theTitle = $j("td.prodName", this).text();
				$j("td.prodName", this).wrapInner( '<a href="'+theURL+'" title="'+theURL+'"></a>' );
			}
			count++;
		});
	}
}

function menuContext_routines()
{
	if ( $j("body.Herbicides").length )		$j("#subNav ul li:eq(0)").addClass("selected");
	if ( $j("body.Insecticides").length )	$j("#subNav ul li:eq(1)").addClass("selected");
	if ( $j("body.Adjuvants").length )		$j("#subNav ul li:eq(2)").addClass("selected");
	if ( $j("body.Fungicides").length )		$j("#subNav ul li:eq(3)").addClass("selected");
	if ( $j("body.Speciality").length )		$j("#subNav ul li:eq(4)").addClass("selected");
	if ( $j("body.Other").length )			$j("#subNav ul li:eq(5)").addClass("selected");
}

function bannerSliders()
{
	$j('#banner').cycle({
		timeout:8000
		,speed:2000
		,random:1
		,pause:1
	});
}

function pdfLinks()
{
	if ( $j("#pdfsLinks").length )
	{
		var theURL = "";
		$j("#pdfsLinks p").each( function()
		{
			theURL = "";
			if ( $j(".label", this).length )
			{
				theURL = $j(".label", this).attr("href");
				if ( theURL == "" )
				{
					$j(this).remove();
				}
			}
		});
		
	}
}

$j(function()
{
	external_links( "orioncp.fueldesign.co.nz", "www.orioncp.co.nz" );
	run_flash( "/flash_logo.swf", "flashLogo", "167", "176" );
	
	productTable_routines();
	menuContext_routines();
	bannerSliders();
	pdfLinks();
	
});
