// JavaScript Document
//Nokia Support global wayfinder script
//remembers your selected location and redirects you to it if the option was set..
//constructor

function WayFinder() {

//attributes / vars
this.localSupport = null ;
this.cookieName = 'wayfinder';
//this.expires = 1000;
// methods / functions
this.CreateCookie = setCookie; 
this.FindWay = getCookie;
this.SaveRegion = save_region;
this.SetLinks = set_link_listener;
this.HighlightSelection = highlight_selection;

//run inner functions
//if cookie is not found, make it. 
//place a trigger function to all support site links
this.SetLinks();

this.localSupport = this.FindWay(this.cookieName);
if(this.localSupport !== null) {
this.localSupport = this.localSupport.split('*');
this.HighlightSelection();
}
//if cookie contains a value, show that value in the special highlight area
// if(this.localSupport !== null && this.localSupport !== false && this.localSupport !== 'undefined') {
// 
// }

}


function setCookie(name, value, expires, path, domain, secure) {
   
    var date = new Date();
    date.setTime(date.getTime()+(expires*24*60*60*1000));
    document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + date.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
        
}



function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}


function set_link_listener() {
var regionLinks = Array();
  
  for(i=0; i < document.links.length; i++) {
    if(document.links[i].className == 'regionLink') {
      document.links[i].onclick = this.SaveRegion;
    }
  }
}

function highlight_selection() {
  var linkContainer = document.getElementById('lastVisited');
  linkContainer.innerHTML = '<h3>Go straight to: <a href="'+this.localSupport[2]+'">' + this.localSupport[1]+ '</a></h3>';
}

function save_region() {
var cookieName = 'wayfinder';
var expires = 1000;
var region = this.parentNode.parentNode.id;
setCookie(cookieName , region+'*' + this.innerHTML + '*' + this.href, expires);
}

