if(typeof ND == "undefined") {
	ND = {};
}

ND.CornerAd = {

	_container: null,
	_flash: null,
	
	// container sizes
	_minWidth: 90,
	_minHeight: 75,
	_maxWidth: 470,
	_maxHeight: 370,
	
	// max x position for container
	_xPosition: 0,
	
	// settings passed by setup
	_settings: null,
	
	setup: function(oObject) {
		this._settings = oObject;
	},
	
	/**
	 * Init the DOM elements, and apply flash
	 */
	init: function() {
        var $body = $$('body')[0];
        
        // if no metadata or settings, cancel initialization
        if(!this._settings) {
	        return;
        }

        // create the container holding the flash
        this._container = new Element('div');
        this._container.writeAttribute('id', 'corner_ad');
        this._flash = new Element('div');
        this._flash.writeAttribute('id', 'corner_ad_flash');
        this._container.insert(this._flash);
        $body.insert(this._container);
        
        // apply the flash
        var swf = 'portal/flash/corner_ad.swf';
        
        var flashVars = {
        		close_callback : 	'ND.CornerAd.onFlashClose()',
        		open_callback : 	'ND.CornerAd.onFlashOpen()',
        		background_image : 	this._settings.background_image,
        		corner_image : 		this._settings.corner_image,
        		target_url : 		this._settings.target_url
        };
        
		var params = {
				base: 'portal/flash/', 
				allowScriptAccess: 'always',
				wmode: 'transparent'
		};
		
		var attributes = {};
		
        // SWF object 2.0 - not used on melitta:
		//swfobject.embedSWF(swf, sFlashContainer, 600, 550, "9.0.0", null, flashVars, params, attributes);
        
	    var so = new SWFObject(swf, "corner_add", 600, 550, "9");
	    for(var i in params) {
	    	so.addParam(i, params[i]);
	    }
	    for(var i in flashVars) {
	    	so.addVariable(i, flashVars[i]);
	    }
	    
	    so.write("corner_ad_flash");
		
	    // calculate absolute position
        this.updatePosition();
        
        // start-state: minimized
        this.minimize();
        
        this.observeEvents();
        
	},
	
	/**
	 * Callback for flash,
	 * called when close animation finished
	 */
	onFlashClose: function() {
		this.minimize();
	},

	/**
	 * Callback for flash
	 * called when open animation starts
	 */
	onFlashOpen: function() {
		this.maximize();
	},
	
	/**
	 * Apply observers for minimizing and maximizing container
	 */
	observeEvents: function() {
		var self = this;
		
		Event.observe(window, 'resize', function(e) {
			self.updatePosition();
			self.minimize();
		});
		
		Event.observe(this._container, 'mouseover', function() {
			self.maximize();
		});
		
	},
	
	/**
	 * Updates the container position, when window resizes
	 */
	updatePosition: function() {
        var $main = $$('div.main')[0];

        var viewport = document.viewport.getDimensions(); // Gets the viewport as an object literal
        var widthView = viewport.width; // Usable window width
        var withMain = 1070; //$main.getWidth() + this._minWidth;
        this._xPosition = widthView > withMain ? widthView : withMain;
	},
	
	/**
	 * Minimize the container
	 */
	minimize: function() {
        this._container.setStyle({
        	width : this._minWidth + 'px', 
        	height: this._minHeight + 'px',
        	left: (this._xPosition - this._minWidth) + 'px'
        });
	},
	
	/**
	 * Maximize the container
	 */
	maximize: function() {
		this._container.setStyle({
			width : this._maxWidth + 'px', 
			height: this._maxHeight + 'px',
			left: (this._xPosition - this._maxWidth) + 'px'
		});
	}

};

Event.observe(window, 'load', function() {
	ND.CornerAd.init();
});

