jQuery(document).ready(
  function () {
    var clsNm = "active-module"; /*CSS attribute used to determine the page the user is on*/
    var ImgNm = ""; /*Name of background image to load*/
    
    /*Get class attributes for menu items*/
    /*The active-module css class attribute will be used 
      to determine the page the user is currently on.*/
    var home = getEl("#moduleContentWrapper5812294");
    var ourclients = getEl("#moduleContentWrapper5813068");
    var ourpeople = getEl("#moduleContentWrapper5813095");
    var ourexperts = getEl("#moduleContentWrapper6218373");
    var ourstory = getEl("#moduleContentWrapper5813087");
    var careers = getEl("#moduleContentWrapper5813100");
    var contactus = getEl("#moduleContentWrapper5813104");
    var newsandviews = getEl("#moduleContentWrapper5957254");
    var mediacentre = getEl("#moduleContentWrapper6109829");
        
    /*Get class attributes for our-client pages*/
    /*The active-module css class attribute will be used 
      to determine the page the user is currently on.*/
    var greenNblacks = getEl("#moduleContentWrapper6023508");
    var winesGermany = getEl("#moduleContentWrapper6023823");
    var lanson = getEl("#moduleContentWrapper6023827");
    var veetee = getEl("#moduleContentWrapper6023848");
    var jroyals = getEl("#moduleContentWrapper6023871");
    var rioja = getEl("#moduleContentWrapper6023850");
    var apple = getEl("#moduleContentWrapper6023869");
    var teacherwhisky = getEl("#moduleContentWrapper6023830");
    var kerrygold = getEl("#moduleContentWrapper6023878");
    var jackson = getEl("#moduleContentWrapper6218383");
    var iwsc = getEl("#moduleContentWrapper6218385");
    
    /*Background Images Array*/
    var bkImgs = new Array();
    bkImgs[0] = 'home_back.jpg';
    bkImgs[1] = 'phipps_background_lanson.jpg';
    bkImgs[2] = 'phipps_background_wofg.jpg';
    bkImgs[3] = 'phipps_background_jr.jpg';
    bkImgs[4] = 'phipps_background_wofr.jpg';
    bkImgs[5] = 'pour_wine.jpg';
    bkImgs[6] = 'phipps_background_teachers.jpg';
    bkImgs[7] = 'phipps_background_appletiser.jpg';
    bkImgs[8] = 'phipps_background_veetee.jpg';
    
    bkImgs[10] = 'phipps_background_careers.jpg';
    bkImgs[11] = 'phipps_background_fg.jpg';
    bkImgs[12] = 'phipps_background_kg.jpg';
    bkImgs[13] = 'phipps_background_mc.jpg';
    bkImgs[14] = 'phipps_background_mu.jpg';
    bkImgs[15] = 'phipps_background_nv.jpg';
    bkImgs[16] = 'phipps_background_oa.jpg';
    bkImgs[17] = 'phipps_background_expertpanel.jpg';
    bkImgs[18] = 'phipps_background_contactus01.jpg';
    bkImgs[19] = 'IWSC_background_iwsc.jpg';
    bkImgs[20] = 'Jackson_background_je.jpg';
    
    
    /*Set page background image ----------------------------*/
    var canvas = $("#canvas"); /*Attach Img to canvas*/
    switch(clsNm) 
    {
      case home[1]:
        /*image rotation*/
        var rnum = Math.floor((4-0)*Math.random()); /*randomises between first 4 imgs in arr*/
        ImgNm = bkImgs[rnum];
      break;
        
      case ourclients[1]:
        /*image rotation*/
        var rnum = Math.floor((8-4)*Math.random()+4); /*randomises between last 5 imgs in arr*/
        ImgNm = bkImgs[rnum];
      break;
      
      case ourstory[1]:
        ImgNm = bkImgs[11];
        break;
      
      case ourpeople[1]:
        $("#sectionContent2604707 ul li:first-child").css(getArchiveMenuCss());
        highlightMenuLink("moduleContent5846516");
        ImgNm = bkImgs[14];
        break;
        
      case ourexperts[1]:
        $("#sectionContent2748775 ul li:first-child").css(getArchiveMenuCss());
        highlightMenuLink("moduleContent6227819");
        ImgNm = bkImgs[17];
        break;
        
      case careers[1]:
        ImgNm = bkImgs[10];
        break;
        
      case contactus[1]:
        ImgNm = bkImgs[18];
        geo("Phipps PR, 17 Exeter Street, London, WC2E 7DU"); //create map
        break;
        
      case newsandviews[1]:
        ImgNm = bkImgs[15];
        break;
        
      case mediacentre[1]:
        $("#static-txt").html(
          "Here you'll find a library of our recent press releases.\
          If you'd like to be kept up to date automatically then \
          subscribe to our RSS feed. If you need any more information \
          on any of our clients then contact us on \
          <a href=\"mailto:askus@thisisphipps.com\">askus@thisisphipps.com</a>."
        ).show();
        ImgNm = bkImgs[13];
        break;

      case greenNblacks[1]:
        ImgNm = bkImgs[0];
        break;

      case lanson[1]:
        ImgNm = bkImgs[1];
        break;

      case winesGermany[1]:
        ImgNm = bkImgs[2];
        break;

      case veetee[1]:
        ImgNm = bkImgs[8];
        break;

      case jroyals[1]:
        ImgNm = bkImgs[3];
        break;

      case rioja[1]:
        ImgNm = bkImgs[4];
        break;

      case teacherwhisky[1]:
        ImgNm = bkImgs[6];
        break;
        
      case apple[1]:
        ImgNm = bkImgs[7];
        break;
        
      case kerrygold[1]:
        ImgNm = bkImgs[12];
        break;
      
      case iwsc[1]:
        ImgNm = bkImgs[19];
        break;
        
      case jackson[1]:
        ImgNm = bkImgs[20];
        break;
      
      default:
        ImgNm = bkImgs[5];
      break;
    }
        
    /*Append Image*/
    var image = document.createElement('img');
    
    if( document.getElementById('lb-background-image') ) { image.src = document.getElementById('lb-background-image').src; $('#moduleContentWrapper5957254').hide(); }
    else { image.src='/storage/background_images/'+ImgNm; }
    
    if( $('#lb-hide-news').length ) { $('#moduleContentWrapper5957254').hide(); }

    image.className='backImg';
    canvas.append(image);
    
    /*Use the maxImage script to adjust background 
      according to browser window size*/
    $('img.backImg').maxImage({
      isBackground: true, 
      verticalAlign: 'top',
      overflow: 'auto'
    });
    
    /*Make left menu text on IE browsers slightly smaller*/
    if($.browser.msie) {
      var ieRules = { 'font-size' : '12px' };
      $("#sectionContent2672847 a").css(ieRules);
    }
    
    /*Remove default 'share this' image*/
    $(".stbutton").removeClass();
    $(".stico_default").removeClass();
  }
);

/*This function highlights the selected left menu item 
  for the "our experts" and "our people" journals*/
function highlightMenuLink(src)
{
  /*Hightlight selected link*/
  var nm = $(".single-journal-entry-wrapper .title a").text();
  var re = new RegExp("[^a-zA-Z\-\'\.]", "g");
  nm = nm.replace(re, " ");
  
  if(src == "moduleContent6227819")
    var get = '#moduleContent6227819 ul li a'; /*Our Experts*/
  else
    var get = '#moduleContent5846516 ul li a'; /*Our People*/
  
  $(get).each(function() {
    var item = $(this).text();
    if(item.trim() == nm.trim()) {
      $(this).css('color', '#ED174F');
    }
  });
}

/*Utility Functions --------------------------------*/    
/*Retuns element if it exists, otherwise it will return an empty string*/
function getEl(elNm) {
  return ($(elNm).length != 0) ? $(elNm).attr("class").split(" ") : "";
}

function getArchiveMenuCss(){
  return { 'border' : '0', 'margin-top' : '-23px' };
}

function geo(addr)
{
  var geocoder = new google.maps.Geocoder();
  var map;
  
  if (geocoder) {
    geocoder.geocode( { 'address': addr}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var addr = {
          zoom: 15,
          center: results[0].geometry.location,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }  
        map = new google.maps.Map($("#map_canvas")[0], addr);
        
        var marker = new google.maps.Marker({
          map : map, 
          position: results[0].geometry.location
        });
      } else {
        $("#map_canvas").html("An error has occured whilst loading this map.");
      }
    });
  }
}
