var body_isbody;
var body_href;
var body_width;
var body_height;
var body_title;
var body_description;

var NewPageOutline;
var page_contents;


function GetPageComponents()
{
 //read the content.
	var elements = $("#contentHome table tbody tr td table tbody tr td table tbody tr:nth-child(2)");
	var element_contents = elements.text();
	//create a new array
		var string_array = new Array();
		//split the object passed values by ;
		string_array = element_contents.split(" ; ", 6);
		//store the values in global variables for later use.
		body_isbody  = string_array[0]; 
		body_href	= string_array[1];
		body_width	= string_array[2];
		body_height = string_array[3];
		body_title	= string_array[4];
		body_description  = string_array[5];
 if(IsBody(body_isbody)!= true)
 {
	//exit the function.
	return;
 }
 if(AttachElementsToPage(CreateElements()) == false)
 {
	//exit the function.
	alert("Elements could not be attached to the page!");
	return;
 }
 return;
} 


//Test to see if the element in question is a body element.
function IsBody(the_element)
{
	var bodyTrigger = the_element;
	bodyTrigger = $.trim(bodyTrigger);
	if (bodyTrigger!="body")
	{
		return false;
	}
	return true;
}

//Create the page elements.
function CreateElements()
{
 var NewDiv = document.createElement('div');
 
 var NewPage = document.createElement("TABLE");

 NewDiv.appendChild(NewPage);
 
	NewPage.id="table_content";
	NewPage.border=0;
	
	var NewPage_body = document.createElement("TBODY");

	NewPage.appendChild(NewPage_body);
	
	var row_title = document.createElement("TR");
	var row_iframe = document.createElement("TR");
	var row_description = document.createElement("TR");

	var cell_title = document.createElement("TD");
	var cell_iframe = document.createElement("TD");
	var cell_description = document.createElement("TD");

	//Title
	NewPage_body.appendChild(row_title);
	var div_title = document.createElement('div');
		div_title.id="div_title";
	var p_title = document.createElement('p');
		p_title.id="title_p";
		var h1_title = document.createElement('h1');
			h1_title.id = 'title';
			h1_title.innerHTML = body_title;
			
			p_title.appendChild(h1_title);
			div_title.appendChild(p_title);
			cell_title.appendChild(div_title);
			row_title.appendChild(cell_title);
			
	
	//Iframe
	NewPage_body.appendChild(row_iframe);
	var div_iframe = document.createElement('div');
		div_iframe.id = "div_iframe";
		
		
	var iframe = document.createElement('iframe');
		iframe.width = body_width;
		iframe.height = body_height;
		iframe.src = body_href;
		iframe.frameborder ="no";
		iframe.scrolling = 'no';
		iframe.id = "iframe_video";
			div_iframe.appendChild(iframe);
			cell_iframe.appendChild(div_iframe);			
			row_iframe.appendChild(cell_iframe);
			
		//Description
	NewPage_body.appendChild(row_description);
	var div_description = document.createElement('div');
		div_description.id='div_description';
		
	var p_description = document.createElement('p');
		p_description.id = 'description';
		p_description.innerHTML = body_description;
			div_description.appendChild(p_description);
			cell_description.appendChild(div_description);
			row_description.appendChild(cell_description);

			
	NewPageOutline = NewDiv;
		if(NewPageOutline.childNodes.length >= 1)
		{
			 return true;
		}
	return false;
}

//Attach the new elements to the page and remove the previous objects.
function AttachElementsToPage()
{
 page_contents = document.getElementById('contentHome');
 if(page_contents.hasChildNodes())
 {
	while(page_contents.childNodes.length >= 1 ) 
	{
		page_contents.removeChild(page_contents.firstChild);
	}
	if(NewPageOutline.hasChildNodes()) 
	{
		page_contents.appendChild(NewPageOutline.firstChild); 
	}
	CreateStyle();
	return true;
 }
 return false; 
}
//Create the page-dynamic css
function CreateStyle()
{
 var string = 
	"table{margin: 0 auto;width: 99%;}"+
	"#table_content{margin: 0 auto; border: 0; z-index: 0; width:"+body_width.toString()+"}"+
	"#div_description{margin: 0 auto;}"+
	"#iframe_video{margin: 0 auto; border: 0; padding: 0};"+
	"#title{font-size: 12pt;}"+
	"#div_title{margin: 0 auto};"+
	"#decription{font-size: 10pt;font-color: #000000;}"+
	"table tr td div{margin: 0 auto;width: 100%;}"+
	"iframe{ border: 0; margin: 0; padding: 0;}";
  AttachStyle(string);
}

//attach the page dynamic css.
function AttachStyle(stringObject)
{	
 var header = document.getElementsByTagName('head')[0];
 var styler = document.createElement('style');
 styler.type= 'text/css'; 
 var str = stringObject; 
 if(styler.styleSheet) {styler.styleSheet.cssText = str;}// IE method
 else {styler.appendChild(document.createTextNode(str));}// others
 //test adding styles.
 if(!(header.appendChild(styler))){alert("could not add dynamic styles!");}
}
window.onload = GetPageComponents;
