// black magic written by Douglas Reed with some code from outside sources.
//
// JSONscriptRequest -- a simple class for making HTTP requests
// using dynamically generated script tags and JSON

// Constructor -- pass a REST request URL to the constructor
//
//when the page loads start the main function
window.onload = main;

//name of container div to fill--this may change depending on sears' preference
var elux_div = 'elux_container_div';

var elux_base_path = 'http://img1.electroluxappliances.com/media';
//var elux_base_path = 'http://172.30.10.35';

function JSONscriptRequest(fullUrl) {
  // REST request path
  this.fullUrl = fullUrl; 
  // Keep IE from caching requests
  if(fullUrl.indexOf("?")>-1)
  {
  	var noCacheVar = '&noCacheIE=';
  }
  else
  {
  	var noCacheVar = '?noCacheIE=';
  }
  
  this.noCacheIE = noCacheVar + (new Date()).getTime();
  // Get the DOM location to put the script tag
  this.headLoc = document.getElementsByTagName("head").item(0);
  // Generate a unique script tag id
  this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
JSONscriptRequest.prototype.buildScriptTag = function () {
  // Create the script tag
  this.scriptObj = document.createElement("script");

  // Add script object attributes
  this.scriptObj.setAttribute("type", "text/javascript");
  this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
  this.scriptObj.setAttribute("id", this.scriptId);
}

JSONscriptRequest.prototype.buildLinkTag = function () {
  // Create the script tag
  this.scriptObj = document.createElement("link");
  this.scriptObj.setAttribute("rel", "stylesheet");
  this.scriptObj.setAttribute("href", this.fullUrl + this.noCacheIE);
  this.scriptObj.setAttribute("id", this.scriptId);
  this.scriptObj.setAttribute("type", "text/css");
}


// removeScriptTag method
JSONscriptRequest.prototype.removeScriptTag = function () {
  // Destroy the script tag
  this.headLoc.removeChild(this.scriptObj); 
}

// addScriptTag method
JSONscriptRequest.prototype.addScriptTag = function () {
  // Create the script tag
  this.headLoc.appendChild(this.scriptObj);
}



//this is essentially the main method
function main()
{
	//add homegrown JS scripts
	obj=new JSONscriptRequest(elux_base_path + "/res/js/elux_sears.js");
	obj.buildScriptTag(); // Build the script tag
	obj.addScriptTag(); // Execute (add) the script tag
	
	//more JS scripts for swf
	obj=new JSONscriptRequest(elux_base_path + "/res/scripts/swfobject.js");
	obj.buildScriptTag(); // Build the script tag
	obj.addScriptTag(); // Execute (add) the script tag

	obj=new JSONscriptRequest(elux_base_path + "/res/javascripts/swfobject.js");
	obj.buildScriptTag(); // Build the script tag
	obj.addScriptTag(); // Execute (add) the script tag
	
	//add some css
	var obj=new JSONscriptRequest(elux_base_path + "/res/css/elux_sears.css");
	obj.buildLinkTag(); // Build the link tag
	obj.addScriptTag(); // Execute (add) the script tag
	
	var url = elux_base_path + "/toJSON.aspx?eluxp=" + gup("eluxp") + "&from=" + escape(removeFromQS("eluxp"));
	var obj=new JSONscriptRequest(url);
	obj.buildScriptTag(); // Build the script tag
	obj.addScriptTag(); // Execute (add) the script tag
}
 
function injectHTML(data)
{
  if(data==null)
    alert('Sorry, the data requested could not be retrieved.');
  else
  {
    document.getElementById(elux_div).innerHTML=data.html;
    if(!document.getElementById("updated"))
	{	
		updateURIs();
	}
	runScriptElux();
  }
}

function gup(name)
{//get URL parameter
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function removeFromQS(name)
{ //remove URL parameter
	var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regex = new RegExp("[\\&]*" + name + "=([^&#]*)");
	return window.location.href.replace(regex,"");
}

function updateURIs()
{
	if(window.location.href.indexOf("?")>-1)
	{
		var markup = '&';
	}
	else
	{
		var markup = '?';
	}

	var arr = document.getElementById(elux_div).getElementsByTagName("a");
	for(var i=0; i<arr.length; i++)
	{
		arr[i].href = removeFromQS("eluxp") + markup + "eluxp=" + getURIEnd(arr[i].href);
	}
}

function getURIEnd(uri)
{
	var pos = uri.lastIndexOf("/");
	return uri.substring(pos+1);
}
 
 //manually traverses DOM tree and executes javascript thats injected by innerHTML
function runScriptElux()
{//start recursive function for elux div
	runScripts(document.getElementById(elux_div));
}

function runScripts(e) {
	if (e.nodeType != 1) return; //if it's not an element node, return
 
	if (e.tagName.toLowerCase() == 'script') {
		eval(e.text); //run the script
	}
	else {
		var n = e.firstChild;
		while ( n ) {
			if ( n.nodeType == 1 ) runScripts( n ); //if it's an element node, recurse
			n = n.nextSibling;
		}
	}
}