// update slideshow
function slideshow(options) {

	this.update = update,
	this.bind_events = bind_events,
	this.yo = yo
	this.interval = options.interval;
	interval = this.interval;
	this.time = 0;
}

function update() {

	$p.stop().scrollTo(0,0);
	$na = $('#navigation');
	$tmp = $na.find('li:eq(0)').clone(true);
	$na.find('li').remove();
	$r.find('li').remove();
	// ajax from here!
	
	// build new slideshow
	var count = project.img.length;
	/*for(i=0;i<count;i++) {
		$j = jQuery('<li><a href="#" class="thumb"><img src="'+project.img[i].thumb+'"/></a></li>');
		$na.append($j);
		$r.append('<li><img src="'+project.img[i].src+'"/></li>');
	}*/
	this.bind_events();
}

function bind_events() {	
	// thumbnails navigation
		//*
		$t = $('a.thumb');
		$t.each(function(i) {
			$(this).click(function(e){
				e.preventDefault();
				/*$p.stop().scrollTo('li:eq('+i+')',200,{onAfter:function(){
					$p.stop();
				},onAfterFirst:function(){
					$p.stop();
				}});*/
				$r.stop().animate({'marginLeft':-(600*i)},1000);
			})
		})
		//*/
		
		// thumbnails hover effect
		$n = $('#navigation').find('a');
		$n.css( 'opacity' , .45 );
		$n.hover(function(){
			$this = $(this);
			$this.css('opacity',1);
		},function(){
			$this = $(this);
			if($this.hasClass('active')) {
				$this.css('opacity',1);
			} else {
				$this.css('opacity',.45);
			}
		});
		$n.each(function(i){
			$(this).click(function() {
				$n.removeClass('active').css( 'opacity' , .45 );
				$(this).addClass('active').css('opacity',1);
			})
		});
		
	
}

function yo() {
	
	$li = $r.find('li');
	l = $li.length;
	w = $li.css('width');
	tt = (l-1)*parseInt(w);
	currentTime = new Date();
	time = parseInt(currentTime.getTime());
	leftTime = 0;
	$r.animate({'marginLeft':0},0).animate({'marginLeft':-tt}, interval );
	$r.hover(
		function() {
			$(this).stop();
			d = new Date();
			now = d.getTime();
			leftTime = interval * (1 - Math.abs( parseInt( $r.css('marginLeft') ) ) / tt ) ;
		}, 
		function() {
			$(this).animate( {'marginLeft' : -tt }, leftTime );
		}
	);
}


function slideSwitch() {
    var $active = $('#bio-slideshow li.active-bio');
    var $next = $active.next();
    
    if( $next.length == 0 ) {
      $first.addClass('active-bio');
	$last.addClass('active-bio-last');
	$next = $first;   
    
    }
    
    $active.addClass('active-bio-last');

    $next.css({opacity: 0.0})
        .addClass('active-bio')
        .animate({opacity: 1.0}, 200, function() {
            $active.removeClass('active-bio');
        });
}

function preload( images , i ) {
	$('<img />').attr('src', images[i]).load(function(){
	   $('#roll-big').find('li:eq('+i+') img').remove();
        $('#roll-big').find('li:eq('+i+')').append( $(this) );
        if( i < images.length) {
      	preload( images , i + 1 );
      	}
       if(i == 0) {
       	slide.yo();
       }
	});
}
function preload_bio( images , i ) {
	$('<img />').attr('src', images[i]).load(function(){
	   $('#bio-slideshow').find('li:eq('+i+') img').remove();
        $('#bio-slideshow').find('li:eq('+i+')').append( $(this) );
        if( i < images.length) {
      	preload_bio( images , i + 1 );
        }
	});
}
function preload_published( images , i ) {
	$('<img />').attr('src', images[i]).load(function(){
	  $('#main').append( $(this).hide() );
        if( i < images.length-1) {
      	preload_published( images , i + 1 );
      	}
	});
}

function check_background() {

	var COOKIE_NAME = 'chiaramirelli-background';
	//alert($.cookie(COOKIE_NAME));
	if( $.cookie(COOKIE_NAME) == 'white' ) {
      
      	$('body').addClass('white');
      	$('#switch-color').addClass('white');
      	$('#chiaramirelli').addClass('white');
      
      }
      
}


function switch_background() {
	$color = $('#switch-color');
	$body = $('body');
	$body.toggleClass('white');
	$color.toggleClass('white');
	$('#chiaramirelli').toggleClass('white');
	$err = $('.error-code').find('img').attr('src');

	var COOKIE_NAME = 'chiaramirelli-background';
      var options = { path: '/', expires: 10 };
      if( $.cookie(COOKIE_NAME) == null ) {
      
      	$.cookie(COOKIE_NAME, 'white');
      
      } else {
      
      	$.cookie(COOKIE_NAME, null);
      
      }
      
	


}
