/* ------------------------------------------------------------
   Maintains the query string. Used to sublink into the site
   ------------------------------------------------------------ */
var qVars = new Object();

function parseQuery( p_qstr ) {
	var pairHalves;
	var queryVars = {};
	if (p_qstr) {
		var N_V_pairs = p_qstr.split("&");
		var l = N_V_pairs.length;
		for(i=0; i<l; i++){
			pairHalves = N_V_pairs[i].split("=");
			queryVars[pairHalves[0]] = pairHalves[1];
			for(d=2;d<pairHalves.length;d++) {
				queryVars[pairHalves[0]] += "=" + pairHalves[d];
			}
		}
	}
	return queryVars;
}

function buildQString( p_queryVars )
{
	var results = "";
	for( var item in p_queryVars ){
		if ( results.length > 0 )
		{
			results += "&";
		}
		if ( item.length > 0 )
		{
			results += item + "=" + p_queryVars[ item ];
		}
	}
	
	return results;
}

function cleanupQString( p_qString )
{
	if( p_qString && p_qString != null && p_qString != "" ) {
	
		if( p_qString.indexOf(";") > -1 ) {
			var temp_q_str = p_qString ;
			p_qString = temp_q_str.substring( 0, p_qString.indexOf(";") );
		}
		
		// remove the question mark
		p_qString = p_qString.substring( 1 );
	
	} else {
		p_qString = "";
	}
	
	return p_qString;
}

// grab the variables off the querystring and hash, build a new hash and pass it on to the history frame
// returns hash
function getHash()
{			
	var q_str = new String(window.location.search);
	var h_str = new String(window.location.href.split("#")[1]);
	var hash = "";
	var l = ""; // location
	var co = ""; // country
	var la = ""; // language
	var addition = "";
	var hashVars;
		
	q_str = cleanupQString(q_str);
			
	qVars = parseQuery(q_str);
	hashVars = parseQuery(h_str);
	
	if(window.location.hash.length > 1) 
	{
		if(hashVars["l"]) 
		{
			l = hashVars["l"];
		}
				
		if(hashVars["co"] && hashVars["la"]) 
		{
			co = hashVars["co"];
			la = hashVars["la"];
		}		
	} 
	else 
	{	
		if(qVars["l"]) 
		{
			l = unescape(escape(qVars["l"]));
		}
				
		if(qVars["co"] && qVars["la"]) 
		{
			co = unescape(escape(qVars["co"]));
			la = unescape(escape(qVars["la"]));
		}
	}
			
	if (l.length > 0) 
	{
		hash = "l=" + l;	
		addition = "&";		
	}
	
	if(co.length > 0 && la.length > 0) 
	{
		hash = hash + addition + "co=" + co + "&la=" + la;
	}
	return hash;
}

// variation on getHash used for SEO
// returns hash
function getHashFromString(q_str, h_str)
{			
	//var q_str = new String(window.location.search);
	//var h_str = new String(window.location.href.split("#")[1]);
	
	h_str = h_str.split("#")[1];
	
	var hash = "";
	var l = ""; // location
	var co = ""; // country
	var la = ""; // language
	var addition = "";
	var hashVars;
		
	q_str = cleanupQString(q_str);
			
	qVars = parseQuery(q_str);
	hashVars = parseQuery(h_str);
	
	if(window.location.hash.length > 1) 
	{
		if(hashVars["l"]) 
		{
			l = hashVars["l"];
		}
				
		if(hashVars["co"] && hashVars["la"]) 
		{
			co = hashVars["co"];
			la = hashVars["la"];
		}		
	} 
	else 
	{	
		if(qVars["l"]) 
		{
			l = unescape(escape(qVars["l"]));
		}
				
		if(qVars["co"] && qVars["la"]) 
		{
			co = unescape(escape(qVars["co"]));
			la = unescape(escape(qVars["la"]));
		}
	}
			
	if (l.length > 0) 
	{
		hash = "l=" + l;	
		addition = "&";		
	}
	
	if(co.length > 0 && la.length > 0) 
	{
		hash = hash + addition + "co=" + co + "&la=" + la;
	}
	return hash;
}


/* ------------------------------------------------------------
   Used for resizing objects when dealing with 100% Flash movies
   ------------------------------------------------------------ */

function getWinSize()
{
	var myWidth = 0, myHeight = 0;
      if( typeof( window.innerWidth ) == 'number' )
      {
         //Non-IE
         myWidth = window.innerWidth;
         myHeight = window.innerHeight;
      }
      else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
      {
          //IE 6+ in 'standards compliant mode'
          myWidth = document.documentElement.clientWidth;
          myHeight = document.documentElement.clientHeight;
      }
      else if(document.body && (document.body.clientWidth || document.body.clientHeight))
      {
          //IE 4 compatible
          myWidth = document.body.clientWidth;
          myHeight = document.body.clientHeight;
      }
      return {w:myWidth,h:myHeight}
      
}   
   
function adjustObjectSize(id,w,h)
{
      var winSizes = getWinSize();
      
      var tmpObject = document.getElementById(id);

      if(winSizes.w < w)
      {
         tmpObject.style.width = w + "px";
      }
      else
      {
         tmpObject.style.width = "100%";
      }
      if(winSizes.h < h)
      {
         tmpObject.style.height = h + "px";
      }
      else
      {
         tmpObject.style.height = (winSizes.h)-2 + "px";
      }
}

/* ------------------------------------------------------------
   Used for getting the users browser
   ------------------------------------------------------------ */

// browser detection
var _appName = navigator.appName;
var _appVersion = navigator.appVersion;
var _userAgent = navigator.userAgent.toLowerCase();

function getBrowser() {
      
      var browser = '';
      
      if ((_appName.indexOf('Microsoft') != -1) && (_userAgent.indexOf('mac') == -1)) {
		  browser = 'IE';
	  // We're treating MAC IE as a special case anymore
	  } else if ((_appName.indexOf('Microsoft') != -1) && (_userAgent.indexOf('mac') != -1)) {		
	      browser = 'Other';
	  } else if (_userAgent.indexOf('safari') != -1) {
		  browser = 'Safari';
	  } else if (_userAgent.indexOf('firefox') != -1) {
		  browser = 'Firefox';
	  } else {
		  browser = 'Other';
	  }
	  return browser;
}

/* ------------------------------------------------------------
   Used for getting the relative path
   ------------------------------------------------------------ */
function getRelativePath( pPath)
{
	var count = 0;
	var url = String( pPath );
	var len = url.length;
	var i;
	var found = false;
	var tempStr;
	var endStr = ".html";
			
	for ( i=0; i<len; i++ )
	{
		if ( url.charAt(i) == "index-2.html" )
		{
			count++;
		}

		if( count == 3 )
		{			
			found = true;		
			tempStr =  url.slice( i, len );
			break;
		}
	}
	
	if ( found )
	{
		var end = tempStr.indexOf( endStr );
		tempStr = tempStr.slice( 0, end + endStr.length );
		return tempStr;
	}
	else
	{
		return url;
	}
}

/*
 Used for opening a new window from Flash
*/

function doNewWindow(strPath, winName, strInfo) 
{
	if (winName == undefined || winName == null) {
		winName = "";
	}
	//IE can't allow a winname with a space. -JH
	winName = winName.split(" ").join(""); 
	
	if ( strInfo == undefined || strInfo == null || strInfo == "" )
		strInfo = "titlebar=yes,status=yes,menubar=yes,location=yes,toolbar=yes,resizable=yes,scrollbars=yes,width=800,height=600";
	
	mainwindow = window.open(strPath, winName, strInfo);
}

/*
 Used for opening a parent window from Flash
*/

function openInParentWindow(strPath, winName, strInfo) 
{
	if (window.opener && !window.opener.closed)
	{
		window.opener.document.location = strPath;
	}
	else
	{
		mainwindow = null;
		doNewWindow(strPath, winName, strInfo);
	}
}
	
	
		
/*
 Used on SEO index.html pages to parse the directory path and convert
 that into a parameter passed to the Flash movie.  For instance, if loaded
 on an index page located here:
 http://nseries.com/products/n95 
 then that would parse the directory path and load a Flash movie located here:
 http://nseries.com/main.html?l=products,n95
*/

function createFlashLinkFromSEO(flashPage, debug) {

	var flashLink = flashPage;
	
	// break dirs and page into variables, eliminating empty spaces
	var paths = location.pathname.split('index-2.html');
	var dirs = new Array();
	for (var i=0; i<paths.length; i++) {
		// make sure path is not empty and that it doesn't contain
		// a period (indicating that it is a file (e.g. index.html)
		// and it is not any of the paths to the actual website, that is,
		// 'nseries' or 'v3'.
		if ((paths[i] != '') && (paths[i] != 'nseries') && (paths[i] != 'v3') && (paths[i].indexOf('.') == -1)) {
				dirs[dirs.length] = paths[i];
		}
	}
	
	if (dirs.length) {
		// three qs options
		// 1. no prior qs, so we need to create one
		// 2. existing qs without the l param
		// 3. existing qs with the l param
		
		if (location.search == '') {
			flashLink = flashLink + '?l=' + dirs.toString();
		} else {
		
			var params = parseQuery(cleanupQString(location.search));
			if (params.l != null) {
				// there is an existing l value on the qs
				params.l = dirs.toString() + "," + params.l;
			} else {
				params.l = dirs.toString();
			}
			flashLink = flashLink + '?' + buildQString(params);
		}
	
	} else {
		// if we're in the root, then no modifications need to be made, and
		// we can just append the normal querystring
		flashLink = flashLink + location.search;
	}
	
	if (debug) {	
		document.write('<table><tr><td>location</td><td>' + location + '</td></tr>');
		document.write('<tr><td>location.hash</td><td>' + location.hash + '</td></tr>');
		document.write('<tr><td>location.host</td><td>' + location.host + '</td></tr>');
		document.write('<tr><td>location.hostname</td><td>' + location.hostname + '</td></tr>');
		document.write('<tr><td>location.href</td><td>' + location.href + '</td></tr>');
		document.write('<tr><td>location.pathname</td><td>' + location.pathname + '</td></tr>');
		document.write('<tr><td>location.port</td><td>' + location.port + '</td></tr>');
		document.write('<tr><td>location.protocol</td><td>' + location.protocol + '</td></tr>');
		document.write('<tr><td>location.search</td><td>' + location.search + '</td></tr>');
		document.write('<tr><td><b>FLASH LINK</b></td><td>' + flashLink + '</td></tr></table>');
	} else {
		return flashLink;
	} 	
	
}

//////BookMark functionlaity for N800 Campaign

function bookmark(urlArr, descriptionArr, urlArrLength)
{
		//if (navigator.appName=='Microsoft Internet Explorer')	
		if (window.sidebar) 
			{   // Firefox Bookmark
				for (var i = 0; i < urlArrLength; i++)
					{							
						var title = descriptionArr.split(",")[i];
						var url = urlArr.split(",")[i];
						window.sidebar.addPanel(title, url, "");
					}			
			} 
			else if( window.external ) 
			{    // IE Bookmark
					for (var i = 0; i < urlArrLength; i++)
					{		
						var url = urlArr.split(",")[i];
						var title = descriptionArr.split(",")[i];
						//document.write(url+ "<br />");
						window.external.AddFavorite(url, title);
					}			
			}
			else if (window.opera && window.print) 
			{ // Opera Bookmark		
				for (var i = 0; i < urlArrLength; i++)
					{
						var url = urlArr.split(",")[i];
						var title = descriptionArr.split(",")[i];				
						var elem = document.createElement('a');				
						elem.setAttribute('href',url);
						elem.setAttribute('title',title);
						elem.setAttribute('rel','sidebar');
						elem.click();	
				}
			} 		
	}
	
