/*
	dc-interactive.net
*/

var myWindow = Class.create();
myWindow.prototype = {
	initialize: function() {
		this.options = Object.extend({
			title: 'My GBox',
			width: 500,
			height: 400,
			url: '',
			params: '',
			method: 'post'
		}, arguments[0] || {});
		
		var pageSize = getPageSize();
		//alert(pageSize);
		//alert(this.options.width);
		//alert(this.options.height);
		
		/*if(this.options.width >= pageSize[2])
			this.options.width = pageSize[2] - 100;
			
		if(this.options.height >= pageSize[3])
			this.options.height = pageSize[3] - 100;*/
		
		pageSize[2] = pageSize[2] - 50;
		pageSize[3] = pageSize[3] - 75;
		
		if(this.options.width >= pageSize[2])
		{
			ratio = pageSize[2] / this.options.width;
			this.options.width = parseInt(this.options.width * ratio);
			this.options.height = parseInt(this.options.height * ratio);
		}
		
		if((this.options.height) >= pageSize[3])
		{
			ratio = pageSize[3] / (this.options.height);
			this.options.width = parseInt(this.options.width * ratio);
			this.options.height = parseInt(this.options.height * ratio);
			
			//this.options.height = pageSize[3] - 75;
		}
		//alert(this.options.width);
		//alert(this.options.height);
		
		if(!$('GB_overlay') || !$('GB_container')) {
			var mBody = document.getElementsByTagName("body").item(0);
			
			// Creating Overlay
			var overlay = document.createElement("div");
			overlay.setAttribute('id','GB_overlay');
			overlay.style.display = 'none';
			mBody.appendChild(overlay);
			overlay = $('GB_overlay');
			
			// Creating Container
			var container = document.createElement("div");
			container.setAttribute('id','GB_container');
			container.style.visibility = 'hidden';
			mBody.appendChild(container);
			container = $('GB_container');
			
			// Creating header
			var header = document.createElement("div");
			header.setAttribute('id','GB_header');
			container.appendChild(header);
			header = $('GB_header');
			
			// Creating header h3
			var heading = document.createElement("h3");
			heading.setAttribute('id','GB_heading');
			//heading.innerHTML = this.options.title;
			header.appendChild(heading);
			
			// Creating header close button
			var closeButton = document.createElement("span");
			closeButton.setAttribute('id','GB_closeButton');
			closeButton.innerHTML = 'X';
			closeButton.onclick = function() {this.end();return false;}.bind(this);
			header.appendChild(closeButton);
			
			// Creating body
			var body = document.createElement("div");
			body.setAttribute('id','GB_body');
			container.appendChild(body);
			
			// Creating Content Area
			var content = document.createElement("div");
			content.setAttribute('id','GB_content');
			body.appendChild(content);
			content = $('GB_content');
			content.addClassName('cb_yellow');
			
			this.newHeight = parseInt(this.options.height + header.getHeight());
			
			content.setStyle({'height': this.options.height+'px'});
			container.setStyle({'height': this.newHeight+'px'});
		}
		else
		{
			this.newHeight = $('GB_container').getHeight();
			container = $('GB_container');
		}
		
		$('GB_heading').update(this.options.title);
		body = $('GB_body');
		windowWidth = parseInt(this.options.width) + parseInt(body.getStyle('border-left-width')) + parseInt(body.getStyle('border-right-width'));
		container.setStyle({'width': windowWidth+'px'});
		
		this.start();
	},
	
	start: function() {
		
		this._prepareIE("100%", "hidden", true);
		window.scrollTo(0,0);
		
		this.setCoordinates();
		//Event.observe(window, 'scroll', this.setCoordinates.bindAsEventListener(this));
		
		var pageSize = getPageSize();
		if(Prototype.Browser.Opera)
			$('GB_overlay').setStyle({'height': $('holder').getHeight()+'px'});
		else
			$('GB_overlay').setStyle({'height': parseInt(pageSize[1])+'px'});
		new Effect.Appear('GB_overlay', {
			duration: 0.4,
			from: 0.0,
			to: 0.7
		});
		
		$('GB_container').setStyle({'display': 'none', 'visibility': 'visible'});
		new Effect.Appear('GB_container', {
			duration: 0.8,
			beforeStart: function() {
				handleGarbage(true, true);
			},
			afterFinish: function() {
				this.loadContent();
			}.bind(this)
		});
		
		new Draggable('GB_container',{
			handle:'GB_header',
			starteffect:function(){
				return;
			},
			endeffect:function(){
				return;
			}
		});
	},
	
	end: function() {
		
		$('GB_content').update('');
		new Effect.Fade('GB_container', { duration: 0.4});
		new Effect.Fade('GB_overlay', {
			duration: 0.8,
			from: 0.7,
			afterFinish: function () {
				this._prepareIE('', '');
				handleGarbage(false, true);
			}.bind(this)
		});
		//Event.stopObserving(window, 'scroll', this.bsetCoordinates);
	},
	
	loadContent: function() {
		$('GB_content').update('');
		$('GB_content').startWaiting('bigBlackWaiting');
		new Ajax.Request(this.options.url, {
			onSuccess : function(r) {
				$('GB_content').stopWaiting();
				$('GB_content').update(r.responseText);
			},
			method: this.options.method,
			parameters: this.options.params
		});
	},
	
	setCoordinates: function() {
		
		var pageSize = getPageSize();
		var pageScroll = getPageScroll();
		
		var GB_left = parseInt(((pageSize[0] / 2) + pageScroll[0]) - (this.options.width / 2));
		var GB_top = (pageScroll[1] + (pageSize[3] / 2)) - (this.newHeight / 2);
		//var GB_top = pageScroll[1] + (pageSize[3] / 15);
		
		if(GB_top < 10)
			GB_top = 10;
		
		if(pageSize[1] > GB_top + this.options.height)
			$('GB_container').setStyle({'left': GB_left+'px', 'top': GB_top+'px'});
	},
	
	_prepareIE: function(height, overflow, hide){
		
		/*bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;*/
		
		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow;
		
		handleGarbage(hide);
	}
}


var Popup = Class.create();
Popup.prototype = {
	initialize: function(url) {
		this.options = Object.extend({
			id: 'my_popup',
			width: 500,
			height: 400
		}, arguments[1] || {});
		
		if(Prototype.Browser.Opera)
		{
			alert('This option is not available for opera');
			return;
		}
		
		this.screenHeight = parseInt(screen.height - 200);
		ratio = this.screenHeight / screen.height;
		this.screenWidth = screen.width * ratio;
		//alert(this.screenWidth + ' - ' + this.options.width);
		
		if(this.options.width > this.screenWidth)
		{
			//alert('1');
			ratio = this.screenWidth / this.options.width;
			this.options.width = parseInt(this.options.width * ratio);
			this.options.height = parseInt(this.options.height * ratio);
		}
		
		if(this.options.height > this.screenHeight)
		{
			//alert('2');
			ratio = this.screenHeight / this.options.height;
			this.options.width = parseInt(this.options.width * ratio);
			this.options.height = parseInt(this.options.height * ratio);
		}
		
		//alert(this.screenWidth + ' - ' + this.options.width);
		
		if(this.options.width == this.screenWidth)
		{
			//alert('3');
			this.options.width = this.screenWidth - 200;
			ratio = this.options.width / this.screenWidth;
			//this.options.width = parseInt(this.options.width * ratio);
			this.options.height = parseInt(this.options.height * ratio);
		}
		
		if(this.options.height == this.screenHeight)
		{
			//alert('4');
			this.options.height = this.screenHeight - 100;
			ratio = this.options.height / this.screenHeight;
			this.options.width = parseInt(this.options.width * ratio);
			//this.options.height = parseInt(this.options.height * ratio);
		}
		
		//alert(this.options.width + ' - ' +  this.options.height);
		
		//alert(this.options.width);
		//alert(this.options.height);
		
		var win = window.open(url, this.options.id, 'toolbar=no, location=no, status=no, menubar=no, scrollbars=no , resizable=no, width='+this.options.width+', height='+this.options.height+', left='+(screen.width  - this.options.width)/2+', top='+parseInt(screen.height - this.options.height)/2);
		win.focus();
		return false;
	}
}

/*
1250*1041

583*435
*/