window.addEvent('domready', function() {
  url = window.location.pathname;
  
  if (url.match('home'))
  {
    if (GetSwfVer() != -1)
    {
      if (oldEnough() == "Yes"){formValue = 1}else{formValue=0};
      swfPath = "images/flashMenu.swf?oldEnough=" + formValue;
      
      swfobject.embedSWF(swfPath, "homepage", "943", "489", "7.0.0", "images/expressInstall.swf");
    }
    else
    {
      $('homepage').setHTML("<div id='mousemove'><div class='inside move'></div></div> <script>nonFlashVersion();</script>");
    }
  }
});
  
function nonFlashVersion()
{
  window.addEvent('domready', function() {
 
    var scroll2 = new Scroller('mousemove', {area: 200, velocity: 0.06});

    // Mousemove
    $('mousemove').addEvent('mouseover', scroll2.start.bind(scroll2));
    $('mousemove').addEvent('mouseout', scroll2.stop.bind(scroll2));

  });
}

function initBoxItems()
{
  //events for showing info for each box item
  window.addEvent('domready', function() {
    $('bottle').addEvent('mouseenter', function(){
      hideBoxes();
      $('box_tio_pepe').setStyle('display','block');
      $('bottle').addClass('bottle_large') 
    });
    $('bottle').addEvent('mouseleave', function(){
      $('bottle').removeClass('bottle_large') }
      );
      
    $('little_wallop').addEvent('mouseenter', function(){
      hideBoxes();
      $('box_wallop').setStyle('display','block')
      $('little_wallop').addClass('little_wallop_large') 
      });
      $('little_wallop').addEvent('mouseleave', function(){
        $('little_wallop').removeClass('little_wallop_large') }
      );
    
    $('cheese').addEvent('mouseenter', function(){
      hideBoxes();
      $('box_manchego').setStyle('display','block') 
      $('cheese').addClass('cheese_large') 
      });
      $('cheese').addEvent('mouseleave', function(){
        $('cheese').removeClass('cheese_large') }
      );
      
    $('boar').addEvent('mouseenter', function(){
      hideBoxes();
      $('box_boar').setStyle('display','block')
      $('boar').addClass('boar_large') 
      }); 
      $('boar').addEvent('mouseleave', function(){
        $('boar').removeClass('boar_large') }
      );
      
    $('chorizo').addEvent('mouseenter', function(){
      hideBoxes();
      $('box_chorizo').setStyle('display','block') 
      $('chorizo').addClass('chorizo_large') 
      });
      $('chorizo').addEvent('mouseleave', function(){
        $('chorizo').removeClass('chorizo_large') }
      );
      
    $('crackers').addEvent('mouseenter', function(){
      hideBoxes();
      $('box_mondovino').setStyle('display','block') 
      $('crackers').addClass('crackers_large') 
      });
     $('crackers').addEvent('mouseleave', function(){
        $('crackers').removeClass('crackers_large') }
      );
      
    $('navarrico').addEvent('mouseenter', function(){
      hideBoxes();
      $('box_artichoke').setStyle('display','block') 
      $('navarrico').addClass('navarrico_large') 
      });
     $('navarrico').addEvent('mouseleave', function(){
        $('navarrico').removeClass('navarrico_large') }
      );
      
    $('teruel').addEvent('mouseenter', function(){
      hideBoxes();
      $('box_jamon').setStyle('display','block')
      $('teruel').addClass('teruel_large') 
      });
    $('teruel').addEvent('mouseleave', function(){
      $('teruel').removeClass('teruel_large') }
    );
      
    $('olives').addEvent('mouseenter', function(){
      hideBoxes();
      $('box_romanico_olives').setStyle('display','block') 
      $('olives').addClass('olives_large') 
      });
    $('olives').addEvent('mouseleave', function(){
      $('olives').removeClass('olives_large') }
    );
    
    $('bacalao').addEvent('mouseenter', function(){
      hideBoxes();
      $('box_piquilla').setStyle('display','block')
      $('bacalao').addClass('bacalao_large')
      });     
    $('bacalao').addEvent('mouseleave', function(){
      $('bacalao').removeClass('bacalao_large') }
    );
    
    $('romanico').addEvent('mouseenter', function(){
      hideBoxes();
      $('box_romanico').setStyle('display','block') 
      $('romanico').addClass('romanico_large')
      });
    $('romanico').addEvent('mouseleave', function(){
      $('romanico').removeClass('romanico_large') }
    );
  });
}

function hideBoxes()
{
 $$('#boxes').getChildren().each(function(element){
  element.setStyle('display','none');
  });
}

function flashMovieLegal(remember)
{
  //show the links on the HTML of the page
  showLinks();
  
  //set a cookie for this session so links appear on homepage again
  Cookie.set('legalDrinkingAgeTemporary', 'Yes');
  //if remember = yes then run the function to set the cookie
  if (remember == "rememberYes"){rememberLegal("flash")};
  
}

function showLinks()
{
  $$('.navigation ul').each(function(element){
    element.removeClass('homepage_hide');
  })
}


function oldEnough()
{
  //check either cookie contains legal drinking age in order to skip form
  if (Cookie.get('legalDrinkingAge') == 'Yes' || Cookie.get('legalDrinkingAgeTemporary') == 'Yes'){return "Yes"}
}


function rememberLegal(type)
{
  //only check for the input box if this function is being called outside of flash
  if (type == "nonFlash"){
    if ($('rememberLegal').getProperty('checked') == true){
      Cookie.set('legalDrinkingAge', 'Yes', {duration: 365});
    }
  }
  else
  { 
    //if its inside of flash just set the cookie
    Cookie.set('legalDrinkingAge', 'Yes', {duration: 365});
  }
  //set a cookie for this session so links appear on homepage again
  Cookie.set('legalDrinkingAgeTemporary', 'Yes');
}

//index actions
window.addEvent('domready', function() {
  url = window.location.pathname;
  if (url == "/" || url.match('index') || url =="/clients/tiopepe/")
  {
    //if flash is detected or if a cookie is already set then redirect to home
    if (GetSwfVer() != -1 || Cookie.get('legalDrinkingAge') == 'Yes')
    {
      document.location.href='home.html'
    }
    else
    {
      $('index').setStyle('display','block');
    }
  }


//home page checks for cookie
  if (url.match("home") && oldEnough() == 'Yes'){showLinks();};
});


//update total on singular product for
window.addEvent('domready', function() {
  if (window.location.pathname.match('order'))
  {
    //get total from price column
    var price = $('price').get('text').trim();
    
    price = parseFloat(price.replace(/^./, ""));

    //set listener on select

    $$('select').addEvent('change', function(){
  
    //get select value
    quantity = parseFloat($('item_quantity_1').getProperty('value'));

    sub_total = price * quantity
    
    //replace price with new total
    $('price').setHTML("&pound;" + sub_total.round(2));

    var grand_total = (price * quantity + 12);

    //replace grand total with new total
    $('grand_total').setHTML("&pound;" + grand_total.round(2));

    });
  }
});