
LOCAL = {
	randomXToY: function(minVal,maxVal,floatVal) {
		var randVal = minVal+(Math.random()*(maxVal-minVal));
		return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
	},
	layout: {
		init: function() {
			//console.log('init');
			jQuery('.referenceBox').hover(function(){
				jQuery('.data', this).stop().animate({top:'0px'}, 'fast');
			}, function(){
				jQuery('.data', this).stop().animate({top:'250px'}, 'fast');
			});
			
            jQuery('ul.refHighlightList').innerfade({
                speed: 1000,
                timeout: 5000,
                type: 'sequence',
                containerheight: jQuery(this).parent().height()+'px'
            });
			
			LOCAL.backgroundSquares.init();
			LOCAL.refbox.init();
			jQuery(window).resize(function(){
				LOCAL.backgroundSquares.windowResize();
			});
		}
	},
	refbox: {
		init: function() {
			jQuery('.referenceBox').click(function(){
				var currLinkElem = jQuery('a', this);
				var linkTarget = currLinkElem.attr('href');
				var refbox = currLinkElem.attr('data-refbox');
				
				if( refbox ) {
					LOCAL.refbox.open({
						target: linkTarget,
						width: currLinkElem.attr('data-refbox-width'),
						height: currLinkElem.attr('data-refbox-height')
					});
				}
				else {
					window.open(linkTarget);
				}
				return false;
			});
		},
		open: function(conf) {
			var refboxTarget = conf.target;
			var refboxWidth = conf.width;
			var refboxHeight = conf.height;
			var refboxSquareSize = 250;
			var refboxPosTop = jQuery(window).scrollTop()+125;
			
			var squareCountInCol = Math.ceil(refboxHeight/refboxSquareSize);
			var squareCountInRow = Math.ceil(refboxWidth/refboxSquareSize);
			var startPointTop = 0;
			var startPointLeft = 0;
			var sumSquares = squareCountInRow * squareCountInCol;
			
			var refboxElem = jQuery('<div id="refboxContainer">');
			refboxElem.css({
				position: 'absolute',
				width: refboxWidth +'px',
				height: refboxHeight +'px',
				top: refboxPosTop+'px',
				left: '50%',
				marginLeft: (refboxWidth/2)*-1 +'px',
				zIndex: 90
			});
			
			var refboxCloseElem = jQuery('<div id="refboxClose">');
			refboxCloseElem.css({
				position: 'absolute',
				width: '50px',
				height: '50px',
				top: '0px',
				right: '0px',
				background: 'url(img/layout/sprite.png) no-repeat -751px -501px',
				zIndex: 90,
				opacity: 0,
				cursor: 'pointer'
			});
			refboxCloseElem.click(function(){
				LOCAL.refbox.close();
			});
			refboxCloseElem.appendTo(refboxElem);
			
			
			var refboxSquareElem = jQuery('<div class="refboxSquare">');
			refboxSquareElem.css({
				width: refboxSquareSize+'px',
				height: refboxSquareSize+'px',
				position: 'absolute',
				opacity: 0,
				backgroundRepeat: 'no-repeat'
			});
			
			var refboxSquareInnerElem = jQuery('<div class="refboxSquareInner">');
			refboxSquareInnerElem.css({
				width: refboxSquareSize+'px',
				height: refboxSquareSize+'px'
			});
			
			for( var i=0; i<squareCountInCol; i++ ) {
				for( var j=0; j<squareCountInRow; j++ ) {
					var currentStartPointTop = startPointTop + (refboxSquareSize*i);
					var currentStartPointLeft = startPointLeft + (refboxSquareSize*j);
					var currentSquareElem = refboxSquareElem.clone();
					currentSquareElem.css({
						top: currentStartPointTop+'px',
						left: currentStartPointLeft+'px',
						backgroundImage: 'url('+ refboxTarget +')',
						backgroundPosition: (currentStartPointLeft*-1)+'px '+ (currentStartPointTop*-1)+'px'
					});
					refboxSquareInnerElem.clone().css('background-color', '#ffffff').appendTo(currentSquareElem);
					currentSquareElem.appendTo(refboxElem);
				}
			}
			refboxElem.appendTo('#containerSite');
			refboxDimElem = jQuery('<div id="refboxDim">');
			refboxDimElem.css({
				position: 'absolute',
				width: '100%',
				height: '100%',
				top: '0px',
				left: '0px',
				backgroundColor: 'rgb(0,0,0)',
				zIndex: 80,
				opacity: 0
			});
			refboxDimElem.click(function(){
				LOCAL.refbox.close();
			});
			refboxDimElem.appendTo('#containerSite');
			jQuery('#refboxDim').animate({opacity: 0.8}, 'fast', function(){
				var refboxSquareElements = jQuery('.refboxSquare');
				LOCAL.refbox.animateShow(refboxSquareElements, 0);
			});
		},
		animateShow: function(refboxSquareElements, index) {
			var speed = 100;
			var fadeOutMultiply = 5;
			
			var index = index||0;
			if(!refboxSquareElements[index]) {
				jQuery('#refboxClose').animate({opacity: 1}, 'fast');
				return false;
			}
			var currElem = jQuery(refboxSquareElements[index]);
			//var colorIndex = index%colorArray.length;
			
			if( currElem ) {
				currElem.css({
					opacity: 0
				});
				//jQuery('.refboxSquareInner', currElem).css('background-color', '#'+colorArray[colorIndex]);
				
				currElem.animate({opacity: 1}, speed, function(){
					jQuery('.refboxSquareInner', this).animate({opacity:0}, speed*fadeOutMultiply);
					LOCAL.refbox.animateShow( refboxSquareElements, (index+1));
				});
			}
		},
		close: function() {
			var refboxSquareElements = jQuery('.refboxSquare');
			jQuery('#refboxClose').animate({opacity: 0}, 'fast');
			LOCAL.refbox.animateClose(refboxSquareElements, (refboxSquareElements.length-1));
		},
		animateClose: function(refboxSquareElements, index) {
			var speed = 100;
			var fadeOutMultiply = 5;
			
			var index = index||refboxSquareElements.length;
			if(!refboxSquareElements[index]) {
				jQuery('#refboxDim').animate({opacity: 0}, 'fast', function(){
					jQuery('#refboxContainer').remove();
					jQuery('#refboxDim').remove();
				});
				return false;
			}
			var currElem = jQuery(refboxSquareElements[index]);
			//var colorIndex = index%colorArray.length;
			
			if( currElem ) {
				jQuery('.refboxSquareInner', currElem).animate({opacity: 1}, speed, function(){
					currElem.animate({opacity:0}, speed*fadeOutMultiply);
					LOCAL.refbox.animateClose( refboxSquareElements, (index-1));
				});
			}
		}
	},
	backgroundSquares: {
		colorArray: ['5f6aab', '2382c0', '0093d2', '009ee0', '00a5c4', '0aaba4', '46b080', '76b856', '97bf0d', '79b51c', '54ab26', '27a22d', 'ffed00', 'ffdd00', 'fecc00', 'fbba00', 'f7a800', 'f29400', 'ee813c', 'eb6b59', 'e8516b', 'e52f75', 'e2007a', 'd3007b', 'c5007b', 'b5007b', 'a5037c', '93117e', '7e4a94'],
		pageWidth: 0,
		pageHeight: 0,
		squareSize: false,
		squareElements: false,
		squareCountInCol: 0,
		startPointTop: 0,
		squareCountInRowHalf: 0,
		startPointLeft: 0,
		sumSquares: 0,
		
		
		init: function() {
			
			if( jQuery.browser.msie ) { return false; }
			
			this.pageWidth = jQuery(document).width();
			this.pageHeight = jQuery(document).height();
			
			jQuery('<div class="square">').appendTo('body');
			
			this.squareSize = jQuery('.square').outerWidth();
			jQuery('#containerSite').css({width:this.pageWidth+'px', height:this.pageHeight+'px'});
			
			var squareElem = jQuery('.square');
			this.squareCountInCol = Math.ceil(this.pageHeight/this.squareSize)+1;
			this.startPointTop = (this.squareSize/2)*-1;
			
			this.squareCountInRowHalf = Math.ceil((this.pageWidth/2)/this.squareSize);
			this.squareCountInRow = this.squareCountInRowHalf*2;
			this.startPointLeft = Math.ceil((this.pageWidth/2)-(this.squareSize*this.squareCountInRowHalf));
			
			this.sumSquares = this.squareCountInRow * this.squareCountInCol;
			
			for( var i=0; i<this.squareCountInCol; i++ ) {
				for( var j=0; j<this.squareCountInRow; j++ ) {
					var currentStartPointTop = this.startPointTop + (this.squareSize*i);
					var currentStartPointLeft = this.startPointLeft + (this.squareSize*j);
					squareElem.clone().appendTo('#containerSite').css({ top: currentStartPointTop+'px', left: currentStartPointLeft+'px' });
				}
			}
			squareElem.remove();
			
			this.squareElements = jQuery('.square');
			
			this.squareElements.hover(function(){
				currElem = jQuery(this);
				currElem.css({
					opacity: 0,
					backgroundColor: '#'+LOCAL.backgroundSquares.colorArray[LOCAL.randomXToY(0,23)]
				});
				
				currElem.stop().animate({opacity:0.3}, 100, function(){
					jQuery(this).animate({opacity: 0}, 400);
				});
			}, function(){});
			
			
			jQuery('#content').mousemove(function(e){
			
			    var currRow = Math.ceil((e.pageY-125)/LOCAL.backgroundSquares.squareSize);
			    var currCol = Math.ceil((e.pageX-LOCAL.backgroundSquares.startPointLeft)/LOCAL.backgroundSquares.squareSize);
			    var currElemIndex = (currRow * LOCAL.backgroundSquares.squareCountInRow) + currCol - 1;
			    var currElem = jQuery(LOCAL.backgroundSquares.squareElements[currElemIndex ]);
			
			    if( currElem && !currElem.is(':animated') ) {
					currElem.trigger('mouseenter');
			    }
			});
			
		},
		windowResize: function() {
			if( jQuery.browser.msie ) { return false; }
			jQuery('.square').remove();
			jQuery('#containerSite').css({width:'auto', height:'auto'});
			LOCAL.backgroundSquares.init();
		}
	}
};





/*
// singel square animation
// square moves down the page
function animateSingleSquares(index) {
	var speed = 100;
	var fadeOutMultiply = 5;
	
	var index = index||0;
	if(!squareElements[index]) { index = 0; }
	var currElem = jQuery(squareElements[index]);
	var colorIndex = index%colorArray.length;
	
	if( currElem ) {
		currElem.css({
			opacity: 0,
			backgroundColor: '#'+colorArray[colorIndex]
		});
		
		currElem.animate({opacity: 0.3}, speed, function(){
			animateSingleSquares( (index+1), function(){});
			jQuery(this).animate({opacity: 0}, speed*fadeOutMultiply);
		});
	}
}
//animateSingleSquares();


// row animation
// complete row moves down the page
function animateRows(row) {
	var speed = 100;
	var fadeOutMultiply = 3;
	var rowStartElement = 0;
	
	var row = row||0;
	if( row>=squareCountInCol ) { row = 0; }
	
	var rowStartElement = squareCountInRow*row;
	var rowEndElement = rowStartElement+squareCountInRow;
	var currElements = jQuery('.square').slice(rowStartElement, rowEndElement);
	var colorIndex = row%colorArray.length;
	
	currElements.css({
		opacity: 0,
		backgroundColor: '#'+colorArray[colorIndex]
	});
	var elemCounter = 0;
	currElements.animate({opacity: 0.3}, speed, function(){
		if( elemCounter==(squareCountInRow-1) ) {
			animateRows( (row+1) );
			currElements.animate({opacity: 0}, speed*fadeOutMultiply);
		}
		elemCounter++
	});
}
//animateRows();


// noise animation
// animate random squares
var noiseArray = [];
for( var i=0; i<20; i++ ) {
	randElemCount = Math.ceil(sumSquares/5);
	var randElemArr = [];
	for( var j=0; j<randElemCount; j++ ) {
		randElemArr.push(randomXToY(0,(sumSquares-1)));
	}
	noiseArray.push(randElemArr);
}
//console.log(noiseArray);


function animateNoise(cycle) {
	var speed = 200;
	var fadeOutMultiply = 3;
	var cycle = cycle||0;
	if( cycle > 9 ) { cycle = 0; }
	var currElemIndexArray = noiseArray[cycle];
	var currElements = [];
	for(var i in currElemIndexArray) {
		currElements.push( jQuery('.square')[currElemIndexArray[i]] );
	}
	
	jQuery(currElements).each(function(){
		jQuery(this).css({
			opacity: 0,
			backgroundColor: '#'+colorArray[(jQuery(this).index()%colorArray.length)]
		});
	});
	var elemCounter = 0;
	jQuery(currElements).animate({opacity: 0.15}, speed, function(){
			jQuery(this).animate({ opacity: 0 }, speed * fadeOutMultiply);
			//console.log(jQuery(this).index()%colorArray.length);
		if (elemCounter == (currElemIndexArray.length - 1)) {
			animateNoise( (cycle+1) );
		}
		elemCounter++
	});
}
//animateNoise();
*/








