var copyright_date = '1999-2009';

var billboards = new Object();
var spotlights = new Object();
var headlines = new String();
var autoForward;
var country_name='worldwide';
var translationsConfig = '/includes/home/js/home_text_en.js';
var newsConfig = 'includes/js/newstool/newstool_na_cala/newsbase.js';
var homeFlash = 'multimedia/flash/home/main/970x190_homepage_player.swf';
var flashConfigDir = 'multimedia/flash/home/main/config/';
var billboardConfigDir = 'includes/home/xml/';

var segments = flashConfigDir + language + '_segments.xml';
var features = flashConfigDir + country_name + '_features.xml';
var rss_link = 'help/rss/index.html';

/* overrides for rss link in flash header */
var rssLinks = {
  japan: '',
  china: '',
  taiwan: ''
}

if (rssLinks[country_name]) { rss_link = rssLinks[country_name]; }

/* prevent jquery from interfering with milonic */
jQuery.noConflict();

/* things to do when the document is ready */
jQuery(document).ready(function() {

  // try to load the newstool file;
  // if the newstool file is broken,
  // build the hpg flash with a blank
  // headline string

  jQuery.ajax(
    {
      type: 'GET',
      url: newsConfig,
      dataType: 'script',
      success: function() { 
        headlines = buildHeadlines();
        buildHPG(headlines);
      },
      error: function() {
        headlines = 'HERE';
        buildHPG(headlines);
      }
    }
  );

  /* load the translations */

  jQuery.getScript(
    translationsConfig,
    function () {
    //  jQuery('#search_box').append(makeSearchBox());
     // doFooter();
      doChangeLocation();
      doLogin();
    }
  );

  /* build the billboards and spotlight */

  jQuery.each(configFiles, function(position,config) {

    var type = config['type'];
    var file = billboardConfigDir + config['file'] + '.xml';

    switch (type) {
      case 'billboard':
        var targetHeight = 272;
        billboards[position] = new Billboard(position,file,targetHeight);
        billboards[position].init();
        break;

      case 'spotlight':
        spotlights[position] = new Spotlight(position,file);
        spotlights[position].init();
        break;
    }

  });

  // set the billboards to auto-advance 
  autoForward = setTimeout( function() { jQuery.each( billboards, function(p,o) { o.forwardNav(); }) }, 10000); 

  // reposition the footer for long country navs
  var footer_top = 625 - 272 + jQuery('#position_1').height();
  if (footer_top > 625) { jQuery('#footer').css({top: footer_top}); }

});

function buildHeadlines() {

  var headlinesArray = new Array();
  var country_id;

  if (region == 'emea') {

    country_NewsList = buildEmeaCountryNewsList();
    country_id = getEmeaCountryId();
    countryNews = country_NewsList[country_id];

  } else {

    if (region == 'apac') {
      country_NewsList = buildApacCountryNewsList();
    }

    for (i=0; i<country_NewsList.length; i++) { if (country_NewsList[i][0] == news_country) { country_id = i; } }

    countryNews = country_NewsList[country_id];
    countryNews = countryNews.slice(1,countryNews.length);

  }

  for (i=0; i<countryNews.length; i += 2) {
    var myUrl = countryNews[i];
    var myHeadline = 'TEST';//countryNews[i+1];

    myHeadline = myHeadline.replace(/&#(\d+);/g, function(whole,match) { return String.fromCharCode(match);});

    headlinesArray[headlinesArray.length] = myUrl;
    headlinesArray[headlinesArray.length] = myHeadline;
  }

  var myHeadlines = headlinesArray.join(':::');
  return(myHeadlines);
  
}

function buildEmeaCountryNewsList() {
  var country_NewsList = [ country_NewsList0,country_NewsList1,country_NewsList2,country_NewsList3,country_NewsList4, country_NewsList5, country_NewsList6,country_NewsList7,country_NewsList8,country_NewsList9,country_NewsList10, country_NewsList11, country_NewsList12,country_NewsList13,country_NewsList14,country_NewsList15,country_NewsList16, country_NewsList17,country_NewsList18,country_NewsList19,country_NewsList20,country_NewsList21, country_NewsList22, country_NewsList23,country_NewsList24,country_NewsList25,country_NewsList26, country_NewsList27 ];
  return(country_NewsList);
}

function buildApacCountryNewsList() {
  var country_NewsList = [ country_NewsList0,country_NewsList1,country_NewsList2,country_NewsList3,country_NewsList4, country_NewsList5, country_NewsList6,country_NewsList7,country_NewsList8,country_NewsList9,country_NewsList10, country_NewsList11, country_NewsList12,country_NewsList13,country_NewsList14,country_NewsList15];
  return(country_NewsList);
}

function getEmeaCountryId() {

  var countries = new Object();

  countries = { Austria: 0, Belgium: 1, Benelux: 1, CIS: 2, CISRu: 3, Denmark: 4, EEurope: 5, EMed: 6, Finland: 7, France_En: 8, France_Fr: 9, Germany: 10, Ireland: 11, Israel: 12, Italy: 13, MEA: 14, MENA: 14, Netherlands: 15, Norway: 16, Poland: 17, Portugal: 18, SouthAfrica: 19, Spain: 20, Sweden_En: 21, Sweden_Sw: 22, Switzerland_De: 23, Switzerland_Fr: 24, Turkey: 25, Turkey_En: 26, Turkey_Tr: 25, UK: 27 };

  var country_id = countries[news_country];
  return(country_id);
}

function buildHPG(headlines) {

  var solExt;

  if (language == 'en') { solExt = ''; } else { solExt = '_' + language; }

  jQuery('#homepage_player').empty().append(
    '<a href="/solutions/index' + solExt + '.html">' +
    '<img src="http://www.nortel.com/images/970x190/flash_altimages/970x190_noflash' + solExt + '.jpg" width="970" height="190" />' +
    '</a>'
  );

  jQuery('#homepage_player').flash(
    { 
      src: homeFlash, 
      width: 970,
      height: 190,
      wmode: 'opaque',
      scale: 'noscale',

      flashvars: { 
        language: language,
        headlines: unescape(headlines),
        segmentsxml: segments,
        messagesxml: features,
        rss_link: rss_link
      }

    },

    { update: false }

  );
} 

function doFooter() {

  populateElement('footer_site_map',text['site_map']['text'],text['site_map']['href']);
  populateElement('footer_privacy',text['privacy']['text'],text['privacy']['href']);
  populateElement('footer_contact_us',text['contact_us']['text'],text['contact_us']['href']);
  populateElement('footer_terms',text['terms']['text'],text['terms']['href']);
  populateElement('copyright',text['copyright']['text'],false);

}

function doChangeLocation() {
  populateElement('change_location',text['change_location']['text'],false);
}

function doLogin() {
  populateElement('login',text['login']['text'],false);
}

function populateElement(id,text,href) {
  if (href) { jQuery('#' + id).attr('href',href).empty().append(text); } 
  else { jQuery('#' + id).empty().append(text); }
}
