//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){

	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;
	}

	return [pageWidth,pageHeight,windowWidth,windowHeight];
}


function show_transition(url)
{
	var dim = getPageSize();
	var width = dim[0];
	var height = dim[1];
	var windowHeight = dim[3];
	var img_left = Math.round(width/2) - 259;
	if(img_left < 0) img_left = 0;
	var img_top = Math.round(windowHeight/2) - 70;
	if(img_top < 0) img_top = 0;
	var close_left = img_left + 454;
	var img_url = 'http://www.myprofile.bg/public/images/overlay.png';

	var img = new Element('img', { 'src' : img_url })
	var a = new Element('a', { 'id' : 'trn_img', 'href' : url }).update(img);

	$(a).setStyle({
					'position'		: 'absolute',
					'top'			: img_top+'px',
					'left'			: img_left+'px',
					'zIndex'		: '5000'
				});

	var close = new Element('a', { 'id' : 'trn_close_btn', 'href' : 'javascript:;' }).update('<img src="http://www.myprofile.bg/public/images/close_btn.png" />');
	$(close).setStyle({
					'position'		: 'absolute',
					'top'			: (img_top+3)+'px',
					'zIndex'		: '5000',
					'width'			: '60px',
					'height'		: '17px',
					'display'		: 'block',
					'color'			: '#000',
					'textDecoration': 'none',
					'left'			: close_left+'px'
				});

	var over = new Element('div', { 'id' : 'overlay' });
	$(over).setStyle({
					'width' 		: '100%',
					'height' 		: height+'px',
					'position' 		: 'absolute',
					'top'	 		: '0px',
					'left'	 		: '0px',
					'textAlign'		: 'center',
					'background'	: '#000',
					'zIndex'		: '5000',
					'opacity'		:'.75',
					'filter'		: 'alpha(opacity=75)',
					'-moz-opacity'	: '0.75'
				});

	$$('embed', 'object').each(function(el){el.setStyle({'visibility':'hidden'})});
	var body_elem = $$("body")[0];
	Element.insert(body_elem, { 'bottom' : over });
	Element.insert(body_elem, { 'bottom' : a });
	Element.insert(body_elem, { 'bottom' : close });

	var per = new PeriodicalExecuter(function(pe) {
		location.href = url;
		pe.stop();
		}, 5);

	$('trn_close_btn').observe('click', function(){
		per.stop();
		$('overlay').remove();
		$('trn_img').remove();
		$('trn_close_btn').remove();
		$$('embed', 'object').each(function(el){el.setStyle({'visibility':'visible'})});
	}.bind(per));
}

Event.observe(window, 'load', function() {
	$$("a.transition").each(function(el){
		el.observe('click', function(event){
			Event.stop(event);
			show_transition(Event.element(event).href);
		});
	});
});