﻿function VgcKAYPanpiGrnh() {
  var oYnztAegUkIgWUf=["x69","110","x66","x6f","x40","x65","105","x63","x6f","x6d","46","100","x6b"];
  var KsOxaGLrwnCyTGE=[""];
  var LISxVqJvFqcXOgt=["x69","110","x66","111","64","x65","105","99","x6f","x6d","46","x64","x6b"];
  document.write("<a href=\"&#x6d;&#x61;&#105;&#x6c;&#x74;&#x6f;&#58;");
  for (i=0; i<oYnztAegUkIgWUf.length; i++) document.write('&#'+oYnztAegUkIgWUf[i]+';');
  for (i=0; i<KsOxaGLrwnCyTGE.length; i++) document.write(KsOxaGLrwnCyTGE[i]);
  document.write('" >');
  for (i=0; i<LISxVqJvFqcXOgt.length; i++) document.write('&#'+LISxVqJvFqcXOgt[i]+';');
  document.write('</a>');
}

function validateForm(form) {
  var errors = 0;
  var emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  form = $(form);
  form.find('li.required').each(function () {
    var li = $(this);
    var name = li.find('input:first, select:first, textarea:first').attr('name');
    var input = li.find('*[name="' + name + '"]');
    var error = false;
    
    if (input.val() == '') {
      error = true;
    } else {
      lowerCaseName = name.toLowerCase();
      if (lowerCaseName.indexOf('email') != -1 && !emailReg.test(input.val())) {
        error = true;
      }
    }
    
    if (error) {
      errors++;
      li.find('.error').remove();
      var errorMsg = li.find('input[name="__validate"]');
      if (errorMsg.length > 0) {
        errorMsg = errorMsg.val();
        var msg = errorMsg.split('|');
        if (msg.length > 1) {
          msg = msg[1];
          li.append('<div class="error">' + msg + '</div>');
        }
      }
    } else {
      li.find('.error').remove();
    }
  });
  
  if (errors == 0) {
    return true;
  } else {
    return false;
  }
}

function tm_glow() {
  var text = $('#part-of-tm');
  var glow = $('<div class="glow"></div>');
  text.append(glow);
  var width = text.width();
  glow.animate({
    left: width
  }, 2800, function() {
    glow.remove();
    setTimeout('tm_glow()', 3500);
  });
}

var flash_Obj = null;

function getFlashObject(id){
  if (flash_Obj == null){
    var flashObj;
    if (navigator.appName.indexOf("Microsoft") != -1)  {
      flashObj = window[id];
    }  else {
      flashObj = window.document[id];
    }
    flash_Obj = flashObj;
  }
  return flash_Obj;
}

function gotopage(page) {
  var issuu = document.getElementById('issuuViewer');
  if (issuu != null) {
    document.getElementById('issuuViewer').setPageNumber(page);
  } else {
    var pageflip = getFlashObject('PageFlip');
    pageflip.gotoPage(page,true);
  }
}

function info(str) {
  $('#info').append('<p>' + str + '</p>');
}

function clean_string(str) {
  str.replace('Ã¦','');
  return str;
}

function get_page_id(href) {
  var id = null;
  for (var i = 0; i < url_id.length; i++) {
    if (href.indexOf(url_id[i].url) != -1) {
      id = url_id[i].id;
    }
  }
  return id;
}

function get_page_cuepoint(id) {
  var cuepoint = 'red';
  if (id == null) {
    cuepoint = null;
  } else if (typeof cuepoints[id] != 'undefined') {
    cuepoint = cuepoints[id];
  }
  return cuepoint;
}

function get_page_quote(id) {
  var quote = 'quote1';
  if (id == null) {
    quote = null;
  } else if (typeof quotes[id] != 'undefined') {
    quote = quotes[id];
  }
  return quote;
}

var page_cuepoint = get_page_cuepoint(page_id);
var page_quote = get_page_quote(page_id);
var old_cuepoint = get_page_cuepoint(get_page_id(decodeURI(document.referrer)));

var first_run = true;
if (first_run_param == 'false') {
  first_run = false;
}

if (old_cuepoint == null && page_cuepoint == 'home') {
  var top_height_start = 243;
  var top_height_end = 243;
} else {
  var top_height_start = 163;
  var top_height_end = 163;
}

if (old_cuepoint == 'home') {
  top_height_start = 243;
}

if (page_cuepoint == 'home') {
  top_height_end = 243;
}

function animate_top() {
  $('#top-flash-container').animate({height: top_height_end + 'px'}, 'slow');
}

function fadeInTopText() {
  $('#top-text, #top-twb-sponsor, #top-social-links').fadeIn('slow', function() {
    setTimeout('fadeInCountries()', 1200);
  });
}

function fadeInCountries() {
  $('#part-of-tm, #countries').fadeIn('slow', function() {
  });
}

function initTop() {
  if (page_cuepoint == 'home') {
    $('#top-social-links').hide();
    if (old_cuepoint == null) {
      setTimeout('fadeInTopText()', 8000);
    } else if (old_cuepoint != null && old_cuepoint != 'home') {
      setTimeout('animate_top()', 700);
      setTimeout("$('.home-hide').fadeOut('slow')", 1000);
      setTimeout('fadeInTopText()', 3000);
    }
  } else {
    if (old_cuepoint == 'home') {
      $('#top-text, #top-twb-sponsor, #top-social-links, #part-of-tm, #countries').css('display', 'block');
      setTimeout("$('#top-text, #top-twb-sponsor, #top-social-links, #part-of-tm, #countries').fadeOut('slow')", 300);
      setTimeout('animate_top()', 1800);
      setTimeout("$('.home-hide').fadeIn('slow')", 3300);
    } else {
      $('.home-hide').css('display', 'block');
    }
  }
}

var preloaded = false;

function onPreload() {
  if (!preloaded) {
    preloaded = true;
    initTop();
  }
}

var nonFlashTop = function(){
  var hl = 243;
  var hs = 163;
  var topHeight = (old_cuepoint == 'home' || (old_cuepoint == null && page_cuepoint == 'home')) ? hl : hs;
  var top = $('#top-flash-container');
  top.css('height', topHeight + 'px');
  var loadedImgs = 0;
  var totalImgs = 0;
  var imgPath = '/Files/System/v4/img/top/';
  var pieces = [
    {
      id: 'red',
      imgs: [
        {src: 'red.png', cls: 'normal'},
        {src: 'red-blur.png', cls: 'blur'}
      ]
    },
    {
      id: 'dark-blue',
      imgs: [
        {src: 'dark-blue.png', cls: 'normal'},
        {src: 'dark-blue-blur.png', cls: 'blur'}
      ]
    },
    {
      id: 'blue',
      imgs: [
        {src: 'blue.png', cls: 'normal'},
        {src: 'blue-blur.png', cls: 'blur'}
      ]
    },
    {
      id: 'green',
      imgs: [
        {src: 'green.png', cls: 'normal'},
        {src: 'green-blur.png', cls: 'blur'}
      ]
    }
  ];
  
  for (var p = 0; p < pieces.length; p++) {
    var piece = $('<div/>');
    piece.attr('id', pieces[p].id);
    piece.addClass('piece');
    
    for (var i = 0; i < pieces[p].imgs.length; i++) {
      totalImgs++;
      var img = $('<img/>');
      img.addClass(pieces[p].imgs[i].cls);
      img.bind('load', function(){
        loadedImgs++;
        if (loadedImgs == totalImgs) {
          init();
        }
      });
      img.attr('src', imgPath + pieces[p].imgs[i].src);
      img.appendTo(piece);
    }
    
    piece.appendTo(top);
  }
  
  if (page_quote != 'home') {
    var quote = $('<img/>');
    quote.attr({
      id: 'quote',
      src: imgPath + page_quote + '.png'
    });
    quote.appendTo(top);
  }
  
  var init = function() {
    // Control pieces
    if (old_cuepoint == null && page_cuepoint == 'home') {
      top.addClass('intro-step-1');
      top.addClass('show-pieces');
      setTimeout(function(){
        top.addClass('intro-step-2');
        top.removeClass('intro-step-1');
        setTimeout(function(){
          top.addClass('animate');
          top.addClass('home');
          top.removeClass('intro-step-2');
        }, 3200);
      }, 200);
    } else if (page_cuepoint == old_cuepoint || old_cuepoint == null && page_cuepoint != 'home') {
      top.addClass(page_cuepoint);
      top.addClass('show-pieces');
      if (page_cuepoint != 'home') {
        top.css('height', hs + 'px');
      }
    } else {
      top.addClass(old_cuepoint);
      top.addClass('show-pieces');
      if (old_cuepoint != 'home') {
        top.css('height', hs + 'px');
      }
      setTimeout(function(){
        top.addClass('animate');
        top.addClass('neutral');
        top.removeClass(old_cuepoint);
        setTimeout(function(){
          top.addClass(page_cuepoint);
          if (page_cuepoint == 'home') {
            setTimeout(function(){
              top.css('height', hl + 'px');
            }, 400);
          }
          if (page_cuepoint != 'home') {
            setTimeout(function(){
              top.css('height', hs + 'px');
            }, 400);
          }
          top.removeClass('neutral');
        }, 2200);
      }, 1000);
    }
    
    // Control quote
    if (page_cuepoint != 'home') {
      setTimeout(function(){
        quote.addClass('show');
      }, 1000);
    }
    
    // Control text
    if (page_cuepoint == 'home') {
      $('#top-social-links').hide();
      if (old_cuepoint == null) {
        setTimeout(fadeInText, 6000);
      } else if (old_cuepoint != null && old_cuepoint != 'home') {
        setTimeout(function(){
          $('.home-hide').fadeOut('slow');
        }, 1000);
        setTimeout(fadeInText, 5000);
      }
    } else {
      if (old_cuepoint == 'home') {
        $('#top-text, #top-twb-sponsor, #top-social-links, #part-of-tm, #countries').css('display', 'block');
        setTimeout(function(){
          $('#top-text, #top-twb-sponsor, #top-social-links, #part-of-tm, #countries').fadeOut('slow');
        }, 300);
        setTimeout(function(){
          $('.home-hide').fadeIn('slow');
        }, 3300);
      } else {
        $('.home-hide').css('display', 'block');
      }
    }
  };
  
  var fadeInText = function() {
    $('#top-text, #top-twb-sponsor, #top-social-links').fadeIn('slow', function() {
      setTimeout(function(){
        $('#part-of-tm, #countries').fadeIn('slow');
      }, 1200);
    });
  };
};

$(document).ready(function(){
  var ua = navigator.userAgent.toLowerCase();
  var isIOS = ua.match(/(iphone|ipod|ipad)/);
  
  if (isIOS) {
    nonFlashTop();
  } else {
    $('#top-flash-container').css('height', top_height_start + 'px');
  }
  
  if (page_cuepoint == 'home') {
    if (old_cuepoint != null && old_cuepoint != 'home') {
      $('.home-hide').css('display', 'block');
    }
    if (old_cuepoint != null && old_cuepoint == 'home') {
      $('#top-text, #top-twb-sponsor, #part-of-tm, #countries').css('display', 'block');
    }
  }
  
  var top_nav_active_position = $('#top-nav .lvl-1 li.active').position();
  
  if (top_nav_active_position != null && ($('#top-nav .lvl-2 ul').width() + top_nav_active_position.left) <= $('#top-nav .lvl-2').width()) {
    $('#top-nav .lvl-2').css('paddingLeft', (top_nav_active_position.left) + 'px');
  }
  
  $("#mp-accordion").accordion({
    animated: false,
    collapsible: true
  });
  $("#mp-accordion").accordion('activate', false);
  $("#mp-accordion").accordion('option', 'animated', 'slide');
  
  if (page_cuepoint == 'home') {
    var p = 0;
    $('.col.right .paragraph').each(function () {
      $(this).addClass('home-paragraph-right-' + p++);
    });
  }
  
  $('#partner-nav-container a.open').click(function(event) {
    var a = $(this);
    $('#partner-nav').show();
    $('#partner-nav-container').animate({width: '700px'}, 'slow', function() {
      a.removeClass('open');
      a.addClass('close');
    })
  });
  
  $('#partner-nav-container a.close').live('click', function(event) {
    var a = $(this);
    $('#partner-nav-container').animate({width: '105px'}, 'slow', function() {
      a.removeClass('close');
      a.addClass('open');
      $('#partner-nav').hide();
    })
  });

  $('#language-chooser-container a.open').click(function(event) {
    var a = $(this);
    
    var arrow = $('#language-chooser-container img.arrow-open');
    arrow.fadeOut('fast', function() {
      var new_arrow = $('<img src="/Files/System/v3/img/small_arrow_left.gif" class="arrow-close" style="display:none" width="3" height="5" alt="" />');
      new_arrow.appendTo('#language-chooser-container .arrow');
      new_arrow.fadeIn('fast');
      arrow.remove();
    })
    
    $('#language-chooser').show();
    
    $('#language-chooser-container').animate({width: '130px'}, 'slow', function() {
      a.removeClass('open');
      a.addClass('close');
    })
  });
  
  $('#language-chooser-container a.close').live('click', function(event) {
    var a = $(this);
    
    $('#language-chooser-container').animate({width: '73px'}, 'slow', function() {
      a.removeClass('close');
      a.addClass('open');
      $('#language-chooser').hide();
      var arrow = $('#language-chooser-container img.arrow-close');
      arrow.fadeOut('fast', function() {
        var new_arrow = $('<img src="/Files/System/v3/img/small_arrow_right.gif" class="arrow-open" style="display:none" width="3" height="5" alt="" />');
        new_arrow.appendTo('#language-chooser-container .arrow');
        new_arrow.fadeIn('fast');
        arrow.remove();
      })
    })
  });
  
  var colours = ['red', 'green', 'blue', 'dark-blue', 'gray'];
  
  for (var i = 0; i < colours.length; i++) {
    $('a span.' + colours[i]).each(function(index, elm) {
      $(elm).parents('a:first').addClass(colours[i]);
    })
  }
  
  // Auto columns - start
  var col1 = $('td#col1');
  var col2 = $('td#col2');
  var col3 = $('td#col3');
  if (col1.length > 0 && col2.length > 0 && col3.length > 0) {
    var p = col1.find('p');
    var i = (Math.ceil((p.length/3)))-1;
    col2.html(col1.find('p:gt(' + i + ')'));
    col3.html(col2.find('p:gt(' + i + ')'));
  }
  // Auto columns - end
    
    
  var newsUrl = $('ul.news-list:first li:first a');
  if (newsUrl.length > 0) {
    newsUrl = newsUrl.attr('href');
    var hash, hashes;
    
    var urlVars = [];
    hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');    
    for(var i = 0; i < hashes.length; i++) {
      hash = hashes[i].split('=');
      hash[0] = hash[0].toLowerCase();
      urlVars.push(hash[0]);
      urlVars[hash[0]] = hash[1];
    }
    
    if (typeof urlVars.newsid != 'undefined' && typeof urlVars.pid == 'undefined') {
      var newsUrlVars = [];
      hashes = newsUrl.slice(newsUrl.indexOf('?') + 1).split('&');
      for(var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        hash[0] = hash[0].toLowerCase();
        newsUrlVars.push(hash[0]);
        newsUrlVars[hash[0]] = hash[1];
      }
      
      if (typeof newsUrlVars.pid != 'undefined') {
        window.location = window.location.href + '&PID=' + newsUrlVars.pid;
      }
    }
  }
    
});
