var content = "";

function init(){
	
	content = document.getElementById('module_div');
	pageSize = getPageSize();
	cwin_doc = window;
	cwin_content = cwin_doc.document.getElementById('module_div');
	currentId = null;
	pageSize = getPageSize();
	modules = new Array();
	getPageBody();

}

function fixHeight(){

	var newHeight=0;
	
	for(var i=0;i<modules.length;i++){
		if(el = document.getElementById("e"+i)){
			var top = new Number(el.style.top.substr(0,(el.style.top.length-2)));
			if(newHeight<(top+el.offsetHeight)){
				newHeight = top+el.offsetHeight;
			}
		}
	}
	
	content.style.height = (newHeight+10)+"px";

}

function setHandlerHeight(){}

function addEmbed(type,name,html,zindex,top,left,width,height,mod_link){
	
	new_id = modules.length;
	
	//add it to the array
	var propertyItem = new Array("html",html);
	var propertyItem2 = new Array("type",type);
	var propertyItem3 = new Array("link",mod_link);
	var moduleItem = new Array("embed",name,Number(zindex),top,left,width,height,new Array(propertyItem,propertyItem2,propertyItem3));
	modules.push(moduleItem);
	
	//add it to the page
	cwin_doc.addModule("embed",new_id,zindex,top,left,width,height,html,type,mod_link);
	
}

function addTextBox(name,html,zindex,top,left,width,height){
	
	new_id = modules.length;
	
	//add it to the array
	var propertyItem = new Array("html",html);
	var moduleItem = new Array("embed",name,Number(zindex),top,left,width,height,new Array(propertyItem));
	modules.push(moduleItem);
	
	//add it to the page
	cwin_doc.addModule("textbox",new_id,zindex,top,left,width,height,html,"textbox",'');
	
}

function insertModule(js_controlfile){
	var e = document.createElement("script");
	e.src = js_controlfile;
	e.type="text/javascript";
	document.getElementsByTagName("head")[0].appendChild(e);
}

function getModuleId(name){

	for(var i=0; i<modules.length;i++){
	
		if(modules[i][1]==name){
			return i;
		}
	
	}
	
}

function getNextZIndex(){

	var highestZIndex=0;
	for(var i=0; i<modules.length; i++){
		if(modules[i][2]!=null){
			if(modules[i][2]>highestZIndex){
				highestZIndex=modules[i][2];
			}
		}
	}
	
	return highestZIndex+1;
	
}

function getModules(){

	new Ajax.Request(root_server+'/profile/profile_ajax.php', {method:'post', postBody:'action=getmodules&user_id='+user_id, onComplete:modulesGot});

}

var modulesGot = function(t){
	
	var xmlDoc = t.responseXML;
	var xmlModules = xmlDoc.getElementsByTagName('module');
	for (var i=0;i<xmlModules.length;i++){
		module_type = xmlModules[i].getAttribute("type");
		
		if(module_type=="embed" || module_type=="picture" || module_type=="video" || module_type=="other"){
			module_zindex = xmlModules[i].getAttribute("zindex");
			module_top = xmlModules[i].getAttribute("top");
			module_left = xmlModules[i].getAttribute("left");
			module_width = xmlModules[i].getAttribute("width");
			module_height = xmlModules[i].getAttribute("height");

			module_html = "";
			module_link = "";
			module_name = "";
			
			for (var j=0;j<xmlModules[i].childNodes.length;j++){
				if(xmlModules[i].childNodes[j].nodeType != 1) continue;
				if(xmlModules[i].childNodes[j].nodeName=="mod_html"){ module_html = xmlModules[i].childNodes[j].firstChild.nodeValue; }
				if(xmlModules[i].childNodes[j].nodeName=="link"){ module_link = xmlModules[i].childNodes[j].firstChild.nodeValue; }
				if(xmlModules[i].childNodes[j].nodeName=="name"){ module_name = xmlModules[i].childNodes[j].firstChild.nodeValue; }
			}

			addEmbed(module_type,escape(module_name),escape(module_html),module_zindex,module_top,module_left,module_width,module_height,module_link);
		}else if(module_type=="textbox"){
			module_name = xmlModules[i].getAttribute("name");
			module_zindex = xmlModules[i].getAttribute("zindex");
			module_top = xmlModules[i].getAttribute("top");
			module_left = xmlModules[i].getAttribute("left");
			module_width = xmlModules[i].getAttribute("width");
			module_height = xmlModules[i].getAttribute("height");
			module_html = xmlModules[i].firstChild.nodeValue;
			addTextBox(escape(module_name),escape(module_html),module_zindex,module_top,module_left,module_width,module_height);
		}else if(module_type=="module"){
			module_js = xmlModules[i].getAttribute("js");
			insertModule(module_js);
		}
		
	}
	
}

function getPageBody(){
		
	new Ajax.Request(root_server+'/profile/profile_ajax.php', {method:'post', postBody:'action=getpage&user_id='+user_id, onComplete:pageGot});
	
}

function pageGot(t){
	
	//add body to modules array
	body_id = modules.length;
	var moduleItem = new Array("body","body",null,null,null,null,null,new Array());
	modules.push(moduleItem);
	var xmlDoc = t.responseXML;
	var xmlModules = xmlDoc.getElementsByTagName('property'); //1
	for (var i=0;i<xmlModules.length;i++){
		var property_name = xmlModules[i].getAttribute("name");
		var property_value = xmlModules[i].getAttribute("value");
		createPropertyValue(body_id,property_name,property_value);
	}	

	styleBody();
	getModules();
	
}

function styleBody(){
	
	var backgroundImage = getPropertyValue(body_id,"backgroundImage");
	var backgroundRepeat = getPropertyValue(body_id,"backgroundRepeat");
	var backgroundPosition = getPropertyValue(body_id,"backgroundPosition");
	var backgroundColor = getPropertyValue(body_id,"backgroundColor");
	var innerBackgroundImage = getPropertyValue(body_id,"innerBackgroundImage");
	var innerBackgroundRepeat = getPropertyValue(body_id,"innerBackgroundRepeat");
	var innerBackgroundPosition = getPropertyValue(body_id,"innerBackgroundPosition");
	var innerBackgroundColor = getPropertyValue(body_id,"innerBackgroundColor");
	var headerBackgroundImage = getPropertyValue(body_id,"headerBackgroundImage");
	var headerBackgroundRepeat = getPropertyValue(body_id,"headerBackgroundRepeat");
	var headerBackgroundPosition = getPropertyValue(body_id,"headerBackgroundPosition");
	var headerBackgroundColor = getPropertyValue(body_id,"headerBackgroundColor");
	var headerLogo = getPropertyValue(body_id,"headerLogo");
	var mainmenuFontFamily = getPropertyValue(body_id,"mainmenuFontFamily");
	var mainmenuFontSize = getPropertyValue(body_id,"mainmenuFontSize");
	var mainmenuColor = getPropertyValue(body_id,"mainmenuColor");
	var mainmenuLinkColor = getPropertyValue(body_id,"mainmenuLinkColor");
	var mainmenuHoverColor = getPropertyValue(body_id,"mainmenuHoverColor");
	var statusFontFamily = getPropertyValue(body_id,"statusFontFamily");
	var statusFontSize = getPropertyValue(body_id,"statusFontSize");
	var statusColor = getPropertyValue(body_id,"statusColor");
	var statusLinkColor = getPropertyValue(body_id,"statusLinkColor");
	var statusHoverColor = getPropertyValue(body_id,"statusHoverColor");
	var footerBackgroundImage = getPropertyValue(body_id,"footerBackgroundImage");
	var footerBackgroundRepeat = getPropertyValue(body_id,"footerBackgroundRepeat");
	var footerBackgroundPosition = getPropertyValue(body_id,"footerBackgroundPosition");
	var footerBackgroundColor = getPropertyValue(body_id,"footerBackgroundColor");
	var footerFontFamily = getPropertyValue(body_id,"footerFontFamily");
	var footerFontSize = getPropertyValue(body_id,"footerFontSize");
	var footerColor = getPropertyValue(body_id,"footerColor");
	var footerLinkColor = getPropertyValue(body_id,"footerLinkColor");
	var footerHoverColor = getPropertyValue(body_id,"footerHoverColor");
	var backgroundScroll = getPropertyValue(body_id,"backgroundScroll");
	var mainmenuBackgroundImage = getPropertyValue(body_id,"mainmenuBackgroundImage");
	var mainmenuBackgroundPosition = getPropertyValue(body_id,"mainmenuBackgroundPosition");
	var mainmenuBackgroundRepeat = getPropertyValue(body_id,"mainmenuBackgroundRepeat");
	var mainmenuBackgroundColor = getPropertyValue(body_id,"mainmenuBackgroundColor");
	var mainmenuFull = getPropertyValue(body_id,"mainmenuFull");
	var headerFull = getPropertyValue(body_id,"headerFull");
	
	//style body
	var el = cwin_doc.document.getElementById('body');
	el.style["backgroundImage"] = backgroundImage;
	el.style["backgroundRepeat"] = backgroundRepeat;
	el.style["backgroundPosition"] = backgroundPosition;
	el.style["backgroundColor"] = backgroundColor;
	el.style["backgroundAttachment"] = (backgroundScroll=="true" || backgroundScroll==true) ?  "scroll" : "fixed";
	
	//style inner
	var el = cwin_doc.document.getElementById('content');
	el.style["backgroundImage"] = innerBackgroundImage;
	el.style["backgroundRepeat"] = innerBackgroundRepeat;
	el.style["backgroundPosition"] = innerBackgroundPosition;
	el.style["backgroundColor"] = innerBackgroundColor;
	
	//header
	var el = cwin_doc.document.getElementById('header');
	el.style["backgroundImage"] = headerBackgroundImage;
	el.style["backgroundRepeat"] = headerBackgroundRepeat;
	el.style["backgroundPosition"] = headerBackgroundPosition;
	el.style["backgroundColor"] = headerBackgroundColor;
	
	if(headerFull==true || headerFull=="true"){
		el.style["width"] = "100%";
	}else{
		el.style["width"] = "980px";
	}
	
	//header logo
	var el = cwin_doc.document.getElementById('logo');
	el.style["backgroundImage"] = "url("+headerLogo+")";
	
	//main menu
	var el = cwin_doc.document.getElementById('mainmenu');
	el.style["backgroundImage"] = mainmenuBackgroundImage;
	el.style["backgroundRepeat"] = mainmenuBackgroundRepeat;
	el.style["backgroundPosition"] = mainmenuBackgroundPosition;
	el.style["backgroundColor"] = mainmenuBackgroundColor;	
	el.style["fontFamily"] = mainmenuFontFamily;
	el.style["fontSize"] = mainmenuFontSize;
	var el_lis = el.getElementsByTagName('li');
	for(var j=0;j<el_lis.length;j++){
		el_lis[j].style['borderLeftColor'] = mainmenuColor;
	}
	var el_links = el.getElementsByTagName('a');
	for(var j=0;j<el_links.length;j++){
		el_links[j].style['color'] = mainmenuLinkColor;
		el_links[j].onmouseover = function(){ this.style.color=mainmenuHoverColor; }
		el_links[j].onmouseout = function(){ this.style.color=mainmenuLinkColor; }
	}
	
	if(mainmenuFull==true || mainmenuFull=="true"){
		el.style["width"] = "100%";
	}else{
		el.style["width"] = "980px";
	}
	
	//status menu
	var el = cwin_doc.document.getElementById('headerlinks');
	el.style["fontFamily"] = statusFontFamily;
	el.style["fontSize"] = statusFontSize;
	el.style["color"] = statusColor;
	var el_links = el.getElementsByTagName('a');
	for(var j=0;j<el_links.length;j++){
		el_links[j].style['color'] = statusLinkColor;
		el_links[j].onmouseover = function(){ this.style.color=statusHoverColor; }
		el_links[j].onmouseout = function(){ this.style.color=statusLinkColor; }
	}
	
	//footer
	var el = cwin_doc.document.getElementById('footer');
	el.style["backgroundImage"] = footerBackgroundImage;
	el.style["backgroundRepeat"] = footerBackgroundRepeat;
	el.style["backgroundPosition"] = footerBackgroundPosition;
	el.style["backgroundColor"] = footerBackgroundColor;
	el.style["fontFamily"] = footerFontFamily;
	el.style["fontSize"] = footerFontSize;
	el.style["color"] = footerColor;
	var el_lis = el.getElementsByTagName('li');
	for(var j=0;j<el_lis.length;j++){
		el_lis[j].style['borderLeftColor'] = footerColor;
	}
	var el_links = el.getElementsByTagName('a');
	for(var j=0;j<el_links.length;j++){
		el_links[j].style['color'] = footerLinkColor;
		el_links[j].onmouseover = function(){ this.style.color=footerHoverColor; }
		el_links[j].onmouseout = function(){ this.style.color=footerLinkColor; }
	}
	
}

function createPropertyValue(id,property_name,property_value){

	//update the array
	var prop_item = new Array(property_name,property_value);
	modules[id][7].push(prop_item);
	
}

function updatePropertyValue(id,property_name,property_value){

	//find the element name
	for(i=0;i<modules[id][7].length;i++){
		if(modules[id][7][i][0]==property_name){
			modules[id][7][i][1] = property_value;
		}
	}	
	
}

function getPropertyValue(id,property_name){

	//go through the module id array until we find the property name
	for(i=0;i<modules[id][7].length;i++){
		if(modules[id][7][i][0]==property_name){
			return modules[id][7][i][1];
		}
	}

}

function addModule(type,id,zindex,top,left,width,height,html,type,mod_link){
	
	//add it to the page
	var new_div = document.createElement("div");
	new_div.id = "e"+id;
	new_div.style.top = top+"px";
	new_div.style.left = left+"px";
	new_div.style.zIndex = zindex;
	new_div.className = "module";

	if(width!=0){ new_div.style.width = width+"px"; }
	if(height!=0){ new_div.style.height = height+"px";}
	
	//create the inner
	var new_i = document.createElement("div");
	new_i.id = "i"+id;
	new_i.style.width = "100%"; 
	new_i.style.height = "100%"; 
	new_i.className = "inner";
	if(html!=""){
		if(type=="picture" || type=="embed" || type=="other"){
			
				if(type=="picture"){
					var el_img = new Image();
					new_i.appendChild(el_img);
					el_img.style.height="100%";
					el_img.style.width="100%";
					if(mod_link!="" && mod_link!=null){ 
						el_img.onclick = function(){ window.location = mod_link };
						el_img.onmouseover = function(){  document.body.style.cursor = 'pointer'; }
						el_img.onmouseout = function(){  document.body.style.cursor = 'default'; }
					}
					el_img.src = unescape(html);
				
				}else if(type=="embed"){
					var so = new SWFObject(unescape(html), "embed_"+id, width, height, "8", "#000000");
					so.addParam("wmode", "transparent");
					so.write(new_i);
				}else if(type=="other"){
					new_i.innerHTML = unescape(html);
				}
			
		}else{
			new_i.innerHTML = unescape(html);	
		}
	}
	
	new_div.appendChild(new_i);
	content.appendChild(new_div);
	
	fixHeight();
	
}


function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(windowWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getElementsByClassName(classname)
{
   var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = document.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
		if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}