var infoPanel;

// Info panel subclass
InfoPanel = function(el, userConfig) {
	if (arguments.length > 0) {
		InfoPanel.superclass.constructor.call(this, el, userConfig);
	}
}

// Inherit from YAHOO.widget.Panel
YAHOO.extend(InfoPanel, YAHOO.widget.Panel);

InfoPanel.CSS_PANEL_RESIZE = "infopanelresize";
InfoPanel.CSS_RESIZE_HANDLE = "infopanelhandle";

			
// Initialize the InfoPanel
InfoPanel.prototype.init = function(el, userConfig) {
	InfoPanel.superclass.init.call(this, el);
	
	this.beforeInitEvent.fire(InfoPanel);
	
	YAHOO.util.Dom.addClass(this.innerElement, InfoPanel.CSS_PANEL_RESIZE);

	this.resizeHandle = document.createElement("div");
	this.resizeHandle.id = this.id + "_r";
	this.resizeHandle.className = InfoPanel.CSS_RESIZE_HANDLE;

	this.beforeRenderEvent.subscribe(function() {
			if (! this.footer) {
				this.setFooter("");
			}
		},
		this, true
	);

	this.renderEvent.subscribe(function() {
		var me = this;

		me.innerElement.appendChild(me.resizeHandle);

		this.ddResize = new YAHOO.util.DragDrop(this.resizeHandle.id, this.id);
		this.ddResize.setHandleElId(this.resizeHandle.id);

		var headerHeight = me.header.offsetHeight;

		this.ddResize.onMouseDown = function(e) {

			this.startWidth = me.innerElement.offsetWidth;
			this.startHeight = me.innerElement.offsetHeight;

			me.cfg.setProperty("width", this.startWidth + "px");
			me.cfg.setProperty("height", this.startHeight + "px");

			this.startPos = [YAHOO.util.Event.getPageX(e),
							 YAHOO.util.Event.getPageY(e)];

			me.innerElement.style.overflow = "hidden";
//			me.body.style.overflow = "auto";
		}

		this.ddResize.onDrag = function(e) {
			var newPos = [YAHOO.util.Event.getPageX(e),
						  YAHOO.util.Event.getPageY(e)];

			var offsetX = newPos[0] - this.startPos[0];
			var offsetY = newPos[1] - this.startPos[1];

			var newWidth = Math.max(this.startWidth + offsetX, 10);
			var newHeight = Math.max(this.startHeight + offsetY, 10);

			me.cfg.setProperty("width", newWidth + "px");
			me.cfg.setProperty("height", newHeight + "px");

			var bodyHeight = (newHeight - 5 - me.footer.offsetHeight - me.header.offsetHeight - 3);
			if (bodyHeight < 0) {
				bodyHeight = 0;
			}
			me.body.style.height =  bodyHeight + "px";

			var innerHeight = me.innerElement.offsetHeight;
			var innerWidth = me.innerElement.offsetWidth;

			if (innerHeight < headerHeight) {
				me.innerElement.style.height = headerHeight + "px";
			}

			if (innerWidth < 20) {
				me.innerElement.style.width = "20px";
			}
		}


		me.bodyContentDiv = YAHOO.util.Dom.getElementsByClassName("yui-content", "div", me.body)[0];
		var resortSliderDiv = YAHOO.util.Dom.getElementsByClassName("yui-slider", "div", me.body)[0];
		var resortSliderThumbDiv = YAHOO.util.Dom.getElementsByClassName("yui-sliderhandle", "div", me.body)[0];
		var resortSliderDivId = YAHOO.util.Dom.generateId(resortSliderDiv, "infopanel");
		var resortSliderThumbDivId = YAHOO.util.Dom.generateId(resortSliderThumbDiv, "infopanel");
		var contentDivId = YAHOO.util.Dom.generateId(me.bodyContentDiv, "infopanel");
		resortSliderDiv.setAttribute("id", resortSliderDivId);
		resortSliderThumbDiv.setAttribute("id", resortSliderThumbDivId);
		me.bodyContentDiv.setAttribute("id", contentDivId);
		
		// create slider
		me.resortSlider = YAHOO.widget.Slider.getVertSlider(resortSliderDivId, resortSliderThumbDivId, 0, 200);
		me.resortSlider.subscribe("change", me.handleSliderChange, me, true);
	}, this, true);
	
	if (userConfig) {
		this.cfg.applyConfig(userConfig, true);
	}
	
	this.changeBodyEvent.subscribe(function() {
		var me = this;
		
		var scriptlets = me.body.getElementsByTagName("script");
		for (var i = 0; i < scriptlets.length; i++) {
			var scriptElem = document.createElement("script");
			scriptElem.setAttribute("type", "text/javascript");
			if (scriptlets[i].attributes["src"] != null) {
				scriptElem.setAttribute("src", scriptlets[i].attributes["src"].value);
			}
			scriptElem.text = scriptlets[i].innerHTML;
			document.getElementsByTagName("head")[0].appendChild(scriptElem);
		}
		
	}, this, true);
	
	
	this.initEvent.fire(InfoPanel);
}

// Set up the default values for the properties
InfoPanel.prototype.initDefaultConfig = function() {
	InfoPanel.superclass.initDefaultConfig.call(this);
	this.cfg.addProperty("messageid", {value: '0'});
	this.cfg.addProperty("msgtype", {value: 'info'});
	this.cfg.addProperty("title", {handler: this.configTitle, supressEvent:true, value: "Info Popup"});
	this.cfg.addProperty("getpopupcontentserviceurl", {value: ""});
}

InfoPanel.prototype.configWidth = function(type, args, obj) {
	InfoPanel.superclass.configWidth.call(this, type, args, obj);
	var width = args[0];
	if (width) {
		var intWidth = parseInt(width);
		intWidth -= 24;
		if (this.bodyContentDiv && this.bodyContentDiv.firstChild && this.bodyContentDiv.firstChild.scrollHeight > this.bodyContentDiv.clientHeight) {
			YAHOO.util.Dom.setStyle(this.bodyContentDiv, "width", "" + (intWidth -11) + "px");
			YAHOO.util.Dom.setStyle(this.resortSlider.getEl(), "display", "block");
		} else {
			YAHOO.util.Dom.setStyle(this.bodyContentDiv, "width", "" + intWidth + "px");
			if (this.resortSlider) {
				YAHOO.util.Dom.setStyle(this.resortSlider.getEl(), "display", "none");
			}
		}
	}
}

InfoPanel.prototype.configHeight = function(type, args, obj) {
	InfoPanel.superclass.configHeight.call(this, type, args, obj);
	var height = args[0];
	if (height) {
		var intHeight = parseInt(height);
		intHeight -= 39;
		var intWidth = parseInt(this.cfg.getProperty("width")) - 24;
		if (this.bodyContentDiv && this.bodyContentDiv.firstChild && this.bodyContentDiv.firstChild.scrollHeight > this.bodyContentDiv.clientHeight) {
			YAHOO.util.Dom.setStyle(this.bodyContentDiv, "width", "" + (intWidth -11) + "px");
			YAHOO.util.Dom.setStyle(this.resortSlider.getEl(), "display", "block");
		} else {
			YAHOO.util.Dom.setStyle(this.bodyContentDiv, "width", "" + intWidth + "px");
			if (this.resortSlider) {
				YAHOO.util.Dom.setStyle(this.resortSlider.getEl(), "display", "none");
			}
		}
		YAHOO.util.Dom.setStyle(this.body, "height", "" + intHeight + "px");
		YAHOO.util.Dom.setStyle(this.bodyContentDiv, "height", "" + intHeight + "px");
		YAHOO.util.Dom.setStyle(this.resortSlider.getEl(), "height", "" + intHeight + "px");
		this.resortSlider.thumb.initSlider(0, 0, 0, (intHeight - 25));
	}
}

InfoPanel.prototype.configTitle = function(type, args, obj) {
	var title = args[0];
	if (!title) {
		title = "";
	}
	var titleSpan =	YAHOO.util.Dom.getElementsByClassName("infotitle", "span", this.header)[0];
	titleSpan.innerHTML = title;
}

InfoPanel.prototype.showMessage = function(messageId, msgType, serviceUrl, width, height ) {
	this.cfg.setProperty("messageid", messageId);
	this.cfg.setProperty("msgtype", msgType);
	if (serviceUrl && serviceUrl != "") {
		this.cfg.setProperty("getpopupcontentserviceurl", serviceUrl);
	} else {
		serviceUrl = this.cfg.getProperty("getpopupcontentserviceurl");
	}
	
	var params = new SOAPClientParameters();
	params.add("messageID", messageId);
	params.add("msgType", msgType);
	SOAPClient.invoke(serviceUrl, "getPopupContent", params, true, this.handleMessage_callback, this);
	
	if (!width) { width = 250; }
	if (!height) { height = 150; }
	
	this.cfg.setProperty("width", "" + width + "px");
	this.cfg.setProperty("height", "" + height + "px");
	
	this.center();
	this.show();
}

InfoPanel.prototype.handleMessage_callback = function(result, xmlResponse, infoPanel) {
	YAHOO.util.Event.onContentReady(infoPanel.bodyContentDiv.id, function() {
		infoPanel.cfg.setProperty("height", infoPanel.cfg.getProperty("height"));
	}, infoPanel, true);
	infoPanel.bodyContentDiv.innerHTML = "<div style='position:absolute;'>" + result.msg + "</div>";
	infoPanel.cfg.setProperty("title", result.title);
}

InfoPanel.prototype.handleSliderChange = function(newOffset) {
	var newTop = newOffset;
	var contentEl = this.bodyContentDiv.firstChild;
	if (contentEl) { 
		var newTop = newOffset * (contentEl.scrollHeight - this.bodyContentDiv.clientHeight) / (this.bodyContentDiv.clientHeight -25);//this.resortSlider.thumb.maxY;
		var newTop = Math.min(-newTop, 0);
		YAHOO.util.Dom.setStyle(contentEl, "top", "" + newTop + "px");
	}
}

YAHOO.util.Event.addListener(window, "load", function() {
	// create div structures
	var infoPanelDiv = document.createElement("DIV");
	infoPanelDiv.setAttribute("id", "infoPanel");
	document.body.appendChild(infoPanelDiv);
	infoPanelDiv.innerHTML = 
		'<div class="hd"><div class="tl"></div><span class="infotitle">&nbsp;</span><div class="tr"></div></div>'
		+ '<div class="bd">'
		+ '		<div class="yui-content"></div>'
		+ '		<div class="yui-slider">'
		+ '			<div class="yui-sliderhandle">'
		+ '			 	<img src="/images/resortpanel/lthumb.gif" border="0" alt="-">'
		+ '			 </div>'
		+ '		</div>'
		+ '</div>'
		+ '<div class="ft"><div class="bl"></div><div class="br"></div></div>';
	
	infoPanel = new InfoPanel("infoPanel", { title: "Info Popup", getpopupcontentserviceurl: gPopupContentService,
		fixedcenter:true,  visible:false, constraintoviewport:false, underlay: 'none'});
	infoPanel.render();
	
});
