

//document.observe('dom:loaded',onloadTransition);
//document.observe('dom:loaded',initializeAnchorTransitions);

addEvent(window,'load',onloadTransition);
addEvent(window,'load',initializeAnchorTransitions);

// Array.shuffle( deep ) - Randomly interchange elements
Array.prototype.shuffle = function( b ) {
 var i = this.length, j, t;
 while( i ) {
  j = Math.floor( ( i-- ) * Math.random() );
  t = b && typeof this[i].shuffle!=='undefined' ? this[i].shuffle() : this[i];
  this[i] = this[j];
  this[j] = t;
 }
 return this;
};

function XbrowserOpac (obj, value) {
	obj.style.opacity = value;
	filterValue = Math.round(value * 100);
	obj.style.filter = "alpha(opacity=" + filterValue + ")";
	//window.status = obj.style.filter;
}

function Sqrs() {

}

Sqrs.prototype.init = function(){
	this.intervalid = 0;
	this.href = '';
	this.counter = 0;
	this.opacity = 0;
	this.fadeElement = document.getElementById('fadeelement');
	
	this.sqrs = new Array();
	this.sqrs.push(document.getElementById('lt'));
	this.sqrs.push(document.getElementById('ct'));
	this.sqrs.push(document.getElementById('cm'));
	this.sqrs.push(document.getElementById('lb'));
	
	this.textdiv = new Array();
	this.textdiv.push(document.getElementById('visiting_address'));
	this.textdiv.push(document.getElementById('visiting_hours'));
	this.textdiv.push(document.getElementById('middle_content'));
	this.textdiv.push(document.getElementById('right_content'));
	this.textdiv.push(document.getElementById('menu'));
}


Sqrs.prototype.setOpac = function() {
	if (this.counter % 4 == 3) {
		var ndx = (((this.counter+1)/4)-1);
		if (this.opacity != 1 && this.sqrs[ndx].getAttribute('opac')) {
			XbrowserOpac (this.sqrs[ndx], this.sqrs[ndx].getAttribute('opac'));
		}
		else {
			XbrowserOpac (this.sqrs[ndx], this.opacity);
		}
	}
	
	if (this.mode == 'out')
		XbrowserOpac (this.fadeElement, 0.25 + (this.counter * 0.05));
	if (this.mode == 'in')
		XbrowserOpac (this.fadeElement, 0.75 - (this.counter * 0.05));
	this.counter++;
}

Sqrs.prototype.transition = function (o,href) {
	this.mode = o;
	this.opacity = (o=='in')? '0.6':'1';
		
	if (this.counter >= (this.sqrs.length*4)) {
		window.clearInterval(this.intervalid);
		
		if (typeof this.callback == 'function') {
			this.callback();
		}
		
		var self = this;
		window.setTimeout(function () {
			if (href)  {
				location.href = href; 
			}
			self.switchTextDivs('in');
		}, 150);
	}
	
	else {
		if (this.counter == 0) {
			//this.shuffleSqrs();
			var self = this;
			this.intervalid = window.setInterval(function () {self.transition(self.mode,href)}, 50);	
		}
		this.setOpac();
	}
	
}

Sqrs.prototype.switchTextDivs = function (o) {
	var textdiv;
	var dspl = (o=='in')? 'block':'none';
	while (textdiv = this.textdiv.pop()) {
		textdiv.style.display = dspl;
	}
}

Sqrs.prototype.shuffleSqrs = function () {
	this.sqrs.shuffle();
}


Sqrs.prototype.setCallBack = function (callback) {
	this.callback = callback;
	
}

var DeOdessa = {};
DeOdessa.sqrs = new Sqrs();

function onloadTransition() {
	DeOdessa.sqrs.init();
	DeOdessa.sqrs.transition('in','');
}


function initializeAnchorTransitions() {
	var anchors = $$('.menu1, .menu2');
	var href = '';
	for (i=0; i<anchors.length; i++) {
		if (anchors[i].getAttribute('active')=='true') {
			anchors[i].removeAttribute ('href');
			XbrowserOpac(anchors[i], 1);
		}
		else
		{
			addEvent(anchors[i],'mouseover',function () {XbrowserOpac(this, 1);});
			addEvent(anchors[i],'mouseout',function () {XbrowserOpac(this, 0.7);});
				addEvent(anchors[i],'click',function () {
						var href = this.getAttribute('href');  
						this.removeAttribute ('href');
						var sqrs = new Sqrs();
						sqrs.init();
						sqrs.switchTextDivs('out');
						sqrs.transition('out',href);
				});
		}
	}
}

