// jQuery plugin workaround for Brand Essence bug on Snow Leopard/Safari 5/Flash 10.1.0
//
// v1.0 -- 08/25/2010 -- initial version.
// v1.1 -- 08/25/2010 -- Added style cursor:hand to anchor tag fo he_IL. 
//                       For some reason it wasn't using the right cursor in IE7 (lbell)
// v1.2 -- 08/26/2010 -- Added a a new config_file parameter (lbell)
// v1.3 -- 08/26/2010 -- Added OBJECT tag for IE support. (lbell)
// v1.4 -- 08/26/2010 -- Added a flashPromosOK check (lbell)
// v1.5 -- 08/28/2010 -- Added asset_server to static image (lbell)
// v1.6 -- 09/08/2010 -- For no flash and in US we link to www.realbusiness.com now.
// v1.7 -- 09/14/2010 -- Handle Israel differently for jQuery carousel plugin. (speterso)
// v1.3 -- 10/31/2010 -- Use nivoSlider when no flash (edhille)

(function($) {

	var self = this;

	$.fn.BrandEssence = function(params) {
		// Specify default settings here.
		var config = {
		};
 
		if (params) $.extend(config, params);

		// Iterate over the selected elements here.
		this.each(function () {

			var userAgent = navigator.userAgent;	
			var regex     = new RegExp('Mac OS X 10_6', 'i');

			if ( userAgent.match(regex) || !flashPromosOK() ) {
				if ( jQuery.browser.msie || jQuery('#be_html').html() == '' ) {
					jQuery(this).append('<a href="http://www.realbusiness.com/" target="_blank"><img border="0" width="740" height="372" src="' + config.assets_server + config.altimg + '"/></a>');
				} else {
					var be_data = {}
					be_data[config.firstjQslide] = 1;
					jQuery('#be_html').show();
					jQuery('#slider').nivoSlider({
						effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
						slices:15,
						animSpeed:500, //Slide transition speed
						pauseTime:6000,
						startSlide:0, //Set starting Slide (0 index)
						directionNav:false, //Next & Prev
						directionNavHide:true, //Only show on hover
						controlNav:true, //1,2,3...
						controlNavThumbs:false, //Use thumbnails for Control Nav
						controlNavThumbsFromRel:false, //Use image rel for thumbs
						controlNavThumbsSearch: '.jpg', //Replace this with...
						controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
						keyboardNav:true, //Use left & right arrows
						pauseOnHover:true, //Stop animation while hovering
						manualAdvance:false, //Force manual transitions
						captionOpacity:1, //Universal caption opacity
						beforeChange: function(slider){ 
							slider.find('p>div.banner').each(function(){
								var id = jQuery(this).attr('id'); 
								if ( typeof(be_data[id]) != 'undefined' ) {
									++be_data[id]
								} else {
									be_data[id] = 1
								}
							}) 
						},
						afterChange: function(){},
						slideshowEnd: function(){} //Triggers after all slides have been shown
					});
					jQuery(window).unload(function(){
						// Send clicks to third-party metrics site.
						jQuery.each(be_data, function(key,val){
							if ( typeof(xrx_hbx_proxy.xrxLid) === 'function' ) {
        						xrx_hbx_proxy.xrxLid(key);
							}
						})
					});
				}
			} else if ( config.country == 'Israel' ) {
				// Client is in Israel. Provide them with a carousel of static linked images.
				// Code moved to CORP::HomePage::BrandEssence.pm in order for jQuery plugin to work.
				jQuery(this).append('');
			} else if ( userAgent.match(regex) ) {
				// Client is using Snow Leopard (i.e. Mac OS 10.6). Provide them with a linked image.
				jQuery(this).append('<a href="http://realbusiness.com/" target="_blank"><img border="0" width="740" height="372" src="' + config.assets_server + config.altimg + '"/></a>');
			} else{
				// Otherwise provide them with the Flash object.
			    jQuery(this).append('<OBJECT classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://macromedia.com/cabs/swflash.cab#version=9,0,0,0" WIDTH="740" HEIGHT="372" id="ImageViewer_obj_media"><PARAM name=wmode value="opaque"><PARAM NAME=movie VALUE="' + config.assets_server + '/assets/motion/corporate/homepage_slides/ImageViewer.swf"><PARAM NAME=allowScriptAccess VALUE="always"><PARAM NAME=FlashVars VALUE="dataSource=' + config.assets_server + config.config_file + config.shortlang + '.xml"><PARAM NAME=quality VALUE=high><PARAM NAME=bgcolor VALUE=#FFFFFF><embed width="740" height="372" flashvars="dataSource=' + config.assets_server + config.config_file + config.shortlang + '.xml" autoplay="false" allowscriptaccess="always" wmode="opaque" bgcolor="#FFFFFF" pluginspage="http://www.adobe.com/go/getflashplayer" type="application/x-shockwave-flash" src="' + config.assets_server + '/assets/motion/corporate/homepage_slides/ImageViewer.swf?playlist=" style="display: block;" name="ImageViewer_obj_media" id="ImageViewer_obj_media"></embed></OBJECT>');
			}
		});
 
		return this;
	};

})(jQuery);

