//-------------------------------------------------------------------//
/* Dialog Handler */
//-------------------------------------------------------------------//

var is_IE = navigator.appName.indexOf('Microsoft') != -1;

var dialogHandler = Class.create();
dialogHandler.prototype = {

	//-------------------------------------------------------------------//

	initialize: function() {
		this.overlay;
		this.dialog = $('dialog');

		this.initOverlay();
	},

	//-------------------------------------------------------------------//
	
	initOverlay: function() {
		var objBody = document.getElementsByTagName("body").item(0);
		
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		objOverlay.style.display = 'none';
		objOverlay.style.position = 'absolute';
		objOverlay.style.top = '0';
		objOverlay.style.left = '0';
		objOverlay.style.zIndex = '90';
		objOverlay.style.width = '100%';
		objBody.insertBefore(objOverlay, objBody.firstChild);

		this.overlay = objOverlay;

		var arrayPageSize = this.getPageSize();
		var arrayPageScroll = this.getPageScroll();	
	},

	//-------------------------------------------------------------------//
	
	getPageScroll: function() {

		var yScroll;

		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}

		arrayPageScroll = new Array('',yScroll); 
		return arrayPageScroll;
	},

	//-------------------------------------------------------------------//
	
	getPageSize: function() {
		
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}


		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	},

	//-------------------------------------------------------------------//

	showDialog: function() {
		var arrayPageSize = this.getPageSize();
		var arrayPageScroll = this.getPageScroll();

		this.overlay.style.height = (arrayPageSize[1] + 'px');
		this.overlay.style.display = 'block';
		this.dialog.style.display = 'block';

		lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3]) / 5);
		if(is_IE) lightboxLeft = ((arrayPageSize[0] + 200) / 4);
		else lightboxLeft = ((arrayPageSize[0] - 100) / 2);
		
		this.dialog.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		this.dialog.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";

		this.hideSelects('hidden');
		this.hideAdverts('hidden');
	},

	//-------------------------------------------------------------------//

	hideSelects: function(visibility){
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},

	//-------------------------------------------------------------------//

	hideAdverts: function(visibility){
		if($('a1SideBanner')) $('a1SideBanner').style.visibility = visibility;
		if($('a1BottomBanner')) $('a1BottomBanner').style.visibility = visibility;
	},

	//-------------------------------------------------------------------//

	showRegDialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_reg', null ));
		this.showDialog();
		$('reg-name').focus();
	},

	//-------------------------------------------------------------------//

	showLoginDialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_login', null));
		this.showDialog();
		$('login-name').focus();
	},

	//-------------------------------------------------------------------//

	showErrorRegStatusDialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_error_reg_status', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showSuccessProfileUpdateDialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_profile_success_update', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showSuccessUploadDialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_success_upoad', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showSuccessAddToFavoriteDialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_success_favorite', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showErrorAddToFavoriteDialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_error_favorite', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showSuccessReportDialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_success_report', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showErrorReportDialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_error_report', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showSuccessSuggestDialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_success_suggest', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showErrorSuggestDialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_error_suggest', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showNeedToLogin: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_need_login', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showNewsletterError: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_newsletter', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showNewsletterSuccess: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_newsletter', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showSuccessProfileErrorAvatar: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_error_profile', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showSendDialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_send', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showVoteDialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_vote', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showActivateStep1Dialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_activate_step1', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showActivateAlreadyDialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_activate_already', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showActivateSuccessDialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_activate_success', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showForgottenPasswdDialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_forgotten_passwd', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showNewPasswdStep1Dialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_forgotten_step1', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showNewPasswdSuccessDialog: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_forgotten_success', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	showAdminPosError: function() {
		this.addEvents();
		this.dialog.update(TrimPath.processDOMTemplate('t_box_admin_error_pos', null));
		this.showDialog();
	},

	//-------------------------------------------------------------------//

	closeDialog: function(url) {
		this.overlay.style.display = 'none';
		this.dialog.style.display = 'none';
		this.dialog.update('');
		this.hideSelects('visible');
		this.hideAdverts('visible');

		if(typeof(url) != 'undefined' && url.length != 0) {
			document.location.href = url;
		}
	},

	//-------------------------------------------------------------------//

	addEvents: function() {
		Event.observe(document,'keydown',function(e){
			if(window.event) var e = window.event;

			if(e.keyCode == 27) {
				this.closeDialog();
				Event.unloadCache();
			}

		}.bind(this));
	}

 	//-------------------------------------------------------------------//

}