
(function($) {

	if(typeof($isJsUnit)!="undefined") {
		if($isJsUnit == "true" && (parent.document.location + "").indexOf("testContainer") > 0) return;
	}
	
	$.fn.hero = function(options) 
	{
		var opts = $.extend({}, $.fn.hero.defaults, options);
		
		return this.each(function() 
		{
			var $hero = $(this);
			
			$hero.bind('hero:embedflash',function()
			{
				var _xml = '<?xml version="1.0" encoding="UTF-8"?><slideShow><slideDelay>'+opts.slideDelay+'</slideDelay>';
				
				_xml += '<cssPath>'+opts.heroCSS+'</cssPath>';
				
				$hero.find('> ul.tracking li').each(function()
				{
					_xml += '<'+$.trim(this.className)+'>'+$.trim($(this).text().replace('&','').replace('"',''))+'</'+$.trim(this.className)+'>';
				});

				$hero.find('> ul').not('.tracking').find('> li').each(function()
				{	
					var $li = $(this);

					_xml += '<imgBlock>';
					_xml += '<img>'+$.trim($li.find('span.swf:first').text())+'</img>';
					
					var _header = "";

					$li.find('span.headerLight').each(function()
					{
						_header += "<span class='headerLight'>"+$.trim($(this).text())+"</span>";
					});

					$li.find('span.headerBold').each(function()
					{
						_header += "<span class='headerBold'>"+$.trim($(this).text())+"</span>";
					});

					_xml += '<heading><![CDATA['+_header+']]></heading>';
					
					var _subhead = "";

					$li.find('span.subHead').each(function()
					{
						_subhead += "<span class='subHead'>"+$.trim($(this).text())+"</span>";
					});

					_xml += '<subHead><![CDATA['+_subhead+']]></subHead>';
					
					_xml += "<body><![CDATA[<span class='body'>"+$.trim($li.find('span.body').text())+"</span>]]></body>";
					_xml += "<btnTxt><![CDATA[<span class='button'>"+$.trim($li.find('span.button').text())+"</span>]]></btnTxt>";
					_xml += "<btnLink>"+$.trim($li.find('span.btnLink').text())+"</btnLink>";
					
					if($li.find('span.txtLink').length > 0)
					{
						_xml += "<txtLink><![CDATA[<span class='txtLink'>"+$.trim($li.find('span.txtLink').html())+"</span>]]></txtLink>";
						_xml += "<txtLinkUrl><![CDATA["+$.trim($li.find('span.txtLink a').attr('href'))+"]]></txtLinkUrl>";
					}
					else
					{
						_xml += '<txtLink></txtLink>';
						_xml += '<txtLinkUrl></txtLinkUrl>';
					}

					$li.find('> ul.tracking li').each(function()
					{
						_xml += '<'+$.trim(this.className)+'><![CDATA['+$.trim($(this).text().replace('&','').replace('"',''))+']]></'+$.trim(this.className)+'>';
					});

					_xml += '</imgBlock>';
				});

				if($.fn.flash.hasFlash("9.0.0"))
				{
					$('.hero-flash').flash({src:opts.heroShell,width:'100%',height:'100%',wmode:'opaque',flashvars:{dataFile:_xml+'</slideShow>'}});
				}
				else
				{
					$('.container').append('');
				}
			});

			$hero.bind('hero:step',function(event,step)
			{
				if(step == '+' || step == '-')
				{
					var _current =  $('div.hero-nav').find('li').index($('div.hero-nav').find('li.active').get(0));
					
					if(step == '-' && _current-1 > -1)
					{
						$hero.trigger('hero:step',[_current-1]);
					}

					if(step == '+' && _current+1 < $('div.hero-nav').find('li').length)
					{
						$hero.trigger('hero:step',[_current+1]);
					}
				}
				else
				{
					$('div.hero-flash div.hero-low-bandwidth').css({display:'none'}).filter(':eq('+step+')').css({display:'block'});
					$('div.hero-nav').find('li').css({backgroundPosition:'0 0'}).removeClass('active').filter(':eq('+step+')').css({backgroundPosition:'0 -13px'}).addClass('active');
				}
			});

			$hero.bind('hero:init',function()
			{
				var $container = $('<div class="hero-flash"></div>').prependTo('body');

				if($hero.find('div.hero-low-bandwidth').length > 0)
				{
					$container.append($hero.find('div.hero-low-bandwidth'));

					var $nav = $('<div class="hero-nav"><div><span class="list"><ul></ul></span><span class="left"></span><span class="right"></span></div></div>').appendTo($container);

					$nav.find('> div').css({width:($container.find('div.hero-low-bandwidth').length*35)+20});

					$container.find('div.hero-low-bandwidth').each(function(n)
					{
						$('<li></li>').appendTo($nav.find('ul')).click(function()
						{
							$hero.trigger('hero:step',[n]);
						}).hover(
							function() {$(this).not('.active').css({backgroundPosition:'0 -13px'});},
							function() {$(this).not('.active').css({backgroundPosition:'0 0'});}
						);
					});

					$nav.find('span.left').click(function()
					{
						$hero.trigger('hero:step',['-']);
					});

					$nav.find('span.right').click(function()
					{
						$hero.trigger('hero:step',['+']);
					});

					$container.find('h2').mafr();
					$container.find('.button').button();

					$hero.trigger('hero:step',[0]);
				}
				else
				{
					$hero.trigger('hero:embedflash');
				}

			}).trigger('hero:init');
		});
	};
	
	var _linkSwf = '';
	var _linkCss = '';
	
	if(document.location.href.indexOf('/ru/') != -1 || document.location.href.indexOf('/th/') != -1 || document.location.href.indexOf('/il/') != -1)
	{
		_linkSwf = '';
		_linkCss = '';

		/* 20090918 leadweb add */
		if(document.location.href.indexOf('/ru/') != -1) {
			$('div.right-rail').css({backgroundColor:'#ffffff'});
			if($.browser.msie && ($.browser.version < 7))
			{
				$('div.right-rail').css({border:'1px solid #ffffff'});
			}
		}
		/* //20090918 leadweb add */
	} 

	$.fn.hero.defaults = 
	{
		slideDelay: 10,
		heroShell:_linkSwf, // 2009-11-09 leadweb modify 
		heroCSS:_linkCss, // 2009-11-09 leadweb modify
		downloadFlashUrl: 'http://get.adobe.com/flashplayer/'
	};

	$.fn.rail = function(options) 
	{
		var opts = $.extend({}, $.fn.rail.defaults, options);
		
		return this.each(function() 
		{
			var $rail = $(this);
			var $container = $('div.container');

			$container.after('<div class="rr-shutter"><span class="toggle"></span></div>');

			var $close = $('.rr-shutter span.toggle');

			if($.browser.msie && ($.browser.version < 7))
			{
				$('.rr-shutter').addClass('rr-shutter-ie6');
			}
			else
			{
				$('.rr-shutter').addClass('rr-shutter-all');
			}

            $rail.bind('rail:init',function()
			{
				 var num_category = $rail.find('ul').length;
                 var openIndex = 0;
				 
                 /* 20091111 leadweb Add comment 
				 if( $.cookie("rightrail_promo_index") != null || $.cookie("rightrail_promo_index") != "" ) {
					 while( num_category != 1 && (openIndex == parseInt($.cookie("rightrail_promo_index") || openIndex >= num_category))){
						  openIndex = Math.floor(Math.random()*num_category);
					 }
				 } 20091111 leadweb Add comment */ 
				 $.cookie("rightrail_promo_index", openIndex ,{ path: '/'});  

                 // add tracking here 
				 var promoIndex = 1;                  
				 $rail.find('ul').each(function(){
						 var linkIndex = 0;
                         $(this).find('li').each(function(){
							    $(this).find('div a').attr('trackingInfo','rnav_cat'+promoIndex+'_promo'+linkIndex).attr('pageName','Category Landing Page - Category '+promoIndex+' - Promotion '+linkIndex);
								linkIndex++;
						 });
					     promoIndex++; 
				 });
               
				 if( num_category == 1) {
					  $rail.find('li.title').hide();					  
				 }else{
					  $rail.find('ul').each(function(){
						 $(this).attr('openheight',$(this).height()).attr('closeheight', $(this).find('li.title').height()+11);
						 $(this).find('li.title').css({height:$(this).find('li.title').height() + 'px'});
						 $(this).find('li:last').find('div').css({border:'none'});
					  });
					  var openHeight = parseInt($rail.find('ul:eq('+openIndex+')').attr('openheight')) + returnOffset(openIndex);
					  $rail.find('ul:eq('+openIndex+')').addClass('active').css({height: openHeight +'px'});
					  $rail.find('ul:not("active")').find('li:not(".title")').hide();
					  $rail.find('ul.active').find('li').show();
				 }

				 $rail.find('h3.header').each(function(){
						var $nav = $(this);
						$nav.hover(
							function() {
								if ( !$nav.parents('ul').hasClass('active'))
								{
									$nav.css({backgroundPosition:'0 -30px'});	
								}
							},
							function() {
								if ( !$nav.parents('ul').hasClass('active'))
								{
									$nav.css({backgroundPosition:'0 0'});	
								}						
							}
						);
						$nav.click(function()
						{
						    var index = $rail.find('ul').index($nav.parents('ul'));
							if($nav.parents('ul').hasClass('active'))
							{
							   index = (index == num_category - 1 )? 0: index+1;
							}
						    switchPromotion(index, parseInt($.cookie("rightrail_promo_index")));
 						    $.cookie("rightrail_promo_index",index,{ path: '/'});

						});					
						function switchPromotion( nIndex, oIndex){
							var newContainer = $rail.find('ul:eq('+nIndex+')');
							var oldContainer = $rail.find('ul:eq('+oIndex+')');
							var openHeight = parseInt(newContainer.attr('openheight')) +  returnOffset(nIndex);
							var closeHeight = oldContainer.attr('closeheight');
							oldContainer.stop().animate({height: closeHeight+'px'},opts.toggleTiming,opts.toggleEasing, function(){
								  oldContainer.find('li:not(".title")').hide();
							}).removeClass('active');							
						    newContainer.find('li:not(".title")').show();
							newContainer.stop().animate({height: openHeight + 'px'}, opts.toggleTiming, opts.toggleEasing, function(){
		 				    }).addClass('active');
						}						
				});

			}).trigger('rail:init');

			$close.click(function()
			{
				if($rail.css("right")== "0px" && $rail.width() == 114)
				{			
					$rail.hide();
					$rail.add('div.rr-shutter').stop().animate({right: -114},opts.toggleTiming,opts.toggleEasing,function()
					{
						$close.css({backgroundPosition:'-10px center'});								
					});
				}
				else {		
					$rail.add('div.rr-shutter').stop().animate({right:0},opts.toggleTiming,opts.toggleEasing,function()
					{
						$rail.show();
						$close.css({backgroundPosition:'1px center'});						 
					});
				}
			});

			$close.hover(
				function()
				{
					$(this).css({backgroundColor:'#f9f9f9'});
				},
				function()
				{
					$(this).css({backgroundColor:''});
				}
			);

			$rail.bind('rail:mouseapproach',function(event,pageX,pageY)
			{
				if(pageX > ($(window).width()-40)) 
				{
					if($rail.css("width") == opts.closeWidth+"px")
					{
						$rail.stop().animate({width:opts.closeWidth+20},opts.toggleTiming,opts.toggleEasing);
					}
				} 
				else 
				{
					if($rail.css("width") == (opts.closeWidth+20)+"px")
					{
						$rail.stop().animate({width:opts.closeWidth},opts.toggleTiming,opts.toggleEasing);
					}
				}
			});
            
			$rail.bind('rail:resize', function(){
				      var openIndex = $rail.find('ul').index($('.right-rail ul.active'));
					  var openHeight = parseInt($rail.find('ul.active').attr('openheight')) + returnOffset(openIndex);
					  $rail.find('ul.active').css({height: openHeight +'px'});
			});

			function returnOffset(nIndex){				
				if( parseInt($(window).height()) <= 610 ){
					return 0;
				}else{
					var _window_offset = $(window).height();							
					$rail.find('ul').each(function(i){
						_window_offset = (i == nIndex)? _window_offset - $(this).attr('openheight'): _window_offset - $(this).attr('closeheight');
					});
					return _window_offset;
				}
			}
		});
	};

	$.fn.rail.defaults = 
	{
		openWidth: 120,
		closeWidth: 11,
		toggleTiming: 800,
		toggleEasing: "easeOutQuint"
	};

})(jQuery);

window.onload = function()
{
	if(typeof($isJsUnit)!="undefined") {
		if($isJsUnit == "true" && (parent.document.location + "").indexOf("testContainer") > 0) return;
	}
	onLoadPreInit();
	
	$('html,body').css({overflow:'hidden'});

	window.setTimeout(function()
	{	
		$('div.homepage-content,div.category-content').hero();
		$('div.right-rail').rail();
	},1000);

	onLoadPostInit();
}
