var first = "true";
var subfolder = '';

//-------Call for Editor--------//
function editor(id,tag){
	if (tag=='DIV'){tag = 'widgets';}
	if (tag=='IMG'){tag = 'images';}
	if (tag=='A'){tag = 'links';}
	file = which_file();
	var page = "serverside/scripts/requestXML.php?editor=true";
	if(navigator.appName == "Microsoft Internet Explorer"){var page = "serverside/scripts/requestXML.php?editor=true&ie=true";}
	showContactTimer(); // quickly begin the load bar
	var stuff = "&tag="+tag+"&id="+id+"&read="+file;
	loadXMLPosDoc(page,stuff);
	if(navigator.appName == "Microsoft Internet Explorer"){ editorarea = grabPosXML("editor");} else{
	editorarea = '<h2>'+id+'</h2>'+grabPosXML("dimensions")+grabPosXML("background")+grabPosXML("border")+grabPosXML("font")+grabPosXML("image")+grabPosXML("link")+grabPosXML("myspace")+grabPosXML("close");}
	byId('editarea').innerHTML = createCode(editorarea);
	dojo.widget.byId('editarea').show();
}

//----------Make Edit--------//
function edit(id,tag,value,attribute){
	//alert(id);
	tochange =  attribute.replace(id,'');
	//alert(attribute);
	file = which_file();
	to_edit = '../../clientside/xml/'+userid+'.xml';
	var page = "serverside/scripts/requestXML.php?edit=true";
	showContactTimer(); // quickly begin the load bar
	if(tochange == 'backgroundImage'){value = 'url('+value+')';}
	var stuff = "&parent="+tag+"&child="+id+"&attribute="+tochange+"&value="+fixCode(value)+"&open="+file+"&write="+to_edit;
	loadXMLPosDoc(page,stuff);
	if(attribute == 'musicBoxurl' || attribute == 'rssBoxurl' || attribute == 'calendarurl'){tochange = 'specialBoxurl';}
	if (tochange == 'url'){ if(tag == 'images'){byId(id).parentNode.setAttribute('href', '#'+value);}else{byId(id).setAttribute('href', '#'+value);}}
	else if (tochange == 'source'){byId(id).setAttribute('src', value);}
	else if (attribute == 'calendarurl' || attribute == 'calendarlimit'){var page = "serverside/scripts/requestXML.php?calendar=true&file="+to_edit;var stuff = "";loadXMLPosDoc(page,stuff);byId('calendar_html').innerHTML = createCode(grabPosXML('html'));}
	else if (attribute == 'rssBoxurl' || attribute == 'rssBoxlimit'){var page = "serverside/scripts/requestXML.php?rss=true&file="+to_edit;var stuff = "";loadXMLPosDoc(page,stuff);byId('rssBox_html').innerHTML = createCode(grabPosXML('html'));}
	else if (attribute == 'musicBoxurl'){byId('musicplayer').setAttribute('src', 'serverside/flash/wimpy_button.swf?theFile='+value+'&autoplay=yes&loopMe=yes');}
	else if (attribute == 'musicBoxwidth'){byId(id).style.width = value;byId('musicplayer').setAttribute('width', value);}
	else if (attribute == 'musicBoxheight'){byId(id).style.height = value;byId('musicplayer').setAttribute('height', value);}
	else if (tochange == 'embed'){byId('pictureBox_html').innerHTML = value;}
	else if (tochange == 'friendid'){byId(id).getElementsByTagName('input')[3].value = value;}
	else if (tochange == 'borderColor-hover'){byId(id).setAttribute('onmouseover', "old=this.style.borderColor;this.style.borderColor = '"+value+"';");}
	else if (tochange == 'alt'){byId(id).parentNode.setAttribute('title', value);byId(id).setAttribute('alt', value);} 
	else if (tochange == 'text'){ byId(id).setAttribute('title', value);byId(id).innerHTML = value; } 
	else if (tochange == 'bodyBG'){document.body.style.backgroundColor=value;}
	else {e_style(id,tochange,value,tag);}
	first = 'false';
}

//------make edit area------//
function create_element(tag,id,inner){
	var editArea = document.createElement(tag);
	editArea.id = id;
	editArea.setAttribute('dojoType', 'contentPane');
	editArea.setAttribute('toggle','wipe');
	//editArea.style.display = 'none';
	editArea.innerHTML = inner;
	byId('header').appendChild(editArea);
}

//--------------style previewer-----//
function e_style(id,str,value,tag){
	//alert('changing '+str+' of '+id+' to '+value);
	if (str == 'borderLeft'){byId(id).style.borderLeft = value+' '+byId(id+"borderColor").value;}
	if (str == 'borderRight'){byId(id).style.borderRight = value+' '+byId(id+"borderColor").value;}
	if (str == 'borderColor' && tag == 'links'){e_style(id,'borderLeft',byId(id+"borderLeft").value);e_style(id,'borderRight',byId(id+"borderRight").value);}
	if (str == 'borderSize'){byId(id).style.border = value+' '+byId(id+"borderColor").value+' '+byId(id+"borderStyle").value;}
	if (str == 'borderStyle'){e_style(id,'borderSize',byId(id+"borderSize").value);}
	if (str == 'borderColor'){e_style(id,'borderSize',byId(id+"borderSize").value);}
	if (str == 'borderColor-hover'){byId(id).style.borderLeft = value;}
	if (str == 'paddingLeft'){byId(id).style.paddingLeft = value;}
	if (str == 'paddingRight'){byId(id).style.paddingRight = value;}
	if (str == 'width'){byId(id).style.width = value;}
	if (str == 'display'){byId(id).style.display = value;}
	if (str == 'height'){byId(id).style.height = value;}
	if (str == 'borderSize'){byId(id).style.borderSize = value;}
	if (str == 'font'){byId(id).style.fontFamily = value;}
	if (str == 'fontColor'){byId(id).style.color = value;}
	if (str == 'fontSize'){byId(id).style.fontSize = value;}
	if (str == 'background'){byId(id).style.backgroundColor = value;}
	if (str == 'backgroundColor'){byId(id).style.backgroundColor = value;}
	if (str == 'backgroundImage'){byId(id).style.backgroundImage = value;}
	if (str == 'backgroundPosition'){byId(id).style.backgroundPosition = value;}
	if (str == 'backgroundRepeat'){byId(id).style.backgroundRepeat = value;}
return;
}

//---------blank editor-------------//
function clear_editor(){
		byId('editarea').innerHTML = '<h2>Nothing Is Being Editted</h2>';
}

//--------------Save Position----------//
function savePosition(id){
	edit(id,'widgets',byId(id).style.left,'left');
	edit(id,'widgets',byId(id).style.top,'top');
}

//-------Required Vars-------------//


//--------XML Requests-----------------//
//-------------------------------------//
function createCode(str){
	str = str.replace(/_#sCode#_/g,"<");
	str = str.replace(/_#eCode#_/g,">");
	str = str.replace(/_#and#_/g,"&");
	str = str.replace(/_#qmark#_/g,"?");
	str = str.replace(/_qmark_/g,"?");
	return(str);
}

function fixCode(str){
	str = str.replace(/&lt;/g,"_#sCode#_");
	str = str.replace(/&gt;/g,"_#sCode#_");
	str = str.replace(/</g,"_#sCode#_");
	str = str.replace(/>/g,"_#eCode#_");
	str = str.replace(/&/g,"**am**");
	str = str.replace(/"/g,"'");
	str = str.replace(/&quot;/g,"'");
	return(str);
}

function showContactTimer () {
	var loader = byId('loadBar');
	loader.style.display = 'block';
	sentTimer = setTimeout("hideContactTimer()",1000);
}

function hideContactTimer () {
	var loader = byId('loadBar');
	var success = byId('success');
	loader.style.display = "none";
	success.style.display = "block";
	success.innerHTML = '<strong style="color:#ffffff;">'+grabPosXML("confirmation")+'</strong>';
	hideTimer = setTimeout("hideSuccessTimer()",3000);
}

function hideSuccessTimer() {
	var success = byId('success');
	success.style.display = "none";
}

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) { 
	elm.addEventListener(evType, fn, useCapture); 
	return true; 
	}
	else if (elm.attachEvent) { 
	var r = elm.attachEvent('on' + evType, fn); 
	EventCache.add(elm, evType, fn);
	return r; 
	}
	else {
	elm['on' + evType] = fn;
	}
}
function getEventSrc(e) {
	if (!e) e = window.event;

	if (e.originalTarget)
	return e.originalTarget;
	else if (e.srcElement)
	return e.srcElement;
}
function addLoadEvent(func) {
var oldonload = window.onload;
	if (typeof window.onload != 'function') {
	window.onload = func;
	} else {
	window.onload = 
		function() {
		oldonload();
		func();
		}
	}
}
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
	
		add : function(node, sEventName, fHandler, bCapture){
			listEvents.push(arguments);
		},
	
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				
				/* From this point on we need the event names to be prefixed with 'on" */
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				
				item[0][item[1]] = null;
			};
		}
	};
}();


addEvent(window,'unload',EventCache.flush, false);

var pos; // variable for posting information
function loadXMLPosDoc(url,posData) {
    // branch for native XMLHttpRequest object
	if (subfolder == 'true'){
			url = '../'+url;
	}
    if (window.XMLHttpRequest) {
        pos = new XMLHttpRequest();
        pos.onreadystatechange = processPosChange;
        pos.open("POST", url, false);
		pos.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        pos.send(posData);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        pos = new ActiveXObject("Microsoft.XMLHTTP");
        if (pos) {
            pos.onreadystatechange = processPosChange;
            pos.open("POST", url, false);
			pos.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            pos.send(posData);
        }
    }
}

function grabPosXML (tagName) {
return pos.responseXML.documentElement.getElementsByTagName(tagName)[0].childNodes[0].nodeValue;
}

function processPosChange() {
    // page loaded "complete"
    if (pos.readyState == 4) {
        // page is "OK"
        if (pos.status == 200) {
			if ( grabPosXML("posStatus") == 'NOTOK' ) { 
				alert('There were problems Sending Email. Please check back in a couple minutes');
			}
		}
	}
}