//	wildcards:
//	- AdContainer		the classname for the widget
YAHOO.namespace("gs.widget");
(function(){
	//	some shortcuts to common libraries
	var Dom = YAHOO.util.Dom,
		Event = YAHOO.util.Event,
		Button = YAHOO.widget.Button,
		Menu = YAHOO.widget.Menu,
		DataSource = YAHOO.util.DataSource,
		GSTools = YAHOO.gs.tools,
		CustomEvent = YAHOO.util.CustomEvent;

	//	create the constructor
	var AdContainer = function(el,config){
		AdContainer.superclass.constructor.call(this,el,config);
		
		el = this.get("element");
		this._iv;
		this._to;
		this._zIndex = 10;
		this._lastOverlay = "first";
		this.refresh([],true);
		
		
		var firstOverlayEl = document.createElement("div");
		firstOverlayEl.innerHTML = el.innerHTML;
		
		var secondOverlayEl = document.createElement("div");
		secondOverlayEl.innerHTML = "hallo world";
		
		el.innerHTML = "";
		firstOverlayEl = el.appendChild(firstOverlayEl);
		secondOverlayEl = el.appendChild(secondOverlayEl);
		
		//	first overlay
		var firstOverlay = new YAHOO.widget.Overlay(firstOverlayEl, { 
			visible:true,
			effect:this.get("overlayEffect"),
			zIndex : this._zIndex
		});
		Dom.setStyle(firstOverlay.element,"position","absolute");
		Dom.setStyle(firstOverlay.element,"left","1px");
		Dom.setStyle(firstOverlay.element,"top","1px");
		firstOverlay.render(el);
		firstOverlay.show();
		this._configs.firstOverlay.value = firstOverlay;

		//	second overlay
		var secondOverlay = new YAHOO.widget.Overlay(secondOverlayEl, { 
			visible:false,
			effect:this.get("overlayEffect"),
			zIndex : this._zIndex+1
		});
		Dom.setStyle(secondOverlay.element,"position","absolute");
		Dom.setStyle(secondOverlay.element,"left","1px");
		Dom.setStyle(secondOverlay.element,"top","1px");
		secondOverlay.render(el);
		secondOverlay.hide();
		this._configs.secondOverlay.value = secondOverlay;
	}
	
	YAHOO.lang.extend(AdContainer,YAHOO.util.Element);
	
	var proto = AdContainer.prototype;
	
	proto.initAttributes = function(config){
		AdContainer.superclass.initAttributes.call(this,config);
		
		this.setAttributeConfig('container_id', {
			writeOnce : true,
			value : config.container_id || 0,
			validator: YAHOO.lang.isNumber
		});
		
		this.setAttributeConfig('itemInterval', {
			value : config.itemInterval || 0,
			validator: YAHOO.lang.isNumber,
			method : function(value){
				if(this._iv){
					this.showItems(value);
				}
			}
		});
		
		this.setAttributeConfig('lastItem', {
			readOnly : true,
			value : {}
		});
		
		this.setAttributeConfig('lastItemId', {
			value : config.lastItemId || 0,
			validator: YAHOO.lang.isNumber,
			method : function(value){
				
			}
		});
		
		this.setAttributeConfig('itemRequestUrl', {
			value : config.itemRequestUrl || "Web/Adcontainer/viewitem/{container_id}?format=json",
			validator: YAHOO.lang.isString,
			method : function(value){
				
			}
		});
		
		this.setAttributeConfig('overlayEffect', {
			writeOnce : true,
			value : config.overlayEffect || {effect:YAHOO.widget.ContainerEffect.FADE, duration: 1},
			validator: function(value){
				if(YAHOO.lang.isObject(value) || YAHOO.lang.isNull(value)) return true;
				return false;
			}
		});

		this.setAttributeConfig('firstOverlay', {
			readOnly : true,
			value : undefined
		});
		
		this.setAttributeConfig('secondOverlay', {
			readOnly : true,
			value : undefined
		});
	}
	
	//	public methods
	proto.showItems = function(itemInterval,lastItemId,firstDelay){
		clearInterval(this._iv);
		clearTimeout(this._to);
		
		if(itemInterval > 0) this._configs.itemInterval.value = itemInterval;
		itemInterval = this.get("itemInterval");
		if(lastItemId >= 0) this.set("lastItemId",lastItemId);
		
		var callback = {
			success : function(xhr){
				var resp = GSTools.parseJsonString(xhr.responseText);
				if(!resp.error){
					lastItem = resp.item_id;
					this.set("lastItemId",Math.round(resp.item_id));
					
					this._zIndex++;
					
					resp.itemHTML = resp.itemHTML.replace(new RegExp("&lt;","gi"),"<");
					resp.itemHTML = resp.itemHTML.replace(new RegExp("&gt;","gi"),">");
					
					var fo = this.get("firstOverlay");
					var so = this.get("secondOverlay");
					if(this._lastOverlay == "first"){
						fo.hide();
						so.element.innerHTML = resp.itemHTML;
						this._lastOverlay = "last";
						so.show();
					}else{
						so.hide();
						fo.element.innerHTML = resp.itemHTML;
						this._lastOverlay = "first";
						fo.show();
					}
				}
			},
			failure : function(xhr){
			},
			argument : {
			},
			timeout : 5000,
			scope : this
		};

		var obj = this;
		var next = function(){
			var _url = obj.get("itemRequestUrl").replace("{container_id}",obj.get("container_id"));
			var lastItemId = obj.get("lastItemId");
			if(lastItemId > 0){
				_url+= "&lastItem="+lastItemId;
			}
			YAHOO.util.Connect.asyncRequest('GET', _url, callback);
		}
		
		var setIV = function(){
			if(itemInterval > 0){
				obj._iv = setInterval(next,itemInterval);
			}else{
				next();
			}
		}
		
		if(firstDelay > 0){
			this._to = setTimeout(setIV,firstDelay);
		}else{
			setIV();
		}
		
		

	}
	
	//	private methods
	var _privateMethod = function(){
	}
	
	YAHOO.gs.widget.AdContainer = AdContainer;
})();