var currentPopupId = '';

window.size = function() {
	var w = 0;
	var h = 0;

	//IE
	if(!window.innerWidth) {
		//strict mode
		if(!(document.documentElement.clientWidth == 0)) {
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else {
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	
	return {width:w,height:h};
}

function centerPrompt(elId, elWidth, elHeight) {
	var elObj = document.getElementById(elId);
	
	elObj.style.zIndex = '4000000';
	
	vpSize = window.size();
	vpScroll = window.getScroll();
	
	if(Browser.Engine.trident4) {	
		elObj.style.position = 'absolute';
		elObj.style.top = ((elHeight < vpSize.height) ? vpScroll.y + (vpSize.height - elHeight) / 2 : vpScroll.y) + "px";
		elObj.style.left = ((vpSize.width / 2) - (elWidth / 2)) + "px";
	} else {		
		elObj.style.position = 'fixed';		
		elObj.style.top = ((vpSize.height / 2) - (elHeight / 2)) + "px";
		elObj.style.left = ((vpSize.width / 2) - (elWidth / 2)) + "px";
	}
}

function popPrompt(promptId, promptWidth, promptHeight) {
	if(Browser.Engine.trident4) {
		// Best browser ever made
		$('do').style.position = 'absolute';
	}
			
	$('do').setStyles({
		display: 'block'
	})
	
	centerPrompt(promptId, promptWidth, promptHeight);
	
	$$('object').each(function(el) {
		el.style.visibility = 'hidden';
	});
		
	$(promptId).setStyles({
		display:'block',
		opacity: 0
	});
	
	currentPopupId = promptId;
	
	var mooFade = new Fx.Tween($(promptId), {duration: 1000, transition: Fx.Transitions.Elastic.easeOut});
	mooFade.start('opacity', 0, 1);
}

function popAjaxPrompt(ajUrl, promptWidth, promptHeight) {
	if(popAlreadyOpen()) {
		hidePrompt();
	}
	
	newElId = 'dyn_' + uniqid();
	oldPopupId = currentPopupId;
	currentPopupId = newElId;
	ajSuccessful = false;

	var aj = new Request({url: ajUrl, method: 'get', evalScripts: true, onSuccess: function(ajData) {
		newEl = new Element('div', {
			'id': newElId
		});
		
		newEl.setStyle('display', 'none');
		newEl.setStyle('overflow', 'hidden');
		newEl.setStyle('width', promptWidth + 'px');
		newEl.setStyle('height', promptHeight + 'px');
		
		newEl.set('html', ajData);
		
		newEl.injectInside(document.body);
		
		currentPopupId = newElId;
		
		popPrompt(newElId, promptWidth, promptHeight);
		
		ajSuccessful = true;
	}}).send();
	
	if(!ajSuccessful) {
		currentPopupId = oldPopupId;
	}
}

function hidePrompt() {
	document.getElementById(currentPopupId).style.display = 'none';
	document.getElementById('do').style.display = 'none';
	
	$$('object').each(function(el) {
		el.style.visibility = 'visible';
	});
	
	$(currentPopupId).destroy();
	currentPopupId = '';
}
function delAlbum_submit() { popupAjaxSubmit('delAlbum_form'); }
function addFriend_submit() { popupAjaxSubmit('addFriend_form'); }
function addFan_submit() { popupAjaxSubmit('addFan_form'); }
function addRival_submit() { popupAjaxSubmit('addRival_form'); }
function addReport_submit() { popupAjaxSubmit('addReport_form'); }
function addFavourite_submit() { popupAjaxSubmit('addFavourite_form'); }
function addSubscription_submit() { popupAjaxSubmit('addSubscription_form'); }

function popupAjaxSubmit(formId) {
	$(formId).set('send', {
		method: 'post',
		url: $(formId).getProperty('action'),
		onSuccess: function(respText) {
			$(currentPopupId).set('html', respText);
		}
	});
	
	$(formId).send();
}

function popAlreadyOpen() {
	return (currentPopupId != '') ? true : false;
}

var win=null;
function popCenterWindow(mypage, myname,w, h, scroll) {
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	
	var settings ='height='+h+',';
	settings +='width='+w+',';
	settings +='top='+wint+',';
	settings +='left='+winl+',';
	settings +='scrollbars='+scroll+',';
	settings +='resizable=yes';

	win=window.open(mypage,myname,settings);
	
	if(parseInt(navigator.appVersion) >= 4){
		win.window.focus();
	}
}