var general = {
	sections: {
		init: function() {
			if(!document.all) {
				$('#section1image').show();
				$('#section2image').show();
				$('#section3image').show();
			}

			$('.section-navi').css('height','192px');			
			$('.section h3').hide();
			$('.section .content').css({
				'clear':'none',
				'paddingTop':'22px',
				'paddingLeft':'0px'});

			$('.colortitle').hide().fadeIn(1000);		
			$('.colorbg').hide().fadeIn(2000);
		
			if(!document.all) {
				this.fadeColsIn(this.collectCols('section0', 40, true, false), 1000);
				
				window.setTimeout(function() {
					general.sections.fadeColsIn(general.sections.collectCols('section4', 40, false, true), 1000);
				}, 5000);
			}
		},
		
		fadeColsIn: function(cols, speed) {
			for(a=0; a<cols.length; a++) {			
				var col = cols[a][0];
				var opa = cols[a][1];
			
				col.animate({
					'opacity':opa
					}, (a+1)*speed);
					
				col.removeClass('free');
				col.removeClass('taken');
			}
		},
		
		fadeColsOut: function(cols, speed) {
			for(a=0; a<cols.length; a++) {			
				var col = cols[a][0];
			
				col.animate({
					'opacity':1
					}, (a+1)*speed);
					
				col.removeClass('taken');
				col.removeClass('free');
			}
		},
		
		collectCols: function(section, perc, left, right) {
			var sectionCols = new Array();
			
			$('.row.'+section).each(function() {
				var rowCols = $('.col', this);
				var rowColsCount = Math.ceil(rowCols.length/(100/perc));

				for(a=0; a<rowColsCount; a++) {
					var rndOpa = (Math.random()*30)/100;
					
					// two step gradient fade
					var opa = (a==(rowColsCount-1))?rndOpa+0.6:rndOpa;
					var opa = (a==(rowColsCount-2))?rndOpa+0.3:opa;
								
					if(left) {					
						sectionCols.push([$(rowCols[a]), opa]);
					}
					
					if(right) {
						sectionCols.push([$(rowCols[rowCols.length-a]), opa]);
					}
				}
			});

			return sectionCols;
		},
		
		open: function(obj, fast) {
			$('h3.content-entry-title', $(obj).parents('.section')).each(function() {
				if($.trim($(this).html())==$.trim($(obj).html())) {
					var sectionid = $(obj).parents('.section')[0].id;
				
					$('.content-entry .content-links', $(this).parents('.section')).hide();
					$('.content-entry', $(this).parents('.section')).hide();
					
					if(fast) {
						$(this).parents('.content-entry').show();
						$('.content-links', $(this).parents('.content-entry')).show();
					}
					else {
						$(this).parents('.content-entry').fadeIn(500);					
						$('.content-links', $(this).parents('.content-entry')).fadeIn(2500);
					}
					
					$('li', $(obj).parents('ul')).removeClass('on');

					$(obj).parents('li').addClass('on');
					
					if(!document.all) {
					
						// manage content image
						var image = $(".content-image", $(this).parents('.content-entry'));
						
						if(image.length>0) {						
							$('#'+sectionid+'image').empty();
	
							image.clone().appendTo('#'+sectionid+'image');
	
							var cols = general.sections.collectCols(sectionid, 30, false, true);
							
							for(col in cols) {
								cols[col][0].css('opacity', 1);
							}
		
							general.sections.fadeColsIn(cols, 100);
						}
						else {
							if(!fast) {
								general.sections.fadeColsOut(general.sections.collectCols(sectionid, 30, false, true), 100);
							}
						}
					}
				}
			});
		}
	},
	
	content: {
		init: function() {
			$('.content-entry').each(function() {
				$('h3.content-entry-title', this).each(function() {
					$('.section-navi ul', $(this).parents('.section')).append("<li><a href=\"javascript://\" onclick=\"general.sections.open(this);\">"+$(this).html()+"</a></li>");
				});
				
				$(this).hide();
			});
			
			$('.section').each(function() {
				general.sections.open($('li a', $(this))[0], true);
			});
		}
	},
	
	background: {
		init: function() {
			$('#background').show();
			
			if(!document.all) {
				$('#headerbackground')
					.css('opacity',0.8)
					.show();
				$('#footerbackground')
					.css('opacity',0.8)
					.show();
			}
			
			var ua = navigator.userAgent.toLowerCase();
			
			// firefox fix
			if(ua.indexOf('gecko')!=-1 && ua.indexOf('safari')==-1) {
				$('#background .col').css('marginRight','-1px');
			}
		
			setInterval("general.background.flash();", 500 );
		},
		
		flash: function() {
			var divs = $('#background div.col.free');
			var rnd_1 = Math.ceil(Math.random()*divs.length)-1;
			var rnd_2 = Math.ceil(Math.random()*divs.length)-1;
			
			var newcolor = $(divs[rnd_2]).css('backgroundColor');
			
			$(divs[rnd_1]).animate({
				backgroundColor:'#efefef'
				}, 500);
			
			$(divs[rnd_1]).animate({
				backgroundColor:newcolor
				}, 1000);
		}
	}
}

$(document).ready(function(){    
	general.sections.init();
	general.content.init();
	general.background.init();
});