/** projects browse **/
function start_slider(){
	var _baseurl = $( 'base' ).attr( 'href' );
	// preload hover images
	$( '#slide_container' ).find( 'img' ).each( function( i , e ) {
		$( e ).parent().append( '<img src="#" class="preload_img" style="display:none" />' );
		var _imgHover = $( e ).parent().find( '.preload_img' );
		var _src = _baseurl + 'public/cover/' + $( e ).parent().attr( 'name' );
		var objImagePreloader = new Image();
	       objImagePreloader.onload = function() {
	           _imgHover.attr('src' , _src );
	           //	clear onLoad, IE behaves irratically with animated gifs otherwise
	           objImagePreloader.onload=function(){};
	       };
	       objImagePreloader.src = _src;
		_imgHover.attr('src' , _src );
		// $( '#preload_img' ).remove();
	});
	// slide logic
	var _margin = 0;
	var _gutter = 0;
	$( '#slide_container' ).find( '.thumb_container' ).each( function( i , el ) {
		_margin += parseInt( $( el ).width() );
		_gutter = i;
	});
	var _totWidth = _margin + ( ( _gutter * 16 ) );
	$( '#slide_container' ).width( _totWidth );
	$( '#slide_container' ).find( '.thumb_container:first' ).addClass( 'alpha' );
	$( '#slide_container' ).find( '.thumb_container:last' ).addClass( 'omega' );
	if( _totWidth > 824 ){
		if( $( '#arrow_right' ).hasClass( 'inactive' ) ){
			$( '#arrow_right' ).toggleClass( 'inactive' );
			$( '#arrow_right' ).css( 'cursor' , 'pointer' );
			$( '#arrow_right' ).click( function() {
				var _marginLeft = parseInt( $( '#slide_container' ).css( 'margin-left' ).replace( 'px' , '' ) );
				if( _marginLeft < 0 )
					_marginLeft = _marginLeft * -1;
				_marginLeft += 824;
				var _margin = 0;
				var _outEach = false;
				var _intCount = 0;
				$( '#slide_container' ).find( '.thumb_container' ).each( function( i , el ) {
					_margin += parseInt( $( el ).width() );
					if( !_outEach ){
						_intCount = i;
						if( _marginLeft < _margin ) {
							_marginLeft = _margin;
							_outEach = true;
						}
					}
				});
				_intCount = _intCount * 16;
				_marginLeft = ( ( _marginLeft + _intCount ) * -1 ) + 824;
				if ( _marginLeft < _margin ) {
					if(( _marginLeft * -1 )+824 <= _totWidth ) {
						$( '#slide_container' ).stop().animate( { marginLeft: _marginLeft + 'px'} , 1000 , function() {
							$( '#arrow_left' ).removeClass( 'inactive' );
							$( '#arrow_left' ).css( 'cursor' , 'pointer' );
							if(( _marginLeft * -1 )+824 == _totWidth ) {
								$( '#arrow_right' ).addClass( 'inactive' );
								$( '#arrow_right' ).css( 'cursor' , 'default' );
							}
						});
					}
				}
			});   
		}
		$( '#arrow_left' ).click( function() {
			var _marginLeft = parseInt( $( '#slide_container' ).css( 'margin-left' ).replace( 'px' , '' ) );
			if( _marginLeft < 0 )
				_marginLeft = _marginLeft * -1;
			var _margin = 0;
			var _outEach = false;
			var _intCount = 0;
			$( '#slide_container' ).find( '.thumb_container' ).each( function( i , el ) {
				_margin += parseInt( $( el ).width() )+16;
				if( !_outEach ){
					if( _marginLeft <= _margin ) {
						_intCount = i;
						_marginLeft = _margin-( parseInt( $( el ).width() )+16 );
						_outEach = true;
					}
				}
			});
			_marginLeft = ( _marginLeft  ) * -1;
			if( _marginLeft > 0 )
				_marginLeft = 0;
			if ( _marginLeft <= 0 ) {
				$( '#slide_container' ).stop().animate( { marginLeft: _marginLeft + 'px'} , 1000 , function() {
					$( '#arrow_right' ).removeClass( 'inactive' );
					$( '#arrow_right' ).css( 'cursor' , 'pointer' );
					if( _marginLeft == 0 ) {
						$( '#arrow_left' ).addClass( 'inactive' );
						$( '#arrow_left' ).css( 'cursor' , 'default' );
					}
				});
			}
		});
	}
	// swap images & show titles
	$( '.thumb' ).hover(
		function(){
			$( this ).find( '.infos' ).show();
			$( this ).find( '.overlay' ).show();
		
			var original_src = $( this ).find( 'img:first' ).attr( 'src' );
			var color_src = $( this ).find( 'img:last' ).attr( 'src' );
			$( this ).find( 'img:first' ).attr( 'src' , color_src );
			$( this ).find( 'img:first' ).attr( 'original_src' , original_src );
		},
		function(){
			$( this ).find( '.infos' ).hide();
			$( this ).find( '.overlay' ).hide();

			var original_src = $( this ).find( 'img:first' ).attr( 'original_src' ); 
			$( this ).find( 'img:first' ).attr( 'src' , original_src );
		}
	);
}
/** project view  :: all sections **/
// browser scroll smoothly
function scrollWinSmooth( target_el )
{
	var target_h = $( target_el ).offset().top;
	if ( target_el == '#thumb_large' )
	{
		target_h = target_h - ($( window ).height()/2) + 120;
	}
	$('html, body').animate({
			scrollTop: target_h
		}, 1000);
}
// browser scroll
function scrollWin()
{
	$('html, body').scrollTop( $( '.project' ).offset().top );
}
// sets a fixed height to absolute_container
var header_h = 288;
var pro_header_h = 118;
function resizeContainer()
{
	var win_w = $( window ).width();
	var win_h = $( window ).height();
	$( ".absolute_container" ).css( 'height' , win_h - pro_header_h + 'px' );
	$( "#slider > ul > li" ).each( function(){
		$( this ).css( 'height' , win_h - pro_header_h ).css( 'width' , win_w + 'px' );
	});
}
// sets a min-height to create body scroll
function resizeBody()
{
	var win_w = $( window ).width();
	var win_h = $( window ).height();
	if ( $( 'body' ).height() < ( win_h + header_h - pro_header_h ) )
		$( 'body' ).css( 'height' , win_h + header_h - pro_header_h + 'px' );	
}
// resizes the given img object
function resizeImg( el )
{
	var win_w = $(window).width();
	var win_h = $(window).height();
	var field_w = win_w;
//	var field_h = win_h - header_h + pro_header_h;
	var field_h = win_h - pro_header_h;
	if( el ) {
		// resize container (parent li)
		el.parent().css( 'height' , win_h - pro_header_h  + 'px').css( 'width' , win_w  + 'px');
		// get image size
		var image_w = $( el ).width();
		var image_h = $( el ).height();
		var ratio;
		if (image_w/image_h >= field_w/field_h)
			ratio = field_h/image_h;
		else
			ratio = field_w/image_w;
		var final_w = image_w*ratio;
		var final_h = image_h*ratio;
		$( el ).css('width', final_w + 'px');
		$( el ).css('height', final_h + 'px');
		if ( final_w > field_w )
			$( el ).css('margin-left', '-'+((final_w-field_w)/2)+'px');
		else
			$( el ).css('margin-left', '0px');
		if ( final_h > field_h )
			$( el ).css('margin-top', '-'+((final_h-field_h)/2)+'px');
		else
			$( el ).css('margin-top', '0px');
	}
}
// resizes the home img object
function resizeHomeImg( el )
{
	var win_w = $(window).width();
	var win_h = $(window).height();
	var field_w = win_w;
	var field_h = win_h;
	if( el ) {
		// resize container (parent li)
		el.parent().css( 'height' , field_h + 'px' ).css( 'width' , field_w + 'px' );
		// get image size
		var image_w = $( el ).width();
		var image_h = $( el ).height();
		var ratio;
		if (image_w/image_h >= field_w/field_h)
			ratio = field_h/image_h;
		else
			ratio = field_w/image_w;
		var final_w = image_w*ratio;
		var final_h = image_h*ratio;
		$( el ).css('width', final_w + 'px');
		$( el ).css('height', final_h + 'px');
		if ( final_w > field_w)
			$( el ).css('margin-left', '-'+((final_w-field_w)/2)+'px');
		else
			$( el ).css('margin-left', '0px');
		if ( final_h > field_h)
			$( el ).css('margin-top', '-'+((final_h-field_h)/2)+'px');
		else
			$( el ).css('margin-top', '0px');
	}
}
// pre-load images from intro section
function loadIntroImgs( images )
{
	$( '.loading' ).show();
	var win_w = $( window ).width();
	var win_h = $( window ).height();
	// images length
	var max = $(images).length;
	// at least 1 image exist
	if(max>0)
	{
		// create the UL element
		var ul = $('<ul></ul>');
		// append to div#wrapper
		$(ul).appendTo($('#slider'));
		// load the first image
		LoadImage(0,max);
	}

	// function of loading image
	// params: (int) index of image in array, (int) length of images array
	function LoadImage(index,max)
	{
		// if current index is lower then max element (max-1)
		if(index<max)
		{
			// create the LI, add loading class
			var list = $('<li id="img_'+index+'"></li>');
			// append to UL
			$('#slider ul').append(list);
			// current LI
			var curr = $("#slider li#img_"+index);
			// new image object
			var img = new Image();
			// image onload
			$(img).load(function () {
				$( this ).css('display','none'); // since .hide() failed in safari
				$( curr ).append(this);
				resizeImg( $( this ) );
				$( this ).fadeIn('slow',function(){
					// once the current loaded, trigger the next image
					LoadImage(index+1,max);
				});
				if ( index == max-1 )
					start_cycle_intro();
			}).error(function () {
				// on error remove current
				$(curr).remove();
				// trigger the next image
				LoadImage(index+1,max);
			}).attr('src', images[index]);
		}
	}
}
function resizeIntroImgs()
{
	$( '#slider img' ).each( function(){
		resizeImg( $( this ) );
	});
}
function start_cycle_intro()
{
	$('#slider ul').cycle({
		fx: 			'fade',
		timeout:  		3500,
        speed:    		750,
		next: 			'#next',
		prev: 			'#prev', 
		prevNextClick: 	onNextPrev, 
		containerResize: 0
	});
	
	$( '.loading' ).fadeOut();
	
	function onNextPrev(isNext, zeroBasedSlideIndex, slideElement) {
        $('#slider ul').cycle( 'pause' );
        $( '#pause' ).hide();
        $( '#play' ).show();
    }

    $( '#pause' ).click( function() {
        $('#slider ul').cycle( 'pause' );
        $( this ).hide();
        $( '#play' ).show();
    });

    $( '#play' ).click( function() {
        $('#slider ul').cycle( 'resume' );
        $( this ).hide();
        $( '#pause' ).show();
    });
}
function start_cycle_images()
{
	var win_h = $(window).height();
	$('#slider ul').cycle({
		fx: 			'scrollHorz',
		timeout:  		0,
        speed:    		1250,
		next: 			'#next',
		prev: 			'#prev', 
		prevNextClick: 	onNextPrev,
		containerResize: 0
	});
	
	function onNextPrev(isNext, zeroBasedSlideIndex, slideElement) {
		var img_index = zeroBasedSlideIndex + 1;
		if ( img_index < 10 )
			img_index = '0' + img_index;
		$( '#counter' ).html( img_index );
    }

	$('#slider ul').css( 'height' , win_h + 'px' );
}
/** HOME **/
function home_start( image_src ){
	// background image pre-loader
	// get parent
	$( '.loading' ).show();
	var parent = $(".home_image");
	var img = new Image();
	// image onload
	$(img).load(function () {
		$( this ).css('display','none'); // since .hide() failed in safari
		$( parent ).append(this);
		resizeHomeImg( $( this ) );
		$( this ).fadeIn('slow',function(){
			$( '.loading' ).fadeOut();
		});
	}).error(function () {
		// on error remove current
		$( parent ).remove();
	}).attr('src', image_src);
	// show hide news backgrounds & images
	$( '.news_wrapper' ).hover(
		function(){
			$( this ).find( '.news_background' ).height( $( this).find( '.news_content').height() + 12 );
			$( this ).find( '.news_background' ).fadeTo("slow", 1);
			$( this ).find( '.news_wrapper' ).fadeTo("slow", 1);
			$( this ).find( '.news_image img' ).fadeTo("slow", 1);
		},
		function(){
			$( this ).find( '.news_background' ).fadeTo("slow", 0);
			$( this ).find( '.news_wrapper' ).fadeTo("slow", 0);
			$( this ).find( '.news_image img' ).fadeTo("slow", 0);
		}
	);
	// fade out img on link clicks
	$("a").live("click", function(){
	  var href = $(this).attr("href");
	  var target = $(this).attr("target");
	  var animDuration = 500;
	  // Do animation here; duration = animDuration.
		if(target != "new")
		{
			$(".home_image").fadeOut(animDuration);
			setTimeout(function () {
				window.location = href;
			}, animDuration);
		}
		else
			{
				window.open(href);
			}

	  return false; // prevent user navigation away until animation's finished
	});
	
}
/** INFO :: PUBLICATIONS **/
// slider
function publications_start(){
	$( '#slide_container' ).find( 'img' ).click( function() {
		var _nImg = $( this ).attr( 'src' ).replace( 'small_' , 'large_' );
		var objImagePreloader = new Image();
		objImagePreloader.onload = function() {
			$('#thumb_large').find( 'img' ).attr('src' , _nImg );
			//	clear onLoad, IE behaves irratically with animated gifs otherwise
			objImagePreloader.onload=function(){};
		};
		objImagePreloader.src = _nImg;
	});
	var _totWidth = $( '#tot_width' ).val();
	$( '#slide_container' ).width( _totWidth + 'px' );
	$( '#slide_container' ).find( 'img:first' ).addClass( 'alpha' );
	$( '#slide_container' ).find( 'img:last' ).css( 'margin-right' , '0' );
	if( _totWidth > 584 ){
		if( $( '#arrow_right' ).hasClass( 'inactive' ) ){
			$( '#arrow_right' ).toggleClass( 'inactive' );
			$( '#arrow_right' ).css( 'cursor' , 'pointer' );
			$( '#arrow_right' ).click( function() {
				var _margin = 0;
				var _gutter = 0;
				$( '#slide_container' ).find( 'img' ).each( function( i , el ) {
					_margin += parseInt( $( el ).width() );
					_gutter = i;
				});
				var _totWidth = _margin + ( ( _gutter * 8  ) );
				$( '#slide_container' ).width( _totWidth );
				var _marginLeft = parseInt( $( '#slide_container' ).css( 'margin-left' ).replace( 'px' , '' ) );
				if( _marginLeft < 0 )
					_marginLeft = _marginLeft * -1;
				_marginLeft += 584;
				var _margin = 0;
				var _outEach = false;
				var _intCount = 0;
				$( '#slide_container' ).find( 'img' ).each( function( i , el ) {
					_margin += parseInt( $( el ).width() );
					if( !_outEach ){
						_intCount = i;
						if( _marginLeft < _margin ) {
							_marginLeft = _margin;
							_outEach = true;
						}
					}
				});
				_intCount = _intCount * 8;
				_marginLeft = ( ( _marginLeft + _intCount ) * -1 ) + 584;
				if ( _marginLeft < _margin ) {
					if(( _marginLeft * -1 )+584 <= _totWidth ) {
						$( '#slide_container' ).stop().animate( { marginLeft: _marginLeft + 'px'} , 600 , function() {
							$( '#arrow_left' ).removeClass( 'inactive' );
							$( '#arrow_left' ).css( 'cursor' , 'pointer' );
							if( ( _marginLeft * -1 )+584 == _totWidth ) {
								$( '#arrow_right' ).addClass( 'inactive' );
								$( '#arrow_right' ).css( 'cursor' , 'default' );
							}
						});
					}
				}
			});
		}
		$( '#arrow_left' ).click( function() {
			var _marginLeft = parseInt( $( '#slide_container' ).css( 'margin-left' ).replace( 'px' , '' ) );
			if( _marginLeft < 0 )
				_marginLeft = _marginLeft * -1;
			var _margin = 0;
			var _outEach = false;
			var _intCount = 0;
			$( '#slide_container' ).find( 'img' ).each( function( i , el ) {
				_margin += parseInt( $( el ).width() )+8;
				if( !_outEach ){
					if( _marginLeft <= _margin ) {
						_intCount = i;
						_marginLeft = _margin-( parseInt( $( el ).width() )+8 );
						_outEach = true;
					}
				}
			});
			_marginLeft = ( _marginLeft  ) * -1;
			if( _marginLeft > 0 )
				_marginLeft = 0;
			if( _marginLeft <= 0 ) {
				$( '#slide_container' ).stop().animate( { marginLeft: _marginLeft + 'px'} , 600 , function() {
					$( '#arrow_right' ).removeClass( 'inactive' );
					$( '#arrow_right' ).css( 'cursor' , 'pointer' );
					if( _marginLeft == 0 ) {
						$( '#arrow_left' ).addClass( 'inactive' );
						$( '#arrow_left' ).css( 'cursor' , 'default' );
					}
				});
			}
		});
	}
}
/** INFO :: HISTORY **/
// show pdf size
var is_ie;
$( document ).ready( function(){
	if(jQuery.browser.msie==true)
		is_ie = true;
	var _baseurl = $( 'base' ).attr( 'href' );
	if( $( '.info_pdf' ).attr( 'class' ) == 'info_pdf') {
		var _este = $( '.info_pdf' );
		$.get( _baseurl + 'index.php/info/historyPDF', function( data ){
			var _sizeAux = data.split( 'startxref' );
			var _size = _sizeAux[1].replace( '%%EOF' , '' );
			_size = _size/1000;
			_size = _size.toFixed(2).replace( '.' , ',' );
			_este.html( 'PDF ' + _size + 'KB' );
		});
	}
});