/*
	                /\ \      __                         
	 __  __     __  \ \ \/'\ /\_\    ___ ___      __     
	/\ \/\ \  /'__`\ \ \ , < \/\ \ /' __` __`\  /'__`\   
	\ \ \_\ \/\ \L\.\_\ \ \\`\\ \ \/\ \/\ \/\ \/\ \L\.\_ 
	 \/`____ \ \__/.\_\\ \_\ \_\ \_\ \_\ \_\ \_\ \__/.\_\
	  `/___/> \/__/\/_/ \/_/\/_/\/_/\/_/\/_/\/_/\/__/\/_/
	     /\___/                                          
	     \/__/

	Yakima Configurator
	Author: Nathan Searles, nathan.searles@pollinate.com
	Version: 2.0.3
	Updated: April 15th 2011
*/

// Set our own name space
var yakConfig;
var yakHost = (("https:" == document.location.protocol) ? "https://" : "http://");
// Find <head>
var head = document.getElementsByTagName('head')[0];

// Get Dealer Settings
var dealerId = dealerId,
	dealerCartUrl = dealerCartUrl,
	newWindow = newWindow,
    showWhispbar = showWhispbar,
	configdirString = '',
	configuratorMarkup;

// Load jQuery version 1.4.4 within the yakConfig namespace
var setjQuery = document.createElement('script');
setjQuery.type = 'text/javascript';
setjQuery.src = yakHost + 'yakima.com/dealers/js/jquery.min.js';
head.appendChild(setjQuery);

// Load SWFobject
var SwfObj = document.createElement('script');
SwfObj.type= 'text/javascript';
SwfObj.src= yakHost + 'yakima.com/dealers/js/swfobject.js';
head.appendChild(SwfObj);

// When required JavaScript is loaded
function jsReady() {
	// Set our own name space
	yakConfig = jQuery.noConflict(true);
	
	// Load Lightbox_me
	var lightboxMe = document.createElement('script');
	lightboxMe.type= 'text/javascript';
	lightboxMe.src= yakHost + 'yakima.com/dealers/js/jquery.lightbox_me.js';
	head.appendChild(lightboxMe);

	var configDir = yakHost + 'yakima.com/dealers/';
	if (showWhispbar) {
	   configDir = yakHost + 'yakima.com/dealers/whispbar/';
	}

    
    // Get Dealer ID
	if (dealerId) {
		configdirString = "&amp;dealerId=" + dealerId + "";
	}
	
	// On redirect, open new window
	if (newWindow == "false") {
		configdirString = "&amp;newWindow=no" + configdirString;
	}
	
	// Dealer has set a cart url
	if (dealerCartUrl) {
		configdirString = "&amp;dealerCartUrl=" + dealerCartUrl + configdirString;
	}
	
	// Lightbox markup
	configuratorMarkup = '<div id="yakima_configurator" style="display:none;">' +
	'	<div class="yakima_configurator_body">' +
	'		<a href="#" class="yakima_configurator_close">' +
	'			<img src="' + yakHost + 'yakima.com/dealers/yakima_configurator_close.png" title="close" class="yakima_configurator_close_image">' +
	'		</a>' +
	'		<div class="yakima_configurator_content">' +
	'			<div id="yakima_configurator_content">' +
	'				<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="600" height="600" id="myFlashConten">' +
	'					<param name="movie" value="' + yakHost + 'yakima.com/configurator/main.swf">' +
	'					<param name="play" value="true">' +
	'					<param name="loop" value="true">' +
	'					<param name="quality" value="best">' +
	'					<param name="scale" value="showall">' +
	'					<param value="#f7f7f7" name="bgcolor">' +
	'					<param name="wmode" value="transparent">' +
	'					<param name="allowscriptaccess" value="always">' +
	'					<param name="menu" value="false">' +
	'					<param name="flashvars" value="configdir=' + configDir + configdirString + '">' +
	'				<!--[if !IE]>-->' +
	'				<object type="application/x-shockwave-flash" data="' + yakHost + 'yakima.com/configurator/main.swf" width="600" height="600">' +
	'					<param name="play" value="true">' +
	'					<param name="loop" value="true">' +
	'					<param name="quality" value="best">' +
	'					<param name="scale" value="showall">' +
	'					<param value="#f7f7f7" name="bgcolor">' +
	'					<param name="wmode" value="transparent">' +
	'					<param name="allowscriptaccess" value="always">' +
	'					<param name="menu" value="false">' +
	'					<param name="flashvars" value="configdir=' + configDir + configdirString + '">' +
	'				<!--<![endif]-->' +
	'				<a href="' + yakHost + 'www.adobe.com/go/getflashplayer">' +
	'					<img src="' + yakHost + 'yakima.com/dealers/flash-required.gif" alt="Get Adobe Flash player">' +
	'				</a>' +
	'				<!--[if !IE]>-->' +
	'				</object>' +
	'				<!--<![endif]-->' +
	'				</object>' +
	'			</div>' +
	'		</div>' +
	'	</div>' +
	'</div>';

	// Load required css
	var configHead = document.getElementsByTagName('head')[0];
	var configStyle = document.createElement('link');
	configStyle.href = yakHost + 'yakima.com/dealers/yakima_configurator.css';
	configStyle.rel = 'stylesheet';
	configStyle.type = 'text/css';
	configStyle.media = 'screen';
	configHead.appendChild(configStyle);
	
	// Update href to link to hidden div
	yakConfig('.yakima_configurator').attr('href', '#yakima_configurator_content');

	// add click event for lightbox
	yakConfig('.yakima_configurator').live('click',function() {
	    yakConfig(configuratorMarkup).lightbox_me({
	        centered: true,
			closeSelector: '.yakima_configurator_close'
		});
	    return false;
	});
};
