//COPYRIGHT Daniel Richardson 2007 - danrichardson.net
//You may use this code free of charge however this statement should remain!

function customAlert(options) {
	this.xPos = 0;
	this.yPos = 0;
	this.title = options.title || "";//"You forgot your title";
	this.text = options.text;
	this.type = options.type || 0;
	this.endText = options.endText || "";
	this.icon = options.icon || null;
	this.offset = options.offset || [0,0]; //top, left
	this.dropShadow = options.dropShadow || [10,10,10,10];
	this.data = options.data || "Loading Data...";
	this.alertID = ("customAlert" + Math.random()).replace(".","");
	this.buttons = new Array();
	this.cssClass = options.cssClass || null;
	this.iFrame = options.iFrame || false;
	this.build();
}
customAlert.prototype.build = function buildAlert() {
	alertMargin = String(this.dropShadow).replace(/,/g,"px ") + "px";
	var DOMel = DomBuilder.apply();
	if (this.iFrame) {
	var html = DOMel.DIV({ id : this.alertID, className : "customAlertBackDrop" }, //shadow div
				  DOMel.DIV({ id : "customAlertWindow", className : this.cssClass, style : "margin:" + alertMargin }, //main div
					DOMel.DIV({ id : "customAlertTop" }, //top div
				  		DOMel.A({ href: "javascript:void(0)", onclick: "destroyAlert('"+ this.alertID +"')" }, "x") //close link
				  	),//end top div
					DOMel.DIV({ id : "customAlertContainer", style : "background:url(" + this.icon + ") top right no-repeat" },
						DOMel.DIV({ id : "customAlertTitle"}, this.title), //alert Title
						//DOMel.DIV({ className : "content", id : "customAlertContent"}, this.data), //alert Content
						DOMel.IFRAME({ src:"'"+this.iFrame+"'" }), //iFrame
						DOMel.DIV({ }, this.endText), //end div text
						DOMel.DIV({ className : "buttons", id : "customAlertButtons"}, "Button Area"), //buttons
						DOMel.DIV({ style : "height:1px"}) //ie fix div
					)//end main container
				  ) //end alert window
				); //end backdrop
	} else {
		var html = DOMel.DIV({ id : this.alertID, className : "customAlertBackDrop" }, //shadow div
				  DOMel.DIV({ id : "customAlertWindow", className : this.cssClass, style : "margin:" + alertMargin }, //main div
					DOMel.DIV({ id : "customAlertTop" }, //top div
				  		DOMel.A({ href: "javascript:void(0)", onclick: "destroyAlert('"+ this.alertID +"')" }, "x") //close link
				  	),//end top div
					DOMel.DIV({ id : "customAlertContainer", style : "background:url(" + this.icon + ") top right no-repeat" },
						DOMel.DIV({ id : "customAlertTitle"}, this.title), //alert Title
						DOMel.DIV({ className : "content", id : "customAlertContent"}, this.data), //alert Content
						DOMel.DIV({ }, this.endText), //end div text
						DOMel.DIV({ className : "buttons", id : "customAlertButtons"}, "Button Area"), //buttons
						DOMel.DIV({ style : "height:1px"}) //ie fix div
					)//end main container
				  ) //end alert window
				); //end backdrop
	}
	var alertEl = document.getElementsByTagName("body")[0].appendChild(html); 			//add new DOM
	alertEl.style.left = ((document.body.clientWidth/2)-alertEl.offsetWidth/2) + "px"; 	//center on screen
	//add any offset
	alertEl.style.top = alertEl.offsetTop + this.offset[0] + "px"; //offset top
	alertEl.style.left = alertEl.offsetLeft + this.offset[1] + "px"; //offset left
	//make the alert box draggable
	/*jQuery(alertEl).Draggable({ 
		zIndex: 	1000,
		opacity: 	0.7,
		containment: 'document',
		handle: jQuery(alertEl).children().children()[0] //make the alert window the handle handle 
	});*/
	alertEl = null;
	this.redraw();
}
customAlert.prototype.button = function addButton(options) {
	this.buttons.push({	text : options.text || "Your Button",
						url : options.url || "javascript:void(0)",
						image: { src : options.src || null, hover : options.hover || null },
						title : options.title || null,
						altTag : options.altTag || "Image Not Found!",
						cssClass : options.css || "cssButton",
						onclick : options.onclick || function(){
							alert("Should there be an event fired here?\nDid you forget onclick:function() ?")
						}
	});
}
customAlert.prototype.dataSource = function data(options) {
	jQuery.ajax({
		   type: options.type || "GET",
		   url: options.url || "",
		   dataType: options.dataType || "html",
		   data: options.data || "",
		   success: options.success || function(data) { if (options.container) { alertCallBack(data,options.container); } 
														else { alert(data); }
													  }
	})
}
customAlert.prototype.redraw = function draw() {
	jQuery("#" + this.alertID).find("#customAlertContent").html(this.data);
	var buttonEl = jQuery("#" + this.alertID).find("#customAlertButtons");
	buttonEl.html(getButtons(this.buttons));
}

function alertCallBack(data,obj) {
	obj.data = data; //set the object data to the return data from the ajax call
	obj.redraw(); //redraw the alert box
}

function destroyAlert(objID) {
	if (jQuery('#'+objID)) {//check the object still exists in the DOM
		jQuery('#'+objID).fadeOut("slow",function(){
				document.getElementsByTagName("body")[0].removeChild(document.getElementById(objID));
		});
	}
	var e = document.getElementsByTagName("select");
	for (var i = 0; i<e.length; i++) {
		e[i].style.visibility="visible";
	}
}

function getButtons(buttons) {
	if (buttons) {
		var ulEl = document.createElement("UL"); //create new unordered list DOM element
		ulEl.id = "customAlertButtons"; //set the id - for stying
	
		for(var i = 0; i < buttons.length; i++) { //loop through the buttons array of objects and make new DOM elements and return the dom elements
			liEl = ulEl.appendChild(document.createElement("LI")); //create new list item into the UL DOMobject
			liAnchorEl = liEl.appendChild(document.createElement("A")); //create an anchor inside the new list item DOM object
			liAnchorEl.href = buttons[i].url; //set the anchor URL
			liAnchorEl.onclick = buttons[i].onclick; //set the onclick
			liAnchorEl.title = buttons[i].title; //set the title - used later on for custom tooltips
			if (buttons[i].image.src) { //if an image path was passed then use images instead of css buttons
				liAnchorImageEl = liAnchorEl.appendChild(document.createElement("IMG")); //add new image DOM element into anchor DOM object
				liAnchorImageEl.src = buttons[i].image.src; //set the image src
				liAnchorImageEl.border = "0"; //set the border to 0
				liAnchorImageEl.alt = buttons[i].altTag; //set the button alt tag for accessbility / missing images
				liAnchorImageEl.title = buttons[i].altTag;//set the button title tag for accessbility / missing images (ie)
				if (buttons[i].image.hover) { //if a mouseover on the image is required
					liAnchorImageEl.setAttribute("onmouseover","this.src='"+buttons[i].image.hover+"'"); //set the mouseover event
					liAnchorImageEl.setAttribute("onmouseout","this.src='"+buttons[i].image.src+"'"); //set the mouseout event
				}
			} else { //use css style button
				liEl.className = buttons[i].cssClass; //set the classname of the list item
				liAnchorEl.innerHTML = buttons[i].text; //add button text into the anchor DOM element
			}	
		}
		return ulEl; //return the finishe DOM object
	} else return null;	
}