var req;
var checkbox;
var page = "";
var className = "";
var baseDir = 'http://www.dabsmyla.com/';

/////////////////////////////////////////////////////////////////////////////////////////
//XMLHTTPREQUEST FUNCTIONS_______________________________________________________________
function loadXMLDoc(url) {
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open('GET', url, true);
		req.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		req = new ActiveXObject('Microsoft.XMLHTTP');
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open('GET', url, true);
			req.send();
		}
	}
}

//callBackFunction is set (as a string) as you send XML request and is called on response
var callBackFunction = "";

//processes request
function processReqChange() {
	// only if req shows 'loaded'
	if (req.readyState == 4) {
		// only if 'OK'
		if (req.status == 200) {
			eval(callBackFunction);
			req.stuff = "ee";
		}else{
			alert(req.statusText);
		}
	}
}

//sets the content and vis of ajax_status
function updateStatus(content, vis){
	document.getElementById('ajax_status').innerHTML = content;
	document.getElementById('screen_dimmer').style.display = vis;
}

/////////////////////////////////////////////////////////////////////////////////////////
//TO KEEP FUNCTIONS_______________________________________________________________

//CANCEL/SUBMIT PROMPT******************************************
function cancelPrompt(){
	document.getElementById(theTargetId).innerHTML = '';
	document.getElementById(theTargetId).style.display = 'none';
}
function submitPrompt(callFunction){
	if(event.keyCode == 13){
		eval(callFunction);
	}
}

//XMLHTTPREQUEST CONTENT, INSERT INTO AN ELEMENT***************
var theTargetId = '';
function getContent(theUrl, target){
	var requestUrl = baseDir + theUrl;
	theTargetId = target;
	callBackFunction = "resolveGetContent()";
	updateStatus('Loading...', 'block');
	loadXMLDoc(requestUrl);
//	alert(requestUrl);
}
function resolveGetContent() {
	updateStatus('', 'none');
	//***************** check for failed at start and print error string
	if (req.responseText.substr(0,6) == 'failed') {
		alert(req.responseText.substr(8));
		checkbox.checked = !checkbox.checked;
	} else {
		//first, seperate the javascript
		var splitContent = req.responseText.split("END THE JAVASCRIPT BIT");
		var javascriptContent = splitContent[0];
		var content = splitContent[1];
		//then insert the content
		document.getElementById(theTargetId).innerHTML = '';
		document.getElementById(theTargetId).innerHTML = content;
		document.getElementById(theTargetId).style.display = 'block';
		//then run the javascript
		eval (javascriptContent);
	}
}

//CLOSE ANYTHING******************************
function ungetContent(target){
	document.getElementById(target).innerHTML = '';
	document.getElementById(target).style.display = 'none';
}

//KILLS EVENT BUBBLING
function killBubbles(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

//GET COMPUTED STYLE OF AN ELEMENT
function getStyle(oElm, strCssRule)
{
	oElm = document.getElementById(oElm);
	var strValue = '';
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, '').getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

//RETURN THE WINDOW DIMENSIONS
function windowDim()
{
	if (document.body && document.body.offsetWidth) {
		winW = document.body.offsetWidth;
		winH = document.body.offsetHeight;
	}
	if (document.compatMode=='CSS1Compat' &&
		document.documentElement &&
		document.documentElement.offsetWidth ) {
	 winW = document.documentElement.offsetWidth;
	 winH = document.documentElement.offsetHeight;
	}
	if (window.innerWidth && window.innerHeight) {
	 winW = window.innerWidth;
	 winH = window.innerHeight;
	}
	return [winW, winH];
}

//RESIZE DIV********************
function resizeDiv(imgWidth, imgHeight, target, theImg){
	winD = windowDim();
	winW = winD[0];
	winH = winD[1];
	maxImgWidth = winW - (30);
	maxImgHeight = winH - (120);
	
	if(imgWidth > maxImgWidth)
	{
		var ratio = maxImgWidth / imgWidth;
		newImgWidth = maxImgWidth;
		newImgHeight = Math.round(imgHeight * ratio);
	}else if(imgHeight > maxImgHeight){
		var ratio = maxImgHeight / imgHeight;
		newImgHeight = maxImgHeight;
		newImgWidth = Math.round(imgWidth * ratio);
	}else{
		newImgWidth = imgWidth;
		newImgHeight = imgHeight;
	}
	//resize the img
	imgTarget = document.getElementById(theImg);
	imgTarget.style.width = newImgWidth + 'px';
	imgTarget.style.height = newImgHeight + 'px';

	//resize the div
	divTarget = document.getElementById(target);
	divTarget.style.width = (newImgWidth) + 'px';
	divTarget.style.height = (newImgHeight + 50) + 'px';
}

