// BackDrop(TM) NameSpace
// Copyright Fall Web Concepts 2008

if(typeof BackDrop == 'undefined' || !BackDrop) {
	var BackDrop = {};
}
BackDrop.control = {};
BackDrop.dialog = {};
BackDrop.util = {};
BackDrop.widget = {};
BackDrop.control.entries = [];
BackDrop.dialog.entries = [];
BackDrop.tokenizer = {};

BackDrop.tokenizer.run = function(HTML, config) {
	HTML = HTML.replace(new RegExp("\\n", "g"), "");
	if(config != null) {
		for(var x=0; x < config.length; x++) {
			var io = HTML.indexOf(config[x].f);
			while(io != -1) {
				HTML = HTML.replace(config[x].f, config[x].r);
				io = HTML.indexOf(config[x].f);
			}
		}
	}
	
	var newid = BackDrop.util.generateId();
	HTML = HTML.replace(/%generateId%/g, newid.toString() );
	return HTML;
}

BackDrop.util.getFlashObject = function(name) {
	if(navigator.appName.indexOf("Microsoft") != -1)
	 {
	  return window[name];
	 }
	 else
	 {
	  return document[name];
	 }
};

BackDrop.util.Event = new Object();
BackDrop.util.Event.events = [];
BackDrop.util.Event.addListener = function(nEl, nEvent, nCallback) {
	BackDrop.util.Event.events[BackDrop.util.Event.events.length] = {element:nEl, event:nEvent, callback:nCallback};
	var ev = eval(nEl + "." + nEvent);
	superobject = this;
	ev = function() {alert(superobject)};
	alert(ev);
};

BackDrop.util.Event.fire = function(el, event) {
	for(var i = 0; i < BackDrop.util.Event.events.length; i++) {
		if(BackDrop.util.Event.events[i].element == el && BackDrop.util.Event.events[i].event == event) {
			eval(BackDrop.util.Event.events[i].callback);
		}
	}
};

BackDrop.util.pullHTML = function(HTML, id, type) {
	var el = document.createElement("DIV");
	el.innerHTML = HTML;
	var e = el.getElementsByTagName(type);
	for(var x = 0; x < e.length; x++) {
		if(e[x].id == id) {
			return e[x].innerHTML;
		}
	}
	return null;
}

BackDrop.util.pullCSS = function(HTML){
	var el = document.createElement("DIV");
	el.innerHTML = HTML;
	var style = el.getElementsByTagName("STYLE");
	var css = "";
	for (var i = 0; i < style.length; i++) {
		css += style[i].innerHTML;
	}
	css = css.replace(new RegExp("\\n", "g"), "");
	style = null;
	el = null;
	return css;
}

BackDrop.util.applyVisualCSS = function(HTML) {
	var el, elTD;
	el = document.createElement("DIV");
	el.innerHTML = HTML;
	elTD = el.getElementsByTagName("TD");
	for(var i = 0; i < elTD.length; i++) {
		elTD[i].className = "BackDrop-Editor-Visual-TD " + elTD[i].className;
	}
	return el.innerHTML;
}

BackDrop.util.fixImagePaths = function(html) {
	var divCan, imgT, tdT, divT, _html;
	divCan = document.createElement("DIV");
	divCan.innerHTML = html;
	
	//process IMG tags
	imgT = divCan.getElementsByTagName("IMG");
	for(var i = 0; i < imgT.length; i++) {
		if(imgT[i].src.indexOf("Templates", 0) == -1 ) {
			var iStart = imgT[i].src.indexOf("images/", 0);
			var begin = imgT[i].src.substr(0, iStart);
			var end = imgT[i].src.substr(iStart, imgT[i].src.length);
			imgT[i].src = begin + "../Templates/" + end;
		}
	}
	imgT = null;
	iStart = null;
	begin = null;
	end = null;
	
	// Process TD tags
	tdT = divCan.getElementsByTagName("TD");
	for(var s = 0; s < tdT.length; s++) {
		if(tdT[s].getAttribute('background') != null && tdT[s].getAttribute("background").indexOf('Templates') == -1 ) {
			var attr = tdT[s].getAttribute('background');
			iStart = attr.indexOf("images/");
			begin = attr.substr(0, iStart);
			end = attr.substr(iStart, attr.length);
			tdT[s].setAttribute('background', begin + "../Templates/" + end);
		}
	}
	tdT = null;
	iStart = null;
	begin = null;
	end = null;
	
	// Process DIV tags
	divT = divCan.getElementsByTagName("DIV");
	for(var x = 0; x < divT.length; x++) {
		if(divT[x].style.backgroundImage.indexOf("Templates") == -1) {
			var bimg = divT[x].style.backgroundImage;
			iStart = bimg.indexOf("images/");
			begin = bimg.substr(0, iStart);
			end = bimg.substr(iStart, bimg.length);
			divT[x].style.backgroundImage = begin + "../Templates/" + end;
		} else if(divT[x].style.backgroundImage.indexOf("Templates") != -1) {
			var bimg = divT[x].style.backgroundImage;
			iStart = bimg.indexOf("Templates");
			begin = bimg.substr(0, iStart);
			end = bimg.substr(iStart, bimg.length);
			divT[x].style.backgroundImage = begin + "../" + end;
		}
	}
	

	_html = divCan.innerHTML;
	divCan = null;
	imgT = null;
	tdT = null;
	return _html;
}

BackDrop.util.in_array = function(needle, haystack) {
	for(var i = 0; i < haystack.length; i++) {
		if(haystack[i] == needle) {
			return true;
		}
	}
	return false;
}

BackDrop.util.in_objectArray = function (needle, haystack, property) {
	for(var i = 0; i < haystack.length; i++) {
		if(eval("haystack[" + i + "]." + property) == needle) {
			return true;
		}
	}
	return false;
}

BackDrop.util.restoreDoc = function(html, iHTML) {
	var bdTag;
	if (html.indexOf("<body") != -1) {
		bdTag = html.indexOf("<body");
		endBdTag = html.indexOf(">", bdTag);
		return html.substr(0, endBdTag + 1) + iHTML + "</body></html>";
	} else {
		return iHTML;
	}
}

BackDrop.util.idList = [];
BackDrop.util.generateId = function(prefix) {
	var count;
	
	if(prefix == null) prefix = "";
	
	if(BackDrop.util.idList.length == 0) {
		BackDrop.util.idList[BackDrop.util.idList.length] = prefix + 0;
		return prefix + 0;
	} else {
		count = 0;
		while(BackDrop.util.in_array(prefix + count, BackDrop.util.idList) ) {
			count++;
		}
		BackDrop.util.idList[BackDrop.util.idList.length] = prefix + count;
		return prefix + count;
	}
}

BackDrop.util.changeId = function(id, prefix) {
	var node = document.getElementById(id);
	var id = BackDrop.util.generateId(prefix);
	node.id = id;
	return id;
}

BackDrop.util.urlEncodeArray = function(array) {
	
}

BackDrop.util.getClientSize = function() {
	var width = document.compatMode=='CSS1Compat' && !window.opera ? document.documentElement.clientWidth : document.body.clientWidth;
	var height = document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
	return [width,height];
}

BackDrop.util.formValidate = function(formID, action) {
	var post = [];
	var send = "{\"form\": [";
	var f = document.getElementById(formID);
	var inputs = f.getElementsByTagName("INPUT");
	for (var i = 0; i < inputs.length; i++) {
		attrs = inputs[i].attributes;
		send += "{";
		for (var s = 0; s < attrs.length; s++) {
			send += "\"" + attrs[s].nodeName + "\":\"" + attrs[s].nodeValue + "\",";
		}
		send += "\"value\":\"" + inputs[i].value + "\",";
		send = send.substr(0, send.length - 1);
		send += "},";
	}
	send = send.substr(0, send.length - 1);
	send += "]}";

	var callback = {
		success: function(o) {
			try {
				var result = eval("(" + o.responseText + ")");
				var go = 0;
				for(var x = 0; x < result.form.length; x++) {
					
					if(result.form[x].result == false) {
						go = go + 1;
						var el = document.getElementById(result.form[x]['id']);
						el.value = result.form[x].value;
						el.className = "formError";
						
						if(el.previousSibling && el.previousSibling.tagName == "SPAN" && el.previousSibling.className == "formErrorText") {
							el.previousSibling.innerHTML = result.form[x].reason + "<br>";
						} else {
							var newEl = document.createElement("SPAN");
							newEl.className = "formErrorText";
							newEl.innerHTML = result.form[x].reason + "<br>";			
							el.parentNode.insertBefore(newEl, el);
						}
					} else {
						var el = document.getElementById(result.form[x]['id']);
						if(el == null) continue;
						el.value = result.form[x].value;
						el.className = "formGood";
						if(el.previousSibling && el.previousSibling.tagName == "SPAN") {
							el.parentNode.removeChild(el.previousSibling);
						}				
					}
				}
				if(go == 0) {
					if(YAHOO.widget.Panel) {
						var panel = new YAHOO.widget.SimpleDialog("sPanel", {modal:true,visible:true,fixedcenter:true});
						panel.setBody("<img src='Libraries/Assets/gears-animation.gif' align='left' />Your account has been created. <br> Please wait while we redirect you to the login page.");
						panel.render(document.body);
						setTimeout(BackDrop.util.formSubmit, 5000, f);
					} else {
						alert("Your account has been created. \n Press 'OK' to go to the login page.");
						f.submit();
					}
				}

			} catch (e) {
				alert(e.message);
				document.body.innerHTML += o.responseText;
			}
		},
		failure: function() {
		}
	}
	var transaction = YAHOO.util.Connect.asyncRequest('POST', action, callback, "post=" + send)
}
/*
 * Used to submit a form through setTimeout
 * Pass the form object into the function.
 */
BackDrop.util.formSubmit = function(form) {
	form.submit();
}

BackDrop.util.findSibling = function(startel, tagname, classname) {
	while(startel.nextSibling != null) {
		startel = startel.nextSibling;
		if(startel.tagName == tagname) {
			if(classname != "" && startel.className == classname) {
				return startel;
			} else if(classname == "") {
				return startel;
			}
		}
	}
	return null;
};

BackDrop.register = function(type, skeletonId, objectType) {
	switch(type) {
		default:
			alert("Registration type invalid");
			break;
		case "control":
			if(!BackDrop.util.in_objectArray(skeletonId, BackDrop.control.entries, "skeletonId") ) {
				BackDrop.control.entries[BackDrop.control.entries.length] = {skeletonId: skeletonId, objectType: objectType};
			}
			break;
		
		case "dialog":
			if(!BackDrop.util.in_objectArray(skeletonId, BackDrop.dialog.entries, "skeletonId")) {
				BackDrop.dialog.entries[BackDrop.dialog.entries.length] = {skeletonId: skeletonId, objectType: objectType};
			}
			break;
	}
}

BackDrop.control.request = function(skeletonId) {
	
}

BackDrop.dialog.request = function(config) {
	if(config == null) {
		alert("could not execute dialog request");
		return null;
	}
	var dlgRef, skHTML, newId;
	// Find Reference in entries
	for(var x = 0; x < BackDrop.dialog.entries.length; x++) {
		if(BackDrop.dialog.entries[x].skeletonId == config.skeletonId) {
			dlgRef = BackDrop.dialog.entries[x];
		}
	}
	// if dlgRef is still null we didn't find anything
	if(dlgRef == null) {
		alert("dlgRef null");
		return null;
	}
	// Get innerHTML to work with
	skHTML = document.getElementById(dlgRef.skeletonId).innerHTML;
	copyCSS = document.getElementById(dlgRef.skeletonId).className;
	
	// Run through tokenizer to change all ids and instance names
	tConfig = [{f: "%instancename%", r: config.instancename}];
	skHTML = BackDrop.tokenizer.run(skHTML, tConfig);
	// Add to document.body
	newId = BackDrop.util.generateId(config.instancename + "_");
	node = document.createElement("DIV");
	node.setAttribute("id", newId);
	node.setAttribute("style", "visibility: hidden;");
	node.className = copyCSS;
	node.innerHTML = skHTML;
	document.body.appendChild(node);
	
	try {
		eval(config.instancename + " = new " + dlgRef.objectType + "();");
		var a = eval("window." + config.instancename + ";");
		// Prototype a node into the object
		a.node = node;
		a.instancename = config.instancename;
		a.parent = config.parentref;
		a = null;
	} catch(e) {
		alert("BackDrop.dialog.request() eval'd code: " + e.message);
		return null;
	}
	// return node reference and instance reference
	return eval("window." + config.instancename);
}

adRotators = [];
BackDrop.widget.Adplayer = function () {
	this.index = 0;
	this.ID = "";
	this.ads = [];
	this.add = function(obj) {
		adRotators[this.ID].ads = obj;
		// Load all Images into objects
		for(var x = 0; x < adRotators[this.ID].ads.length; x++) {
			var cAd = adRotators[this.ID].ads[x];
			cAd.image = new Image();
			cAd.image.src = cAd.img;
			cAd.image.border = 0;
		}
	};

	this.show = function() {
		var total = adRotators[this.ID].ads.length;
		
		if(adRotators[this.ID].index > total -1) {
			adRotators[this.ID].index = 0;
		}
		var cIndex = adRotators[this.ID].index;
	
		var target = document.getElementById(this.ID);
		var anchor = document.createElement("A");
		anchor.href = adRotators[this.ID].ads[cIndex].link;
		target.innerHTML = "";
		
		anchor.appendChild(adRotators[this.ID].ads[cIndex].image);
		target.appendChild(anchor);
		adRotators[this.ID].index++;
		
		setTimeout("adRotators['" + this.ID + "'].show();", 8000);
	};
};

BackDrop.widget.Rollover = {};
BackDrop.widget.Rollover.images = [];
BackDrop.widget.Rollover.mouseChange = function() {
	var index = this.getAttribute("rollover:index");
	if(BackDrop.widget.Rollover.images[index][2] == 0) {
		this.src = BackDrop.widget.Rollover.images[index][1].src;
		BackDrop.widget.Rollover.images[index][2] = 1;
	} else {
		this.src = BackDrop.widget.Rollover.images[index][0].src;
		BackDrop.widget.Rollover.images[index][2] = 0;
	}
};
BackDrop.widget.Rollover.add = function(id) {
	var el, i,origImage, fileparts,filename,nfilename,url,index,protocol;
	el = document.getElementById(id);
	i = new Image();
	origImage = new Image();
	origImage.src = el.src;
	fileparts = el.src.split(/\\|\//g);
	filename = fileparts[fileparts.length-1];
	url = "";
    if(document.location.toString().indexOf( 'https://' ) != -1 )
        protocol = "https";
    else
        protocol = "http";
	for(var x = 0; x < fileparts.length -1; x++) {
		if(fileparts[x] != "http:" && fileparts[x] != "https:" && fileparts[x] != "") {
			url += fileparts[x] + "/"; // Windows doesn't care about the slash so lets make it linux compat'
		}
	}
	filename = filename.split(".");
	nfilename = "";
	for(var s = 0; s < filename.length -1 ; s++) {
		nfilename += filename[s] + ".";
	}
	nfilename = nfilename.substr(0, nfilename.length -1);
	nfilename += "_rollover." + filename[filename.length -1];
	i.src = protocol + "://" + url + nfilename;
	index = BackDrop.widget.Rollover.images.length;
	// add both image objects to the array
	BackDrop.widget.Rollover.images[index] = [origImage, i, 0];

	el.setAttribute("rollover:index", index);
	el.onmouseover = BackDrop.widget.Rollover.mouseChange;
	el.onmouseout = BackDrop.widget.Rollover.mouseChange;
};

BackDrop.widget.Rollover.active = function(id) {
	var el,fileparts,filename,nfilename,url;
	el = document.getElementById(id);

	fileparts = el.src.split(/\\|\//g);
	filename = fileparts[fileparts.length-1];
	url = "";
        if(document.location.toString().indexOf( 'https://' ) != -1 )
        protocol = "https";
    else
        protocol = "http";
	for(var x = 0; x < fileparts.length -1; x++) {
		if(fileparts[x] != "http:" && fileparts[x] != "https:" && fileparts[x] != "") {
			url += fileparts[x] + "/"; // Windows doesn't care about the slash so lets make it linux compat'
		}
	}
	filename = filename.split(".");
	nfilename = "";
	for(var s = 0; s < filename.length -1 ; s++) {
		nfilename += filename[s] + ".";
	}
	nfilename = nfilename.substr(0, nfilename.length -1);
	nfilename += "_rollover." + filename[filename.length -1];
	el.src = protocol + "://" + url + nfilename;
};

BackDrop.widget.Rollover.init = function() {
	var allImages = document.getElementsByTagName("IMG");
	for(var x = 0; x < allImages.length; x++) {
		if(allImages[x].getAttribute("rollover") && allImages[x].getAttribute("rollover") == "true") {
            if(allImages[x].getAttribute("active") && allImages[x].getAttribute("active") == "true") {
                BackDrop.widget.Rollover.active(allImages[x].id);
            } else {
                BackDrop.widget.Rollover.add(allImages[x].id);
            }
		}
	}
	allImages = null;
};

BackDrop.widget.Collapse = {};

BackDrop.widget.Collapse.add = function(id) {
	var el = document.getElementById(id);
	var hdDiv = BackDrop.util.findSibling(el.firstChild, "DIV", "hd");
	var bdDiv = BackDrop.util.findSibling(el.firstChild, "DIV", "bd");
	bdDiv.oHeight = bdDiv.style.height;
	bdDiv.style.overflow = "hidden";
	if(bdDiv.getAttribute('hide') && bdDiv.getAttribute('hide') == "true") {
		bdDiv.style.height = "1px";
	}

	hdDiv.onmouseover = function(){this.style.cursor='pointer';};
	hdDiv.onclick = function() {
		var bdDiv, anim, curHeight, destHeight;
		bdDiv = BackDrop.util.findSibling(this, 'DIV', 'bd');
		curHeight = bdDiv.style.height.substring(0, bdDiv.style.height.length - 2);
		destHeight = bdDiv.oHeight.substring(0, bdDiv.oHeight.length -2);
		if(curHeight == destHeight) {
			anim = new YAHOO.util.Anim(bdDiv, {height:{to:0}},0.5);
		} else if(curHeight <= 1) {
			anim = new YAHOO.util.Anim(bdDiv, {height:{to:destHeight}},0.5);
		} else {
			return;
		}
		
		anim.animate();
	};
};
BackDrop.widget.Collapse.scan = function() {
	var divs = document.getElementsByTagName("DIV");
	for(var x = 0; x < divs.length; x++) {
		if(divs[x].getAttribute('collapse') && divs[x].getAttribute("collapse") == "true") {
			BackDrop.widget.Collapse.add(divs[x].id);
		}
	}
	divs = null;
};

BackDrop.util.allImagesLoaded = function() {
    // All images are saved in an array called document.images.
    var images = document.images;

    // Loop through all the images
    for (var i = 0;i<images.length;i++)
    {
            // If the image isnt loaded we return 0
            if(images[i].complete == false) {
                    return 0;
            }
    }

    // If we make it this far, all the images should be loaded!
    return 1;
};