var evoRcPanel;

// Articles panel subclass
EvoRcPanel = function(el, userConfig) {
	if (arguments.length > 0) {
		EvoRcPanel.superclass.constructor.call(this, el, userConfig);
	}
}

// Inherit from YAHOO.widget.Panel
YAHOO.extend(EvoRcPanel, YAHOO.widget.Panel);

// Initialize the evoRcPanel
EvoRcPanel.prototype.init = function(el, userConfig) {
	EvoRcPanel.superclass.init.call(this, el);
	
	this.beforeInitEvent.fire(EvoRcPanel);
	
	this.beforeRenderEvent.subscribe(function() {
			if (! this.footer) {
				this.setFooter("");
			}
		},
		this, true
	);

	this.renderEvent.subscribe(function() {
		var me = this;

		var headerHeight = me.header.offsetHeight;

		me.bodyContentDiv = YAHOO.util.Dom.getElementsByClassName("content", "div", me.body)[0];
		var contentDivId = YAHOO.util.Dom.generateId(me.bodyContentDiv, "evorcpanel");
		me.bodyContentDiv.setAttribute("id", contentDivId);
		
		me.selectionDiv = YAHOO.util.Dom.getElementsByClassName("selection", "span", me.body)[0];
		me.navDiv = YAHOO.util.Dom.getElementsByClassName("nav", "span", me.body)[0];

		me.navList = { testimonials: 'Testimonials', evoglossary: 'Glossary', evofaq: "FAQ's" };
		
		var navHTML = "";
		var footerHTML = "";
		 
		for (var sectionId in me.navList) {
			navHTML += '<a id="evorcnav_' + sectionId + '" class="off" href="javascript:evoRcPanel.showSection(\'' + sectionId + '\');">' + me.navList[sectionId] + '</a>';
			if (footerHTML == "") {
				footerHTML = "<span>";
			} else {
				footerHTML += '<span class="sep">|</span>';
			}
			footerHTML += '<a href="javascript:evoRcPanel.showSection(\'' + sectionId + '\');">' + me.navList[sectionId] + '</a>';
		}
		footerHTML += "</span>";
		
		me.navDiv.innerHTML = navHTML;
		me.footer.innerHTML = footerHTML;
		
	}, 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(evoRcPanel);
}

// Set up the default values for the properties
EvoRcPanel.prototype.initDefaultConfig = function() {
	EvoRcPanel.superclass.initDefaultConfig.call(this);
	this.cfg.addProperty("sectionid", {value: 'articles'});
	this.cfg.addProperty("getevoresortcentreserviceeurl", {value: ""});
}

EvoRcPanel.prototype.configWidth = function(type, args, obj) {
	EvoRcPanel.superclass.configWidth.call(this, type, args, obj);
//	var width = args[0];
//	if (width) {
//		var intWidth = parseInt(width);
//		intWidth -= 210;
//		YAHOO.util.Dom.setStyle(this.bodyContentDiv, "width", "" + intWidth + "px");
//	}
}

EvoRcPanel.prototype.configHeight = function(type, args, obj) {
	var intHeight = 200; 
	if (this.bodyContentDiv && this.bodyContentDiv.firstChild) {
		if (this.autoHeight) {
			intHeight = Math.min(600, this.bodyContentDiv.firstChild.clientHeight);
//			intHeight = Math.max(180, intHeight);
		} else {
			intHeight = parseInt(args[0]);
			intHeight -= 260;
		}
	}
	args[0] = ""+(intHeight + 260)+"px";
	EvoRcPanel.superclass.configHeight.call(this, type, args, obj);

	YAHOO.util.Dom.setStyle(this.bodyContentDiv, "height", "" + intHeight + "px");

//	var intWidth = parseInt(this.cfg.getProperty("width")) - 210;
//	YAHOO.util.Dom.setStyle(this.bodyContentDiv, "width", "" + intWidth + "px");
}

EvoRcPanel.prototype.showSection = function(sectionId, serviceUrl, width, height ) {
	this.cfg.setProperty("sectionid", sectionId);
	if (serviceUrl && serviceUrl != "") {
		this.cfg.setProperty("getevoresortcentreserviceeurl", serviceUrl);
	} else {
		serviceUrl = this.cfg.getProperty("getevoresortcentreserviceeurl");
	}
	
	if (!width) { width = 770; }
	if (height) { 
		this.cfg.setProperty("height", "" + height + "px");
	} else {
		//this.autoHeight = true;
	}

	this.autoHeight = false;
	this.cfg.setProperty("height", "600px");
	this.cfg.setProperty("width", "" + width + "px");
//	this.selectionDiv.innerHTML = "<span>" + this.navList[sectionId] + "</span>";
	this.selectionDiv.innerHTML = this.navList[sectionId];
	for (var i in this.navList) {
		if (i == sectionId) {
			YAHOO.util.Dom.replaceClass(YAHOO.util.Dom.get("evorcnav_" + i), "off", "on");
		} else {
			YAHOO.util.Dom.replaceClass(YAHOO.util.Dom.get("evorcnav_" + i), "on", "off");
		}
	}
	
	var params = new SOAPClientParameters();
	params.add("sectionID", sectionId);
	SOAPClient.invoke(serviceUrl, "getSection", params, true, this.handleMessage_callback, this);
}

EvoRcPanel.prototype.handleMessage_callback = function(result, xmlResponse) {
	YAHOO.util.Event.onContentReady(evoRcPanel.bodyContentDiv.id, function() {
		evoRcPanel.cfg.setProperty("height", evoRcPanel.cfg.getProperty("height"));
	}, evoRcPanel, true);

	evoRcPanel.bodyContentDiv.innerHTML = '<div style="height:auto;">' + result.content + '</div>';
	
	evoRcPanel.center();
	YAHOO.util.Dom.setStyle(evoRcPanel.element, "top", "50px");	
	evoRcPanel.show();
}

YAHOO.util.Event.addListener(window, "load", function() {
	// create div structures
	var evoRcPanelDiv = document.createElement("DIV");
	evoRcPanelDiv.setAttribute("id", "evoRcPanel");
	document.body.appendChild(evoRcPanelDiv);
	evoRcPanelDiv.innerHTML = 
'			<div class="hd">'
+ '				<div class="logo"><img src="images/articlespanel/vc_logo_229x49.png" width="229" height="49" alt="Vacation Source.com"  style="behavior: url(\'scripts/pngbehavior.htc\');"></div>'
+ '				<span>'
+ '					<a class="close" href="javascript:evoRcPanel.hide();">CLOSE WINDOW</a>'
+ '				</span>'
+ '			</div>'
+ '			<div class="bd">'
+ '				<div class="yui-content">'
+ '					<div class="mast">'
+ '						<div class="bkg">'
+ '							<img src="images/articlespanel/mastphoto4.png" width="201" height="116" alt="Vacation Ownership reSource center"><img' 
+ '								src="images/articlespanel/mastphoto3.png" width="173" height="116" alt=""><img'
+ '								src="images/articlespanel/mastphoto2.png" width="173" height="116" alt=""><img'
+ '								src="images/articlespanel/mastphoto1.png" width="173" height="116" alt="">'
+ '						</div>'
+ '						<div class="menu">'
+ '							<span class="selection"></span>'
+ '							<span class="nav"></span>'
+ '						</div>'
+ '					</div>'
+ '					<div style="clear:both;height:10px;">&nbsp;</div>'
+ '					<div class="content"></div>'
+ '					<div style="clear:both;"></div>'
+ '				</div>'
+ '			</div>'
+ '			<div class="ft"></div>';
	
	evoRcPanel = new EvoRcPanel("evoRcPanel", { getevoresortcentreserviceeurl: gGetEvoResortCenterServiceUrl,
		fixedcenter:false, visible:false,  constraintoviewport:false, underlay: 'none', close: false });
	evoRcPanel.render();
});
