/**
 * A window opener
 */
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
/**
*Cookie functions
*/
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name,"",-1);
}
/**
 * Some helpers to work with DOM elements
 */
function $(element) {
  if (arguments.length > 1) {
    for (var i = 0, elements = [], length = arguments.length; i < length; i++)
    elements.push($(arguments[i]));
    return elements;
  }
  if (typeof element == 'string')
  element = document.getElementById(element);
  return element;
};
document.getElementsByClassName = function(className, parentElement) {
  var children = ($(parentElement) || document.body).getElementsByTagName('*');
  var elements = [], child;
  for (var i = 0, length = children.length; i < length; i++) {
    child = children[i];
    if (Element.hasClassName(child, className))
    elements.push(child);
  }
  return elements;
};

Element = {
  hasClassName: function(element, className) {
    if (!(element = $(element))) return;
    var elementClassName = element.className;
    if (elementClassName.length == 0) return false;
    if (elementClassName == className ||
    elementClassName.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
    return true;
    return false;
  },
  
  addClassName: function(element, className) {
    if (!(element = $(element))) return;
    var classNames = element.className;
    element.className = classNames + ' ' +className;
    return element;
  },
  
  removeClassName: function(element, className) {
    if (!(element = $(element))) return;
    element.className = element.className.replace(new RegExp("[\s]*" +className +"\\b"), "");
    return element;
  }
};

/**
 * Page workflow manager
 */
var PageManager = {
  /**
  * default value corresponding to the radio button
  * change it if you change the radio default checked value
  */
  selectedValue: '',
  switchPage: function(popupId, videoId, nvmCookie) {
    if(nvmCookie=="true") {
      this.videoId = videoId;
      var linkContainer = document.getElementById('links');
      var links = linkContainer.getElementsByTagName('a');
      if (this.videoId == "video1") {
        //alert("video1");
        //window.location = links[0].href;
        if(navigator.appName == "Microsoft Internet Explorer"){
          window.open(links[0].href);
        }else{
          try{
            window.location = links[0].href;
          }catch(err){
            popupId="popup_step2_no";
          }
        }
        return false;
      } else if (this.videoId == "video2") {
        //alert("video2");
        //window.location = links[1].href;
        if(navigator.appName == "Microsoft Internet Explorer"){
          window.open(links[1].href);
        }else{
          try{
            window.location = links[1].href;
          }catch(err){
            popupId="popup_step2_no";
          }
        }
        return false;
      } else if (this.videoId == "video3") {
        //alert("video3");
        //window.location = links[2].href;
        if(navigator.appName == "Microsoft Internet Explorer"){
          window.open(links[2].href);
        }else{
          try{
            window.location = links[2].href;
          }catch(err){
            popupId="popup_step2_no";
          }
        }
        return false;
      }
    }
    if (typeof videoId != "undefined") {
      this.videoId = videoId;
      for(var i = 1; i <= 3; i++) {
        var spanElement = document.getElementById("video" + i + "LinkInPopup2");
        if (spanElement && spanElement.style) {
          spanElement.style.display = (videoId.indexOf("video" + i) == 0) ? "inline" : "none";
        }
      }
    }
    var aPopups = document.getElementsByClassName('popup');
    for (var i = 0, length = aPopups.length; i < length; i++) {
      popup = aPopups[i];
      if (popup.id == popupId) {
        popup.style.display = "block";
        var currentPopup = popup;
      } else {
        popup.style.display = "none";
      }
    }
    // Position popups correctly
    if (this.videoId == 'video1') {
      currentPopup.style.left = '45px';
      Element.removeClassName(currentPopup, 'reversed');
    }
    if (this.videoId == 'video2') {
      currentPopup.style.left = '229px';
      Element.removeClassName(currentPopup, 'reversed');
    }
    if (this.videoId == 'video3') {
      currentPopup.style.left = '170px';
      Element.addClassName(currentPopup, 'reversed');
    }
    return false;
  },

  setOption: function(value) {
    PageManager.selectedValue = value;
  },
      
  validatePopup: function() {
    var popupId = 'popup_step2_' +PageManager.selectedValue;
    if(popupId == 'popup_step2_yes'){
      window.location= "http://nds1.nokia.com/files/support/global/phones/software/NokiaVideoManager_1_5_20.exe";
    }else{
      popupId="popup_step2_already";
      createCookie('nokiavideomanager','true',365);
      var linkContainer = document.getElementById('links');
      var links = linkContainer.getElementsByTagName('a');
      for (i = 0; i < links.length; i++) {
        if (links[i].parentNode.style.display == 'inline') {
          if(navigator.appName == "Microsoft Internet Explorer"){
            window.open(links[i].href);
          }else{
            try{
              window.location = links[i].href;
            }catch(err){
              popupId="popup_step2_no";
            }
          }
        }
      }
    }
    PageManager.switchPage(popupId);
    return false;
  },
  
  togglePage: function(step) {
    var body = document.getElementsByTagName('body')[0];
    body.id = 'demo_step' +step;
    // toggle popups
    var aPopups = document.getElementsByClassName('popup1');
    for (var i = 0, length = aPopups.length; i < length; i++) {
      popup = aPopups[i];
      if (popup.id == 'popup_step' +step) {
        popup.style.display = "block";
      } else {
        popup.style.display = "none";
      }
    }
    // Force pager to display in IE, cause changing body id in js
    // doesn't seem to be considered in CSS.
    // Another great bug from IE 6/7
    var pager = $('pager');
    if (step == 0) {
      pager.style.display = "none";
    } else {
      pager.style.display = "block";
    }
    return false;
  },
  
  closePage: function(popupId) {
    $(popupId).style.display = "none";
    return false;
  }
}

