// JScript File
/* 
	Place custom client javascript code here.
*/

var LoggedIntoCMS = false;

//************************************************************
// Function:      sIFRinit
// Description:   rumfont flash replacement for h1 title tags
//************************************************************

function sIFRinit() 
{  
	if(typeof sIFR == "function")
	{    
		sIFR.replaceElement(named({sSelector:"div.elContent h1", sFlashSrc:mstrAppPath + "_themes/ACR/_flash/rumfont.swf", sColor:"#FFFFFF", sLinkColor:"#000000", sBgColor:"#000000", sWmode:"Transparent", sHoverColor:"#CCCCCC", nPaddingTop:0, nPaddingBottom:0, sFlashVars:""})); //textalign=right
		sIFR.replaceElement(named({sSelector:"div.elContent h1.enjoying", sFlashSrc:mstrAppPath + "_themes/ACR/_flash/rumfont.swf", sColor:"#958479", sLinkColor:"#000000", sBgColor:"#000000", sWmode:"Transparent", sHoverColor:"#CCCCCC", nPaddingTop:0, nPaddingBottom:0, sFlashVars:""})); //textalign=right
		sIFR.replaceElement(named({sSelector:"form.competition div.intro h1 span", sFlashSrc:mstrAppPath + "_themes/ACR/_flash/rumfont.swf", sColor:"#FFFFFF", sLinkColor:"#000000", sBgColor:"#000000", sWmode:"Transparent", sHoverColor:"#CCCCCC", nPaddingTop:0, nPaddingBottom:0, sFlashVars:""})); //textalign=right
	}  
}
addEvent(window, 'load', sIFRinit, false);

//***************************************************************
// Function:      BottleMenu
// Description:   displays applicable bottle name on bottle hover
//***************************************************************

BottleMenu = function() 
{
  var objMenus = getElementsByClassName(document.body, 'ul', 'elBottleMenu');
  objMenus.each(function(objMenu) {
    var objItems = $(objMenu).getElementsBySelector('li');    
    objItems.each(function(menuitem) {    
      if(menuitem.title!='')
      {        
        var _this = menuitem;
        menuitem.onmouseover=function() 
		        {
              if(this.title != '')
              {
                var objDiv = $('BottleText');
                if(!objDiv)
                {
                  objDiv = $CE('div', {id: 'BottleText'});
                }
                objDiv.innerHTML = this.title;                
                this.parentNode.parentNode.appendChild(objDiv);
              }
		        }     
		    menuitem.onmouseout=function() 
		        {		
		        /*           
		            var objDiv = $('BottleText');
                if(!objDiv)
                {
                  objDiv = $CE('div', {id: 'BottleText'});
                }
                objDiv.innerHTML = '';                         
            */
		        }
      }
    });
  });
}
addEvent(window, 'load', BottleMenu, false);

//***********************************************************
// Function:      ACRMenu
// Description:   display larger image on click of thumbnails
//***********************************************************

ACRMenu = function() 
{  
  var objMenus = getElementsByClassName(document.body, 'ul', 'elACRMenu');
  objMenus.each(function(objMenu) {
    var objItems = $(objMenu).getElementsBySelector('li');
    objItems.each(function(menuitem) {
      if(menuitem.title!='') 
      {
        var _this = menuitem;        
        menuitem.onclick=function() 
		        {
              if(this.title != '')
              { 
                var objDiv = $('ACRImage');
                if(!objDiv)
                {
                  objDiv = $CE('div', {id: 'ACRImage'});
                }
                var strId = 'img' + this.title.replace(/\s/g,'');
                
                // hide all the ACR images
                var objACRImages = getElementsByClassName(document.body, 'p', 'ACRImage');
                objACRImages.each(function(objACRImage) {
                    $(objACRImage).hide();
                });
                
                // show the given ACR image
                $(strId).style.display = 'block';
              }
		        }            
      }
    });
  });
}
addEvent(window, 'load', ACRMenu, false);

function clearhighlights()
{
	var objMenus = getElementsByClassName(document.body, 'ul', 'elVinopolisMenu');
  objMenus.each(function(objMenu) 
  {
  
    var objItems = $(objMenu).getElementsBySelector('li');
	
    objItems.each(function(menuitem) 
    {
			var objA = $(menuitem).getElementsBySelector('a');		
			objA[0].className = objA[0].className.replace(" selected", "");			
    });
  });
}

//***********************************************************
// Function:      VinopolisMenu
// Description:   display larger image on click of thumbnails
//***********************************************************

VinopolisMenu = function() 
{  
  var objMenus = getElementsByClassName(document.body, 'ul', 'elVinopolisMenu');
  objMenus.each(function(objMenu) {
    var objItems = $(objMenu).getElementsBySelector('li');
	
    objItems.each(function(menuitem) {
      if(menuitem.title!='') 
      {
        var _this = menuitem;
        menuitem.onclick=function() 
		{			
			clearhighlights();
			var objA = $(this).getElementsBySelector('a');		
			objA[0].className = objA[0].className + " selected";
		  if(this.title != '')
			{ 
				var objDiv = $('VinopolisImage');
				if(!objDiv)
				{
				  objDiv = $CE('div', {id: 'VinopolisImage'});
				}
				var strId = 'img' + this.title.replace(/\s/g,'');
				
				// hide all the vinopolis images
				var objVinopolisImages = getElementsByClassName(document.body, 'p', 'VinopolisImage');
				objVinopolisImages.each(function(objVinopolisImage) {
					$(objVinopolisImage).hide();
				});
				
				// show the given vinpolis image
				$(strId).style.display = 'block';
			 }
		}            
      }
    });
  });
}
addEvent(window, 'load', VinopolisMenu, false);

//***********************************************************
// Function:      DrinkingRumMenu
// Description:   displays drink paragraph upon hover
//***********************************************************

DrinkingRumMenu = function() 
{
  var objMenus = getElementsByClassName(document.body, 'ul', 'elDrinkingRumMenu');
  objMenus.each(function(objMenu) {
    var objItems = $(objMenu).getElementsBySelector('li');
    objItems.each(function(menuitem) {
      if(menuitem.title!='')
      {
        var _this = menuitem;
        menuitem.onmouseover=function() 
		        {
              if(this.title != '')
              {
                var objDiv = $('DrinkingRumText');
                if(!objDiv)
                {
                  objDiv = $CE('div', {id: 'DrinkingRumText'});
                }
                var strId = 'txt' + this.title.replace(/\s/g,'');
                
                // hide all the drinking rum text
                var objTexts = getElementsByClassName(document.body, 'p', 'DrinkingRumText');
                objTexts.each(function(objText) {
                    $(objText).hide();
                });
                
                // show the given drinking rum text
                $(strId).style.display = 'block';
              }
		        }    
        menuitem.onmouseout=function() 
		        {
                var objTexts = getElementsByClassName(document.body, 'p', 'DrinkingRumText');
                objTexts.each(function(objText) {
                    $(objText).hide();
                });
		        }            
      }
    });
  });
}
addEvent(window, 'load', DrinkingRumMenu, false);

//***********************************************************
// Function:      IslandMenu
// Description:   caribbean explorer - displays island name, 
//                map coords, distillery information upon 
//                hovering over island flags 
//                (caribbean explorer html replacement)
//***********************************************************

IslandMenu = function() 
{
  var objMenus = getElementsByClassName(document.body, 'ul', 'elIslandMenu');
  objMenus.each(function(objMenu) {
    var objItems = $(objMenu).getElementsBySelector('li');
    objItems.each(function(menuitem) {
      if(menuitem.title!='')
      {
        var _this = menuitem;         
        menuitem.onmouseover=function() 
		        {
              if(this.title != '')
              {
                var objDiv = $('IslandDistilleries');
                if(!objDiv)
                {
                  objDiv = $CE('div', {id: 'IslandDistilleries'});
                }
                var strId = this.title.replace(/\s/g,'');                
                
                // hide all the distillery information
                var objDistilleries = getElementsByClassName(document.body, 'div', 'IslandDistilleries');
                objDistilleries.each(function(objDistillery) {
                    $(objDistillery).hide();
                });
                
                // show the given distillery information
                $(strId).style.display = 'block';
              }
		        }    
        menuitem.onmouseout=function() 
		        {
                var objTexts = getElementsByClassName(document.body, 'div', 'IslandDistilleries');
                objTexts.each(function(objText) {
                    $(objText).hide();
                });
		        }             
      }
    });
  });
}
addEvent(window, 'load', IslandMenu, false);

// initially hide all the christmas cocktail copy and show the introductory copy
ChristmasCocktailCopy = function() 
{
    hideAllChristmasCocktailCopy();
    showChristmasIntroCopy();
}
addEvent(window, 'load', ChristmasCocktailCopy, false);

// show the introductory copy
function showChristmasIntroCopy()
{
    var objCopy = $('CocktailIntro');
    if(objCopy){
       objCopy.show();
    }
}

// hide all the christmas cocktail copy and display the given christmas cocktail copy
function displayChristmasCocktail(strCocktailName)
{
    hideAllChristmasCocktailCopy();
    
    var objCopy = $(strCocktailName);
    if(objCopy){
       objCopy.show();
    }
}

// hide all the christmas cocktail copy including the introductory copy
function hideAllChristmasCocktailCopy()
{       
    var objCopy = $('CocktailIntro');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('CocktailPartridge');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('CocktailTurtle');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('CocktailFive');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('CocktailFrench');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('CocktailDancing');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('CocktailGeese');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('CocktailLeaping');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('CocktailSwimming');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('CocktailPiping');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('CocktailCalling');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('CocktailMilk');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('CocktailDrummer');
    if(objCopy){
       objCopy.hide();
    }  
    
}

//***********************************************************
// Function:      displayIslandMap
// Description:   displays the given island image map and island 
//                copy when clicking an island flag
//                (caribbean explorer html replacement)
//***********************************************************

function displayIslandMap(strMapName)
{ 
  // hide the island flags and island distillery information
  var objMenus = getElementsByClassName(document.body, 'ul', 'elIslandMenu');
  if(objMenus.length > 0)
  {  
	  $(objMenus[0]).hide();
	  var objD = getElementsByClassName(document.body, 'div', 'IslandDistilleries');
	  objD.each(function(item) {
		$(item).hide();
	  });   
	  
	  // show the given island image map
	  $(strMapName).style.display='block';
	  	  
	  // the code logic below is as follows ...
	  // if not on the brand image/brand copy page
	  // show the given island copy since we must be on an island page
	  // else
	  // hide the landing copy and island copy since we must be on a brand page 
	 
	  var objBrandImage = getElementsByClassName(document.body, 'div', 'brandimage')[0];
    if (!objBrandImage) 
    {         
      // show the given island copy	  
	    showIslandCopy(strMapName+'Copy');      
    }
    else
    {            
      // hide the landing copy and island copy
      hideLandingCopy();
      
      if ('none' == objBrandImage.style.display )
      { 				
        showIslandCopy(strMapName+'Copy');      
      }      
    }
  }
}

//***********************************************************
// Function:      showLandingCopy
// Description:   displays the caribbean explorer landing copy
//***********************************************************

function showLandingCopy()
{   
	  // hide all the island copy for all islands
	  hideAllIslandCopy(); 
	  
	  // show the landing copy	  
	  var objCopy = $('CaribbeanExplorerLandingCopy');
    if(objCopy)
    {
       objCopy.show();
    }
}

//***********************************************************
// Function:      hideLandingCopy
// Description:   hide the caribbean explorer landing copy
//***********************************************************

function hideLandingCopy()
{
    // hide the landing copy	  
	  var objCopy = $('CaribbeanExplorerLandingCopy');
    if(objCopy)
    {
       objCopy.hide();
    }
}

//***********************************************************
// Function:      showIslandCopy
// Description:   displays the given island copy
//***********************************************************

function showIslandCopy(strMapName)
{ 	
    // hide the landing copy
    hideLandingCopy(); 
	  
	  // hide all the island copy for all islands
	  hideAllIslandCopy();	  
	  
	  // display the island copy for this island	  	  
	  var objBrandImage = getElementsByClassName(document.body, 'div', 'brandimage')[0];
    if (!objBrandImage)
    {    
      // display the island copy for this island	  	  
	    document.getElementById(strMapName).style.display = 'block';	  	  	  
    }
    else
    {       
			// on a brand page		
			var flashenabled = IsFlashEnabled();
			
			if (flashenabled)
			{				
					// flash version
					if ('none' == objBrandImage.style.display )
					{ 
							// the brand image is hidden
							// show the island copy since an island has been selected
							document.getElementById(strMapName).style.display = 'block'
					}   
					else
					{				
							// the brand image is displayed
							// hide the island copy since the brand is being viewed
							document.getElementById(strMapName).style.display = 'none'; 	  	  			
					}				
			}
			else
			{				
				// html version
				document.getElementById(strMapName).style.display = 'block';	
			}      
      
    }    
}

function IsFlashEnabled()
{	
	var requiredMajorVersion = 9;	
	var requiredMinorVersion = 0;	
	var requiredRevision = 0;
	
	var enabled = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);			
	return enabled;
}

//***********************************************************
// Function:      hideAllIslandCopy
// Description:   hide all the island copy
//***********************************************************

function hideAllIslandCopy()
{   
    var objCopy = $('MapAntiguaCopy');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('MapBahamasCopy');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('MapBarbadosCopy');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('MapBelizeCopy');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('MapDominicaCopy');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('MapDomRepCopy');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('MapGrenadaCopy');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('MapGuyanaCopy');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('MapHaitiCopy');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('MapJamaicaCopy');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('MapStLuciaCopy');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('MapStVincentCopy');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('MapSurinameCopy');
    if(objCopy){
       objCopy.hide();
    }
    objCopy = $('MapTrinidadCopy');
    if(objCopy){      
       objCopy.hide();
    }
}


//***********************************************************
// Function:      highlightBottle
// Description:   highlights the given bottle
//***********************************************************

function highlightBottle(strName)
{
  unhighlightBottles();
  
  // highlight the given bottle
  $(document.getElementsByClassName(strName)[0]).getElementsBySelector('a')[0].addClassName('selected');
}

//***********************************************************
// Function:      unhighlightBottles
// Description:   removes the highlight off all the bottles 
//                in the bottle menu
//***********************************************************
function unhighlightBottles()
{
  // unhighlight all the bottles 
  $A(getElementsByClassName(document.body, 'ul', 'elBottleMenu')[0].getElementsByTagName('a')).each(function(item) {
    item.removeClassName('selected')
  });
}

//***********************************************************
// Function:      closeIsland
// Description:   closes the island image maps,
//                returns to the caribbean explorer page on 
//                first landing
//***********************************************************

function closeIsland()
{
	var flashenabled = IsFlashEnabled();
	  
  if (!flashenabled)
  {
		// hide all the island image maps
	  $A($('ImgMaps').getElementsByTagName('img')).each(function(item) {
    item.style.display = 'none';  
		});
		
		unhighlightBottles();
	}
 
  // hide the brand image and brand copy  
  var objBrandImage = getElementsByClassName(document.body, 'div', 'brandimage')[0];
  if (objBrandImage) {
    objBrandImage.style.display = 'none';	     
  }    
  var objBrandCopy = getElementsByClassName(document.body, 'div', 'brandcopy')[0];
  if (objBrandCopy) {
    objBrandCopy.style.display = 'none';	     
  }
  
  if (!flashenabled)
  {
		// show the island flags
	  getElementsByClassName(document.body, 'ul', 'elIslandMenu')[0].show();
	}

  // show the landing copy
  showLandingCopy();
}

//***********************************************************
// Function:      getBottleId
// Description:   returns the appropriate bottle id to be passed 
//                into the caribbean explorer flash map so the
//                relevant bottle can be highlighted
//***********************************************************

function getBottleId()
{	
  if($('aspnetForm').hasClassName('elExpAngostura'))
  {
     return 1;
  }
  
  if($('aspnetForm').hasClassName('elExpAppletonEstate'))
  {
     return 2;
  }
  
  if($('aspnetForm').hasClassName('elExpBarbancourt'))
  {
     return 3;
  }
  
  if($('aspnetForm').hasClassName('elExpRonBarcello'))
  {
     return 4;
  }
  
  if($('aspnetForm').hasClassName('elExpBorgoe'))
  {
     return 5;
  }
  
  if($('aspnetForm').hasClassName('elExpBrugalExtraViejo'))
  {
     return 6;
  }
  
  if($('aspnetForm').hasClassName('elExpChairmansReserve'))
  {
     return 7;
  }
  
  if($('aspnetForm').hasClassName('elExpClarkesOldGrog'))
  {
     return 8;
  }
  
  if($('aspnetForm').hasClassName('elExpCockspurFineRum'))
  {
     return 9;
  }
  
  if($('aspnetForm').hasClassName('elExpDoorlysXO'))
  {
     return 10;
  }
  
  if($('aspnetForm').hasClassName('elExpEldorado'))
  {
     return 11;
  }
  
  if($('aspnetForm').hasClassName('elExpEnglishHarbour'))
  {
     return 12;
  }
  
  if($('aspnetForm').hasClassName('elExpMountGayEclipse'))
  {
     return 13;
  }
  
  if($('aspnetForm').hasClassName('elExpMountGilboa'))
  {
     return 14;
  }
  
  if($('aspnetForm').hasClassName('elExpOneBarrel'))
  {
     return 15;
  }
  
  if($('aspnetForm').hasClassName('elExpSunsetRum'))
  {
     return 16;
  }
  
  if($('aspnetForm').hasClassName('elExpWesterhall'))
  {
     return 17;
  }
  
  if($('aspnetForm').hasClassName('elExpXMRoyal'))
  {
     return 18;
  }
			
	return 0;
}

//***********************************************************
// Function:      
// Description:   main menu fade/cascading functionality
//***********************************************************

// Crossbrowser onmouseenter and onmouseleave events

if (!Prototype.Browser.IE) {
  var MouseenterMouseleave = {
    cache: {enter: [], leave: []},
    attach: function(type, element, observer, useCapture) {
      var wrapped = function(event) {
        if (event.relatedTarget==event.currentTarget || event.relatedTarget.childOf(event.currentTarget)) return;
        observer(); 
      };
      element.observe('mouse'+(type=='enter'?'over':'out'), wrapped, useCapture);
      this.cache[type].push({e:element, o:observer, w:wrapped});
    },
    detach: function(type, element, observer, useCapture) {
      var cached = this.cache[type].find(function(obj) {
        return obj.e==element && obj.o==observer;
      });
      if (cached) {
        element.FstopObserving('mouse'+(type=='enter'?'over':'out'), cached.w, useCapture);
        this.cache[type] = this.cache[type].without(cached);
      }
    }
  };
}

Event.Fobserve = function(element, name, observer, useCapture) {
    
	element = $(element);
    useCapture = useCapture || false;
  
    if (name == 'keypress' &&
      (Prototype.Browser.WebKit || element.attachEvent))
      name = 'keydown';
  
    if (!Prototype.Browser.IE && (name == 'mouseenter' || name == 'mouseleave')) {
      MouseenterMouseleave.attach(name.substring(5), element, observer, useCapture);
    } else {
      //Event._observeAndCache(element, name, observer, useCapture);
    }
	
  };

Event.FstopObserving = function(element, name, observer, useCapture) {
    element = $(element);
    useCapture = useCapture || false;

    if (name == 'keypress' &&
        (Prototype.Browser.WebKit || element.attachEvent))
      name = 'keydown';

    if (!Prototype.Browser.IE && (name == 'mouseenter' || name == 'mouseleave')) {
      MouseenterMouseleave.detach(name.substring(5), element, observer, useCapture);
    } else {
      if (element.removeEventListener) {
        element.removeEventListener(name, observer, useCapture);
      } else if (element.detachEvent) {
        try {
          element.detachEvent('on' + name, observer);
        } catch (e) {}
      }
    }  
  };



// New menu hover effect

var mstrScope = '';
MenuFadeHover = function() 
{
    var objMenu = getElementsByClassName(document.body, 'ul', 'FadeMenu')[0];
	
	if(objMenu)
	{
		objMenu = $(objMenu);
		
		//give toplevel elements special class
	    var objEls = objMenu.childElements();
	    objEls.each(function(node, index) {
	    	node.addClassName('selector');
	    });

		//hide all the LI's below the top level
		var objEl = objMenu.select('ul');
		objEl.each(function(node, index) {
			$A(node.descendants()).each(function(nodes) {
		        if(nodes.tagName == 'LI')
		        {
					if(!Prototype.Browser.IE)
					{
						nodes.setStyle({backgroundImage: 'url('+mstrAppPath+'_themes/ACR/_images/ACR/trans.png)'});
					}
					else
					{
						nodes.setStyle({backgroundColor: '#000000'});
					}
					nodes._hidden=nodes.getStyle('display')=='none';
					nodes.setStyle({display: 'none'});
				}
			});
		});

	    var objItems = objMenu.childElements();

	    objItems.each(function(menuitem) {
	    	
	    	//setup the events on each main menu item
		    menuitem.onmouseenter=function(evt) 
		    {
    	    this.addClassName('MenuItemHover');
    
			    if(this.hasClassName('selector'))
			    {
			    //cancel any other effects in progress
					var queue = Effect.Queues.get(mstrScope);
					queue.each(function(effect) { effect.cancel(); });	

					var _id = this.firstDescendant().firstDescendant().innerHTML;
				    mstrScope = _id;

					//fade the other top level LI's
					$A(this.siblings()).each(function(neighbours) {
						if(neighbours.tagName == 'LI')
						{
							//new Effect.Fade(neighbours.firstDescendant(), {duration: 0, from: 1, to: .5});  //opacity
							neighbours.firstDescendant().setStyle({color: '#3a362b'});
						}
					});		
				    
					//display each child LI, fading into view
					$A(this.descendants()).each(function(children) {
						if(children.tagName == 'LI' && !children._hidden)
						{
							if(mstrScope==_id)
							{
								var intDur = .3;
								
								if(_id=='Admin.')
								{
									intDur = 0;
								}

								if(!Prototype.Browser.IE)
								{
									new Effect.Appear(children, {duration: intDur, from: 0, to: 1, queue: { position: 'end', scope: _id }});  //duration
								}
								else
								{
									new Effect.Appear(children, {duration: intDur, from: 0, to: .5, queue: { position: 'end', scope: _id }});  //duration
								}
							}
						}
					});	
				}
		    }
		    menuitem.onmouseleave=function() 
		    {
	            this.itemout();
		    }
	        menuitem.itemout=function() 
	        {
	            this.removeClassName('MenuItemHover');
	            if(this.hasClassName('selector'))
	            {
				
					//undo fade the top level LI's
					$A(this.siblings()).each(function(neighbours) {
						if(neighbours.tagName == 'LI')
						{
							//new Effect.Fade(neighbours.firstDescendant(), {duration: 0, from: .5, to: 1});  //opacity
							neighbours.firstDescendant().setStyle({color: '#d8c694'});
							
						}
					});
					
					//cancel any other effects in progress
					var queue = Effect.Queues.get(mstrScope);
					queue.each(function(effect) { effect.cancel(); });	 

					//hide all menu items
					objMenu.getElementsBySelector('ul li').each(function(children) {
						children.setStyle({display: 'none'});
					});

	        	}
	        }	

			if(!Prototype.Browser.IE)
			{
				//add the events for non IE browsers only
				Event.Fobserve(menuitem, 'mouseenter', menuitem.onmouseenter.bindAsEventListener(menuitem), true);
				Event.Fobserve(menuitem, 'mouseleave', menuitem.onmouseleave.bindAsEventListener(menuitem), true);
			}

	    });	
	}
};

addEvent(window, 'load', MenuFadeHover, false);

//***********************************************************
// Function:      isValidDate
// Description:   check that the date is valid 
//                in format and values
//***********************************************************

function isValidDate() 
{
	
	var Day = document.aspnetForm.txtday.value;
  	var Month = document.aspnetForm.txtmonth.value;
  	var Year = document.aspnetForm.txtyear.value;	 

  // make sure the date is in the correct format
  if (isNaN(Day) || isNaN(Month) || isNaN(Year))
  {
    return false;
  }

  // check Month range
	if (Month < 1 || Month > 12) 
	{ 
		alert("Month must be between 1 and 12.");
		return false;
	}
	if (Day < 1 || Day > 31) 
	{
		alert("Day must be between 1 and 31.");
		return false;
	}	
	if (Year < 1753 || Year > 9999) 
	{
	alert("Year must be between 1753 and 9999.");
		return false;
	}
	if ((Month==2 || Month==4 || Month==6 || Month==9 || Month==11) && Day==31) 
	{
		alert("Month "+Month+" doesn't have 31 days!")
		return false;
	}
	// check For february 29th
	if (Month == 2) 
	{ 
		var isleap = (Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0));
		if (Day>29 || (Day==29 && !isleap)) {
			alert("February " + Year + " doesn't have " + Day + " days!");
			return false;
		}
	}
	return true;
}

function IsAgeValid(given_age, country)
{	
	var agevalid = false;
	
	switch (country)
	{
		case "ba":
		case "dm":
		case "it":
		case "tz":	
		{			
			// 16 years old is valid
			if (given_age > 5844)
			{
				agevalid = true;
			}
			break;					
		}
		case "gr":
		{			
			// 17 years old is valid
			if (given_age > 6210)
			{
				agevalid = true;
			}
			break;
		}
		case "al":
		case "ar":
		case "am":
		case "au":
		case "at":
		case "az":
		case "bs":
		case "bh":
		case "by":
		case "be-du":		
		case "be-fr":		
		case "bm":
		case "bo":
		case "bz":
		case "br":
		case "bg":
		case "cm":
		case "ca-al":
		case "ca-mn":
		case "ca-qb":	
		case "cl":
		case "cn":
		case "co":
		case "cr":
		case "hr":
		case "cs":
		case "dk":
		case "ec":
		case "sv":
		case "ee":
		case "ge":
		case "de": 
		case "gt":
		case "hn":
		case "hk":
		case "hu":
		case "ie":
		case "il": 
		case "jm":
		case "ke":
		case "lv":
		case "lb":
		case "ls":
		case "li":
		case "lt":
		case "lu":
		case "mk":
		case "my":
		case "mt":
		case "mu":
		case "mx":
		case "md":
		case "yu":
		case "np":
		case "nl":
		case "nz":
		case "ni":
		case "pw":
		case "pa":
		case "pg":
		case "py":
		case "pe":
		case "ph":
		case "pl":
		case "pt":
		case "pr":
		case "ro":
		case "ru":
		case "as":
		case "rs":
		case "sg":
		case "si":
		case "sb":
		case "za":
		case "es":
		case "lk":
		case "sz":
		case "se":
		case "ch-fr":		
		case "ch-de":		
		case "tw":
		case "th":
		case "to":
		case "tt":
		case "tr":
		case "uk":
		case "uy":
		case "uz":
		case "ve":
		case "zw":
		{			
			// 18 years old is valid
			if (given_age > 6575)
			{
				agevalid = true;
			}
			break;
		}
		case "ca-bc":
		case "ca-nb":
		case "ca-nw":
		case "ca-nf":
		case "ca-ns":
		case "ca-nn":
		case "ca-on":
		case "ca-pe":
		case "ca-yk":
		case "ca-sa":		
		case "kr":
		{			
			// 19 years old is valid
			if (given_age > 6940)
			{
				agevalid = true;
			}
			break;
		}
		case "fi":
		case "is":
		case "jp":
		case "no":		
		{			
			// 20 years old is valid
			if (given_age > 7306)
			{
				agevalid = true;
			}
			break;
		}
		case "eg":
		case "fj":
		case "id":
		case "kz":
		case "ua":
		case "us":		
		{			
			// 21 years old is valid
			if (given_age > 7671)
			{
				agevalid = true;
			}
			break;
		}
		case "in":
		{			
			// 25 years old is valid
			if (given_age > 9132)
			{
				agevalid = true;
			}
			break;
		}
		default:
		{
			// 21 years old is valid
			if (given_age > 7671)
			{
				agevalid = true;
			}			
		}
	}		
	return agevalid;
}

//***********************************************************
// Author:        RT
// Last Modified: 03/02/2009 14:12
// Function:      SwitchLocale
// Description:   Takes the given locale and redirects to the 
//                associated domain. MiWeb now configured to 
//                serve up translations based on domain, so this
//                effectively switches the language.
//***********************************************************

function SwitchLocale(locale)
{
  if(locale !== '')
  {
    var domain = '';
  
    switch(locale)
    {
      case 'en':
        if(location.hostname === 'www.truerum.com') // If we've come from .com, retain this hostname.
        {
          domain = 'www.truerum.com';
        }
        else
        {
          domain = 'www.truerum.co.uk';
        }
        break;
      case 'it':
        domain = 'www.truerum.it';
        break;
      case 'es':
        domain = 'www.truerum.es';
        break;
      default:
        domain = 'www.truerum.co.uk'; // Redirect to .co.uk by default
        break;
    }
    
    var targetUrl = location.protocol + '//' + domain + location.pathname + location.search;
    
    location.href = targetUrl;
  }
}

//***********************************************************
// Function:      StoreSiteEntryData
// Description:   Handles redirect to site-entry.ashx to save
//                details dob and country to DB and sets cookie.
//***********************************************************

function StoreSiteEntryData(dob, country)
{
	var targetURL;
  
  // RT: Updated how targetURL is set to simply redirect to the home.ashx page of the current host
  //     thus ensuring that the script works on both proofing and live environments.
  
  // RT: 
  // Parse the URL params and get the value of 'ReturnUrl'.
  var returnUrl = '';
  var searchString = document.location.search;

  // Strip leading '?'
  searchString = searchString.substring(1);

  var urlParams = searchString.split('&');

  for(i = 0; i < urlParams.length; i++)
  {
    var nameValuePair = urlParams[i].split('=');
    var name = nameValuePair[0];
    var value = nameValuePair[1];
    
    if(name === 'ReturnUrl')
    {
      returnUrl = value;
      break;
    }
  }
  
  // If no ReturnUrl param set, default to root.
  returnUrl = returnUrl === '' ? '%2f' : returnUrl;

  targetURL = encodeURIComponent(document.location.protocol) + '%2f%2f' + location.host + returnUrl;

  document.location.href = mstrAppPath + 'site-entry.ashx?dob=' + dob + '&country=' + country + '&MwCookieRedirect=' + targetURL + '&ReturnUrl=' + targetURL;
}

//***********************************************************
// Function:      AgeCheckSubmit
// Description:   check whether the given age is valid and then submit
//                then redirect to home page else display message
//***********************************************************

function AgeCheckSubmit()
{	

  if (isValidDate()) 
	{ 	
		var Day = document.aspnetForm.txtday.value;
    	var Month = document.aspnetForm.txtmonth.value;
    	var Year = document.aspnetForm.txtyear.value;
    	var country = document.aspnetForm.country.value;
		millennium = new Date(Year, Month-1, Day);
	}
	else 
	{	
	  alert("Please enter a date in the correct format.");
	  return;
	}
		
	//var millennium =new Date(1990, 6, 11) //Month is 0-11 in JavaScript
	today=new Date()
	//Get 1 day in milliseconds
	var one_day=1000*60*60*24
	
	//Calculate difference btw the two dates, and convert to days
	age_days = Math.ceil((today.getTime()-millennium.getTime())/(one_day))
	
	// the age is valid	
	if (IsAgeValid(age_days, country))	
	{
			// Set cookie and save the details to the database		
			var dob = Day + '/' + Month + '/' + Year;				
			StoreSiteEntryData(dob, country);
			RememberMe();
	}
	else 
	{
		alert("We're sorry that you aren't of an age that you can enjoy True Rum, please come back in a few years.");
	}
}

//***********************************************************
// Function:      vinopoliswalkthrough
// Description:   hide the vinopolis images/thumbnails and 
//                display the walkthrough movie
//***********************************************************

function vinopoliswalkthrough()
{  
  // hide all the vinopolis images
  var objVinopolisImages = getElementsByClassName(document.body, 'p', 'VinopolisImage');
  objVinopolisImages.each(function(objVinopolisImage) {
      $(objVinopolisImage).hide();
  });
  
  // hide all the vinopolis thumbnails
  getElementsByClassName(document.body, 'ul', 'elVinopolisMenu')[0].style.display = "none";
  
  // hide the view walkthrough link
  getElementsByClassName(document.body, 'p', 'viewwalkthrough')[0].style.display = "none";
     
  // show the vinopolis walkthrough
  document.getElementById('VinopolisWalkThroughVideo').style.display = 'block';	   
  
  // show the view images link
  getElementsByClassName(document.body, 'p', 'viewimages')[0].style.display = 'block';	       
}


//***********************************************************
// Function:      vinopolisimages
// Description:   hide the vinopolis walkthrough movie and 
//                display the images/thumbnails
//***********************************************************

function vinopolisimages()
{
  // hide the vinopolis walkthrough
  document.getElementById('VinopolisWalkThroughVideo').style.display = 'none';	   
     
  // hide the view images link
  getElementsByClassName(document.body, 'p', 'viewimages')[0].style.display = "none";
  
  // show all the vinopolis images
  var objVinopolisImages = getElementsByClassName(document.body, 'p', 'VinopolisImage');
  objVinopolisImages.each(function(objVinopolisImage) {
      $(objVinopolisImage).show();
  });
  
  // show all the vinopolis thumbnails
  getElementsByClassName(document.body, 'ul', 'elVinopolisMenu')[0].style.display = "block";
  
  // show the view walkthrough link
  getElementsByClassName(document.body, 'p', 'viewwalkthrough')[0].style.display = "block";  
}


function BackToIsland(strMapName)
{
    var objBrandImage = getElementsByClassName(document.body, 'div', 'brandimage')[0];
    var objBrandCopy = getElementsByClassName(document.body, 'div', 'brandcopy')[0];
    
    if (objBrandImage)
    {
        objBrandImage.style.display = 'none';
    }
    if (objBrandCopy)
    {
        objBrandCopy.style.display = 'none';
    }       
    document.getElementById(strMapName+'Copy').style.display = 'block';	
}

function RememberMe()
{   
    if (document.aspnetForm.remember.checked)
    {        
        setCookie("RememberMe","1",365);
    }
    else
    {   
        deleteCookie("RememberMe");
        
    }
}

function setCookie(c_name,value,expiredays)
{
    var expdate = new Date();
    expdate.setDate(expdate.getDate()+expiredays);
    document.cookie = c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+expdate.toGMTString());
}

function deleteCookie(c_name) 
{    
    document.cookie = c_name + "=; expires=Thu, 01-Jan-09 00:00:01 GMT" + "; path=/";
} 

function IsChecked(that)
{
    var radio=$(that).select('input');
    return radio[0].checked||radio[1].checked||radio[2].checked;
}

addEvent(window, 'load', function(){
	$$('form.competition .formsubmit').each(function(element){
		element.observe('click',function(){
			var value=$$('.elCemailaddress textarea');
			if(value)document.cookie='email='+escape(value[0].value);
		});
	});
	if($$('form.competition .elCnearestcity').length){
		var country=locale=='en-gb'?'UK':locale=='es'?'ESPA'+String.fromCharCode(209)+'A':locale=='it'?'ITALIA':'';
		var show=true;
		if(country){
			var list=$$('form.competition .elCnearestcity select')[0];
			for(var i=0;i<list.options.length;i++)
				if(
					list.options[i].value=='UK'||
					list.options[i].value=='ESPA'+String.fromCharCode(209)+'A'||
					list.options[i].value=='ITALIA'
				){
					show=list.options[i].value==country;
					list.remove(i--);
				}else
					if(!show)list.remove(i--);
		}
	}
}, false);
