
var loader_gif = assets_url +'images/loader.gif';

/*
 * show loader
 */

$.fn.showLoader = function(loader_uri, clear_content, container_height) {
	
	return this.each(function ( ) {
				var h = container_height || $(this).height( );
				clear_content = clear_content || false;
				if( clear_content )
				{
					$(this).html( ' ' );
				}
				$(this).css( "background-image", "url( '"+  loader_uri +"' )" );
				$(this).css( "background-repeat", "no-repeat" );
				$(this).css( "background-position", "center" );
				
				$(this).height( h );
    		});
};

/*
 * hide loader
 */
$.fn.hideLoader = function() {
	
	return this.each(function ( ) {
			$(this).css( 'background-image', '' );
			$(this).css( 'height', 'auto' );
    	});
};

$(document).ready( function( ){
	
	$(".regular").focus(function(){
		$(this).addClass( "active" );
	});

	$(".regular").blur(function(){
		$(this).removeClass( "active" );
	});
	
	$(".on").parents( 'li' ).addClass( 'on' );
	
	$("#menu > ul > li").hover(function(){
		$(this).addClass( 'active' );
	}, function(){
		$(this).removeClass( 'active' );
	});
	
	$("#menu ul li").hover(function(){
		$(this).children( "ul" ).show(  );
	}, function(){
		$(this).children( "ul" ).hide( );
	});
	
	$("#searchbar .input").click(function(){
		this.value = '';
	});

	//projects list
	if( $(".projects .list a.active").length <= 0 )
	{
		$(".projects .list a:first").addClass( 'active' );
	}
	//--------------------
	//load and display project in a so-so pretty way
	//--------------------
	$(".projects .list a").click(function(){

		$(".projects .list a.active").removeClass( 'active' );		
		$(this).addClass( 'active' );
		
		var url = $(this).attr( 'href' );
		
		var preloader = document.createElement( 'div' );
		$(preloader).css( "display", "none" );
		$(preloader).addClass( "project" );
		$("body").append( $(preloader) );

		$("#project-area").showLoader( assets_url +'images/loader.gif', true );
		
		$(preloader).load( url, {type : "get"},function( ){
//			console.log( 'preloader height: ' + $(preloader).height( ) );
			$("#big img").load(function(){
					$("#project-area").css( {opacity: 0 } );
					$("#project-area").html( $(preloader).html( ) );
//					console.log( 'preloader height: ' + $(preloader).height( ) );
					$("#project-area").animate( {opacity: 1, height: $(preloader).height( ) }, function(){ $("#project-area").hideLoader( ); } );
					$(preloader).remove( );
					startup( );
			})					
		})
		return false;
	});
	//--------------------
	//slideshow
	//--------------------
//		var counter = 0;
//		var  slideshow = setInterval( function(){
//			console.log( counter );
//			counter = ( counter == $(".projects .list a").length-1 ? 0 : counter+1 );
//			$( $(".projects .list a").get( counter ) ).click( );
//		}, 10000 );
	//--------------------
	
	
	startup( );
});

function startup( )
{
	//activate project thumbs
	$("#project-area .thumbs a").click(function(){
		var href = $(this).attr( 'href' );
//		$("#big").loader( assets_url +'images/loader.gif' );
//		$("#big").prev( "div" ).height( $("#big").height( ) );
		
		$("#big").showLoader( loader_gif );
		$("#big img").fadeOut( "fast", function(){ 
			
		})
//		$("#big").attr( 'src', href );
		$("#big img" ).attr( 'src', href ).load(function(){
			$("#big").hideLoader( );
			$(this).fadeIn( "slow" );
		});
		return false;
	});

/*	
	//activate project thumbs
	$("#project-area .thumbs a").click(function(){
		var href = $(this).attr( 'href' );
		

		var thumb_preloader = new Image;
		$(thumb_preloader).css( "display", "none" );
		$("body").append( $(thumb_preloader) );

		var perm_width = $("#big").width( );
		$("#big").animate( {opacity: 0}, function(){ 
				$(thumb_preloader).attr( 'src', href );
				$(thumb_preloader).load(function(){
					$(thumb_preloader).attr( 'width', perm_width );
					$("#big").attr( 'src', href );
					$("#big").attr( 'src', href ).animate( { opacity: 1, height: $(thumb_preloader).height( ) }, "slow" );
					$(this).remove( );
				});
				
		});
		return false;
	});
	*/
}
