
	$(document).ready( function() {
		
		/* about page */
		$('#about-page .box-c1').click( function() {
			$('.hidden-content').hide();
			aboutPage.click(this);
		} );

		$('#about-page .box-c2').click( function() {
			$('.text-area').hide();
			aboutPage.click(this);
		} );
		
		$('#about-page #box-about').click();
		
		/* media page */
		$('#media-page .box').each(function(i,a){
			if( a.href ) {
				$(a).mouseover( mediaPage.mouseover.bind(this,a) );
				$(a).mouseout( mediaPage.mouseout.bind(this,a) );
			}
		});

		$('.media-info-box').mousemove( mediaPage.mouseout );


		/* single project page */
		$('#single-project-page .box-c1').click( function(){
			$('.hidden-content').hide();
			singleProjectPage.click(this);
		});

		$('#single-project-page .box-c2').click( function(){
			$('.hidden-content').hide();
			singleProjectPage.click(this);
		});

		$('#single-project-page #box-pic1').click();
	});
	
	var mediaPage = {
		timeout: 0,
		isOver: 0,
		positionCache: {},
		
		checkPosition: function( r, c, infor, infoc,isHorizontal )
		{
			// horizontal info box
			if( isHorizontal ) {
				if( infor>r ) {
					return true;
				} else if( infor<(r-2) ) {
					return true;
				} else if( infoc > c ) {
					return true;
				} else if( infoc<(c-2) ) {
					return true;
				}
			// vertical info box
			} else {
				if( infor>r ) {
					return true;
				} else if( infor<(r-4) ) {
					return true;
				} else if( infoc > c ) {
					return true;
				} else if( infoc<(c-1) ) {
					return true;
				}
			}
			return false;
		},
		
		mouseover: function(a)
		{
			clearTimeout( mediaPage.timeout );
			mediaPage.timeout = setTimeout( mediaPage.real_mouseover.bind(this,a), 250 );
		},
		
		real_mouseover: function(a)
		{
			if( !a ) return;
			$('.media-info-box').hide();

			// br2c2
			var m = a.id.match(/br(\d)c(\d)/);
			if( m ) {
				var infor, r = parseInt(m[1],10);
				var infoc, c = parseInt(m[2],10);
				
				var count = 0;
				var isHorizontal = $('#info-box-br'+r+'c'+c).css('width') >  $('#info-box-br'+r+'c'+c).css('height') ? 1 : 0;
				
				if( ! mediaPage.positionCache[ a.id ] ) {
					do {
						if( isHorizontal ) {
							infor = rand(4);
							infoc = rand(5);
						} else {
							infor = rand(3);
							infoc = rand(6);
						}
						
						if( count++>1000 ) throw 'Too much';

					} while( ! mediaPage.checkPosition( r,c,infor,infoc,isHorizontal ) );
					
					mediaPage.positionCache[ a.id ] = 'box-r'+infor+' box-c'+infoc;
				}
				
				$('#info-box-' + a.id).attr('className','');
				$('#info-box-' + a.id).addClass('media-info-box ' + mediaPage.positionCache[ a.id ] );
				$('.media-info-box').hide();
				$('#info-box-' + a.id).show();
				$('#info-box-' + a.id).css('opacity',.2);
				$('#info-box-' + a.id).animate({
					opacity: .9
				}, 200 );
			}
		},
		
		mouseout: function(a)
		{
			clearTimeout( mediaPage.timeout );
			$('.media-info-box').stop();
			$('.media-info-box').hide();
		}
	};
	
	function rand ( n )
	{
		return ( Math.floor ( Math.random ( ) * n + 1 ) );
	}


	var aboutPage = {
		click: function(obj) 
		{
			$('.'+$(obj).attr('id')+'-content').show();

			var m = null;
			$( $('#content').attr('class').split(/ /) ).each(function(i,className){
				if( !m && className.match(/-background/) ) {
					m = className;
				}
			});
			$('#content').removeClass(m);
			$('#content').addClass($(obj).attr('id')+'-background');
		}
	};
	//var mediaPage = aboutPage;
	//var contactPage = aboutPage;
	var singleProjectPage = aboutPage;
	var singleMediaPage = aboutPage;
	
	dbg = function(a){console.log(a);}

