//////////////////////////////////////////////////
// init / create div
//////////////////////////////////////////////////

var tv = document.getElementById('homelightingJs').getAttribute('tv');	

if(document.domain=='localhost') {
	var path = 'http://localhost/test/h/docs/';
} else if(document.domain=='hl-renewal.ctrlv.eu') {
	var path = 'http://hl-renewal.ctrlv.eu/docs/';	
} else if(document.domain=='www.qa.lighting.philips.com') {
	var path = 'http://www.qa.lighting.philips.com/microsite/homelighting/docs/';
} else if(document.domain=='www.lighting.philips.com') {
	var path = 'http://www.lighting.philips.com/microsite/homelighting/docs/';
} else if(document.domain=='www.homelighting.philips.com') {
	var path = 'http://www.homelighting.philips.com/microsite/homelighting/docs/';
}

var running = false;
var cssinterval = '';
var loadinterval = '';
var scriptruns = 0;
var homelightingArray = [];
homelightingArray['tv'] = tv;
homelightingArray['loadFilesArray'] = [];
homelightingArray['unloadFilesArray'] = [];
homelightingArray['finderArray'] = "";

loadomatic(path+'js/prototype_cms.js','prototypejs','create_homelightingWrapper()');

function create_homelightingWrapper()
{
//hier komt i niet als crashed
	var div = document.getElementById('homelightingWrapper');
	div.style.width = '960px';
	div.style.height = '820px';
	div.style.position = 'relative';
	//div.style.backgroundColor = '#FFFFFF';
	div.style.overflow = 'hidden';
	new Ajax.Updater('homelightingWrapper', path+'extra/magic/tv/'+tv, { method: 'POST', parameters: 'tv='+homelightingArray['tv'], evalScripts: true }); 
}
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// load-o-matic(tm)
//////////////////////////////////////////////////
function loadomatic(file,name,exec)
{
	//add to array
	homelightingArray['loadFilesArray'].push([file,name,exec]);

	//run parser
	if(running == false)
	{
		running = true;
		loadFiles();
	}
}
//////////////////////////////////////////////////
// loadFiles
//////////////////////////////////////////////////
function loadFiles()
{
	//grab entry and parse it
	var tmp = homelightingArray['loadFilesArray'].shift();
	var file_path = tmp[0];
	var file_id = tmp[1];
	var file_exec = tmp[2];

	var file_type = file_path.substr(file_path.lastIndexOf(".")+1);
	//////////////////////////////////////////////////
	// eof
	//////////////////////////////////////////////////
	if(file_path == "eof")
	{
		eval(tmp[1]);
	}
	//////////////////////////////////////////////////
	// CSS
	//////////////////////////////////////////////////
	if(file_type == "css")
	{
		//checked of al bestaat/geladen is
		if(document.getElementById(file_id) == null)
		{
			var css  = document.createElement('link');   
			css.rel = 'stylesheet';
			if(file_exec == "print")
			{
				css.media = 'print';
  				//omdat het print is, wordt de height van de load check niet gezet. 
			}
			else
			{
				css.media = 'screen';
			}
			css.type = 'text/css'; 			
			css.href  = file_path;
			css.id = file_id;
			document.getElementsByTagName('head').item(0).appendChild(css);
			//
			if(file_exec == "print")
			{
				document.getElementById('ccsloadcontainer').innerHTML = '<div id="'+file_id+'_load" style="display: block; width: 50px; height: 10px; font-size: 0px;"></div>';
			}
			else
			{
					document.getElementById('ccsloadcontainer').innerHTML = '<div id="'+file_id+'_load" style="display: block; width: 50px; height: 1px; font-size: 0px;"></div>';
			}
			cssinterval = setInterval('checkCssLoaded(\''+file_id+'_load\')', 20);
			return;
			//
		}
	}
	
	//////////////////////////////////////////////////
	// JS
	//////////////////////////////////////////////////
	if(file_type == "js")
	{
		
		//checked of al bestaat/geladen is
		if(document.getElementById(file_id) == null)
		{		
			//add, if fails, it's FF
			try {
				//IE
				var js = document.createElement('<script type="text/javascript" src="'+file_path+'" id="'+file_id+'" ></script>');
			} catch (e) {
				//FF
				var js  = document.createElement('script');   
				js.type = 'text/javascript';   
				js.src  = file_path; 
				js.defer = false;
				js.id = file_id;
			}

			document.getElementsByTagName('head').item(0).appendChild(js);
			//////////////////////////////////////////////////
			// IE
			//////////////////////////////////////////////////
			js.onreadystatechange = function ()
			{
				
				if (js.readyState == 'loaded' || js.readyState == 'complete' || js.readyState == 4)
				{
					//debug
					//trace('<font color="#00CC00">loaded = ' + file_path + '</font>');
					
					if(file_exec != undefined)
					{
						eval(file_exec);
					}
				}
			}
			//////////////////////////////////////////////////
			// FIREFOX
			//////////////////////////////////////////////////
			js.onload = function ()
			{
				//debug
				//trace('<font color="#00CC00">loaded = ' + file_path + '</font>');
				
				if(file_exec != undefined)
				{
					eval(file_exec);
				}
			}
			
		}
		else
		{
			//debug
			//trace('<font color="#ff0000">exists already = ' + file_path + '</font>');

			if(file_exec != undefined || file_exec != '')
			{
				eval(file_exec);
			}
		}
		
	}
	//leeg, dus running=false, anders nog x
	if(homelightingArray['loadFilesArray'].length == 0)
	{
		running = false;
	}
	else
	{
		loadFiles();
	}
}
//////////////////////////////////////////////////
// checkCssLoaded
//////////////////////////////////////////////////
function checkCssLoaded(id)
{
	//kan nog een check in voor aantal loops...
	if(document.getElementById(id))
	{
		if(document.getElementById(id).offsetHeight >= 10)
		{
			//debug
			//trace('<font color=#00CC00>loaded = '+id+' = '+document.getElementById(id).offsetHeight+'</font>');
						
			var del = document.getElementById(id);
			if(del)
			{
				del.parentNode.removeChild(del);
			}
			clearInterval(cssinterval);
			
			//volgende loadFile
			if(homelightingArray['loadFilesArray'].length == 0)
			{
				running = false;
			}
			else
			{
				loadFiles();
			}
		}
		else
		{
			//debug
			//trace('<font color=#0000ff>loading = '+id+' = '+document.getElementById(id).offsetHeight+'</font>');	
		}
	}
}
//////////////////////////////////////////////////
// unloadFiles
//////////////////////////////////////////////////
function unloadFiles()
{
	if(homelightingArray['unloadFilesArray'].length == 0)
	{
		return;
	}
	
	tmp = homelightingArray['unloadFilesArray'].shift();
	var id = tmp[0];

	var head = document.getElementsByTagName('head').item(0);
	var js = document.getElementById(tmp[0]);
	if(js)
	{
		js.parentNode.removeChild(js);
	}
	unloadFiles();
}
//////////////////////////////////////////////////
// loadMeta
//////////////////////////////////////////////////
function loadMeta()
{
	//alert("loadMeta");
}
//////////////////////////////////////////////////
// trace
//////////////////////////////////////////////////
function trace(trace)
{
	document.getElementById('tracewindow').innerHTML += trace+"<br>";
}
//////////////////////////////////////////////////
// omnitureTrack
//////////////////////////////////////////////////
function omnitureTrack(pagename)
{
	pagename = pagename.toString();
	var pos = homelightingArray['tv'].indexOf("_");
	var country = homelightingArray['tv'].substr(0,pos);
	if (country == "gl")
	{
		country = "global";
	}
	var language = homelightingArray['tv'].substr(pos+1);

	if(document.domain == 'localhost' || document.domain == 'hl-renewal.ctrlv.eu')
	{
		//alert('"division":"LI",\n"section":"homelighting",\n"pagename":"'+pagename+'",\n"country":"'+country+'",\n"language":"'+language+'",');
	}
	else
	{
		_page.metrics.trackAjax({
						"division":"LI",
						"section":"homelighting",
						"pagename":pagename,
						"country":country,
						"language":language
					});
	}
}	
//////////////////////////////////////////////////
// resizeWrapper
//////////////////////////////////////////////////
function resizeWrapper(parameter, fixedsize)
{
	
	if(document.getElementById(parameter))
	{
		document.getElementById(parameter).style.display = 'block';
	}
	else
	{
		//div doesn't exist, so quit
		return;	
	}

	
	if(fixedsize != undefined)
	{
		var totalHeight = fixedsize;
	}
	else
	{
		var div = $(parameter);	
		var totalHeight = div.offsetHeight;	
		totalHeight += 184; //84 menu, 80 footer, 20 witruimte
	}
	
	
	



	if(document.getElementById("finder") && document.getElementById("lamps_database"))
	{
		if(parameter == "finder" || parameter == "lamps_database" || parameter == "lamps_index")
		{
			//alert("lamps_finder="+document.getElementById("finder").offsetHeight+" lamps_database="+document.getElementById("lamps_database").offsetHeight);
			if(document.getElementById("finder").offsetHeight > document.getElementById("lamps_database").offsetHeight)
			{
				totalHeight = document.getElementById("finder").offsetHeight + 184;
			}
			else
			{
				totalHeight = document.getElementById("lamps_database").offsetHeight + 184;
			}
			//database loading resizen
			document.getElementById('loading_lamps_database').style.height = (document.getElementById("lamps_database").offsetHeight) + 'px';		
		}
	}

	if(totalHeight < 820)
	{
		totalHeight = 820;
	}
	var div = $('homelightingWrapper');
	div.style.height = totalHeight + 'px';
	
	//popup resizen
	if(document.getElementById('popup'))
	{
		document.getElementById('popup').style.height = totalHeight + 'px';
	}
	//loading_content resizen
	document.getElementById('loading_content')
	{
		document.getElementById('loading_content').style.height = (totalHeight - 84) + 'px';
	}
}	
//////////////////////////////////////////////////
// eof
//////////////////////////////////////////////////
homelightingArray['eof'] = [];
function doneLoading(id)
{
	homelightingArray['eof'][id] = true;
	
	
	if(id == "lamps_finder" || id == "lamps_home")
	{
		if(homelightingArray['eof']["lamps_finder"] == true && homelightingArray['eof']["lamps_home"] == true)
		{
			document.getElementById("loading_content").style.display = "none";
			document.getElementById("loading_site").style.display = "none";
			homelightingArray['eof']["lamps_index"] = true;
		}
	}

	if(id == "contact_index")
	{
		if(homelightingArray['eof']["contact_index"] == true && homelightingArray['eof']['contact_index2'] == true)
		{
			document.getElementById("loading_content").style.display = "none";
		}
	}

	if(id == "calc_index")
	{
		if(homelightingArray['eof']["calc_index"] == true && homelightingArray['eof']['calc_index2'] == true)
		{
			document.getElementById("loading_content").style.display = "none";
		}
	}

	if(id == "calc_detail")
	{
		if(homelightingArray['eof']["calc_detail"] == true && homelightingArray['eof']['calc_detail2'] == true)
		{
			document.getElementById("loading_content").style.display = "none";
		}
	}

	if(id == "sim_index")
	{
		if(homelightingArray['eof']["sim_index"] == true && homelightingArray['eof']['sim_index2'] == true)
		{
			document.getElementById("loading_content").style.display = "none";
		}
	}
	
	if(id == "faq_index")
	{
		if(homelightingArray['eof']["faq_index"] == true && homelightingArray['eof']['faq_index2'] == true)
		{
			document.getElementById("loading_content").style.display = "none";
		}
	}	
	
	if(id == "faq_glsban")
	{
		if(homelightingArray['eof']["faq_glsban"] == true && homelightingArray['eof']['faq_glsban2'] == true)
		{
			document.getElementById("loading_content").style.display = "none";
		}
	}	
	
	if(id == "lamps_detail")
	{
		if(homelightingArray['eof']["lamps_detail"] == true && homelightingArray['eof']['lamps_detail2'] == true)
		{
			document.getElementById("loading_content").style.display = "none";
		}
	}

	if(id == "lamps_savings")
	{
		if(homelightingArray['eof']["lamps_savings"] == true && homelightingArray['eof']['lamps_savings2'] == true)
		{
			document.getElementById("loading_content").style.display = "none";
		}
	}
	
	if(id == "lamps_compare")
	{	
		if(homelightingArray['eof']["lamps_compare"] == true && homelightingArray['eof']['lamps_compare2'] == true)
		{
			document.getElementById("loading_content").style.display = "none";
		}
	}
	
	if(id == "lamps_page")
	{	
		if(homelightingArray['eof']["lamps_page"] == true && homelightingArray['eof']['lamps_page2'] == true)
		{
			document.getElementById("loading_content").style.display = "none";
		}
	}
	
	if(id == "lamps_database")
	{		
		if(homelightingArray['eof']["lamps_database"] == true && homelightingArray['eof']['lamps_database2'] == true)
		{
			document.getElementById("loading_content").style.display = "none";
			document.getElementById("loading_lamps_database").style.display = "none";
			resizeWrapper("lamps_database");
		}
	}
	
	if(id == "led_page")
	{	
		if(homelightingArray['eof']["led_page"] == true && homelightingArray['eof']['led_page2'] == true)
		{
			document.getElementById("loading_content").style.display = "none";
		}
	}

	if(id == "ark_index")
	{	
		if(homelightingArray['eof']["ark_index"] == true && homelightingArray['eof']['ark_index2'] == true)
		{
			document.getElementById("loading_content").style.display = "none";
		}
	}	

}
