// contains properties and methods of general character
// to be used on all of the site's pages
///////////////////////////////////////////////////////

var Page = new Class({
	
	// CONSTRUCTOR
	initialize: function() {
		Window.onDomReady(this.startPage);
		Window.onDomReady(this.setOpacities);
		Window.onDomReady(this.stretchColumns);
		//this.openWindow	= openWindow;
	},
	
	// METHODS
	startPage:	function() {
		this.navigation		= new Navigation;
	},
	
	setOpacities: function() {
		var opacityStart		= '0.0';
		var opacityEnd			= '0.6';
		var opacityDuration		= 1500;
		//var contentContainer486	= $('contentContainer486Background');		
		//var cols				= $$('.contentContainer154Background');
		//var colsWide			= $$('.contentContainer320Background');
		//var colsWideMiddle		= $$('.contentContainerMiddle320Background');
		//var colsWide652			= $$('.contentContainer652Background');
		var backgrounds			= $$('#contentContainer486Background','.contentContainer154Background','.contentContainer320Background','.contentContainerMiddle320Background','#contentContainer652Background');
		
		if(backgrounds) {
			for(var i=0;i<backgrounds.length;i++) {
				var node = backgrounds[i];
				var myFader	= new fx.Style(node, 'opacity', {duration: opacityDuration});
				myFader.custom(opacityStart, opacityEnd);
			}
		}
		
		/*if(contentContainer486) {
			//contentContainer486.setOpacity(opacity);
			var myFader	= new fx.Style(contentContainer486, 'opacity', {duration: opacityDuration});
			myFader.custom(opacityStart, opacityEnd);
		}
		
		if(cols.length > 0) {
			for(var i=0; i<cols.length; i++) {
				var node = cols[i];
				//node.setOpacity(opacity);
				var anotherFader = new fx.Style(node, 'opacity', {duration: opacityDuration});
				anotherFader.custom(opacityStart, opacityEnd);
			}
		}
		
		if(colsWide.length > 0) {
			for(var i=0; i<colsWide.length; i++) {
				var node = colsWide[i];
				//node.setOpacity(opacity);
				var anotherFader = new fx.Style(node, 'opacity', {duration: opacityDuration});
				anotherFader.custom(opacityStart, opacityEnd);
			}
		}
		
		if(colsWideMiddle.length > 0) {
			for(var i=0; i<colsWideMiddle.length; i++) {
				var node = colsWideMiddle[i];
				//node.setOpacity(opacity);
				var anotherFader = new fx.Style(node, 'opacity', {duration: opacityDuration});
				anotherFader.custom(opacityStart, opacityEnd);
			}
		}*/
	},
	
	stretchColumns: function() {
		var cols				= $$('.contentContainer154');
		var textCols			= $$('.contentText154');
		var mainCols			= $$('.contentText486');
		var imgHeight			= new Array(); // sum of all 90px high images
		var colOffset			= new Array();
		var minHeight			= 157; // used for mainCols only
		var smallImgHeight		= 90;
		var largeImgHeight		= 192;
		var colHeight			= 0; // calculated columns' height
		var hasImage			= new Array();
		var alertString			= "";
		
		if(cols.length > 0) {
			for(var i=0;i<cols.length;i++) {
				var node 			= cols[i];
				var nodeDimensions	= node.getPosition();
				var k					= 1;
				var l					= 1;
				
				for(var j=0;j<node.childNodes.length;j++) {
					childnode = node.childNodes[j];
					//alert(i+" "+childnode.tagName);
					if(childnode.tagName == 'IMG' && (childnode.src.indexOf('mood_') > -1 || childnode.src.indexOf('index.php') > -1)) {
						//alert("foo");
						if(childnode.height == smallImgHeight) {
							imgHeight[i] = k*smallImgHeight;
							if(k == 1) colOffset[i] = k*smallImgHeight+12;
							else colOffset[i] = k*smallImgHeight+(k-1)*12;
							k++;
							
						} else if(childnode.height == largeImgHeight) {
							imgHeight[i] = l*largeImgHeight;
							colOffset[i] = l*largeImgHeight;
							l++;
						}
						hasImage[i] = true;
					}
				}
				
				if(hasImage[i] != true) hasImage[i] = false;
				if(!colOffset[i]) colOffset[i] = 0;
				//alert(colOffset[i]);
				if(nodeDimensions.height > colHeight)
					colHeight = nodeDimensions.height;
			}
		}
		
		if(textCols.length > 0) {
			var otherCol = 0;
			for(var i=0;i<textCols.length;i++) {
				otherCol = (i==0) ? 1 : 0;
				var node		= textCols[i];
				var nodeHeight	= 0;
				if(node.style.height < colHeight) {
					nodeHeight = (colHeight-colOffset[i]-24); // don't know where that 24 comes from, but it's working
					/*if(!hasImage[otherCol]) {
						nodeHeight -= imgHeight[0];
					}*/
					nodeHeight = nodeHeight+"px";
					node.style.height = nodeHeight;
				}
			}
		}
		
		if(mainCols.length > 0) {
			for(var i=0;i<mainCols.length;i++) {
				var node = mainCols[i];
				var nodeDimensions	= node.getPosition();
				if(nodeDimensions.height < minHeight)
					node.style.height = minHeight+"px";
			}
		}
	},
	
	openWindow: function(uriParam, name, windowWidth, windowHeight) {
		var uriPrefix;
		var screenWidth;
		var screenHeight;
		var positionX;
		var positionY;
		
		uriPrefix		= "";
		uri				= uriPrefix+uriParam;
		
		screenWidth		= screen.width;
		screenHeight	= screen.height;
		
		positionX		= Math.round(((screenWidth-windowWidth)/2), 0);
		positionY		= Math.round(((screenHeight-windowHeight)/2), 0);
		
		optional	= "width="+windowWidth+",";
		optional	+= "height="+windowHeight+",";
		optional	+= "top="+positionY+",";
		optional	+= "left="+positionX+",";
		optional	+= "location=no,";
		optional	+= "menubar=no,";
		optional	+= "resizable=no,";
		optional 	+= "scrollbars=no,";
		optional	+= "status=no,";
		optional	+= "toolbar=no";
		
		window.open(uri, name, optional);
	}
	
	
});

var page = new Page();