var TorreyObjects = new Array();
var TorreyClasses = new Array();
var TorreyIdOffset= 0;
var TorreyObStarted = new Array();
var TorreyObHandler;

function TorreyProductLoader(settings)
{
	TorreyClasses[TorreyClasses.length] = settings;
	TorreyObStarted[TorreyClasses.length] = false;
}

function TorreyProductStartObjects()
{
	var completed = false;
	o = 0;
	
    for(o=0;o < TorreyClasses.length;o++) {
    	if(document.getElementById(TorreyClasses[o]['DivId']) && !TorreyObStarted[o]) {
    		TorreyObjects[o] = new TorreyProductLoaderMain(TorreyClasses[o]);
    		TorreyObStarted[o] = true;
    		completed = true;
    	}
    	if(!TorreyObStarted[o]) {
    		completed = false;
    	}
    }
    if(completed) {
    	window.clearInterval(TorreyObHandler);
    }
}

function TorreyProductLoaderMain (settings) {
	this.div = document.getElementById(settings['DivId']);
	if(settings['url']) {
		this.baseUrl = settings['url'];
	} else {
		this.baseUrl = 'http://shop.parents.com/Shop/Product/LoaderTarget';
	}
	this.iFrame = false;
	this.settings = settings;
	
	this.getUrl = function()
	{
		var args = "";
		var count = 0;
		for (var key in this.settings)
		{
			count++;
				if (args != "") {
					args += "&";
				} else { 
					args = "?";
				}
				args = args + escape(key) + "=" + escape(this.settings[key]);
		}
		return this.baseUrl+args;
	}
	
	this.createiFrame = function()
	{
		if(this.div) {
			this.div.innerHTML = this.div.innerHTML + "<iframe id=\""+this.settings['DivId']+"_iframe"+"\" name=\""+this.settings['DivId']+"_iframe"+"\" scrolling=\"no\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\"  src=\""+this.getUrl()+"\"></iframe>";
			this.iFrame = document.getElementById(this.settings['DivId']+"_iframe");
			if(this.settings['Width']) this.iFrame.style.width = this.settings['Width'];
			if(this.settings['Height']) this.iFrame.style.height = this.settings['Height'];
			
		} else {
			alert("Id "+this.settings['DivId']+" does not exist.");
		}
	}

	this.createiFrame();
}

TorreyObHandler = window.setInterval(TorreyProductStartObjects,10);

