/* javascript setup done by mtness.net *\
	on behalf of primel.net
    for http://bc-horvath.de
\*              2008-07-30             */


var DEBUG;(typeof(window.console) != "undefined")?DEBUG=1:DEBUG=0;//alert(DEBUG);

var url = window.location;

var scrolly; 

var counter 			= 0;														if(DEBUG==1)console.log("         counter:"+counter);
var myElement 			= $('overview').getElements('.project_mini_pic');			if(DEBUG==1)console.log("   No. of images:"+myElement.length);
var container_height 	= $('portfolio').getStyle("height").toInt();				if(DEBUG==1)console.log("container_height:"+container_height);
var innerheight 		= myElement.length*(36+10);									if(DEBUG==1)console.log("     innerheight:"+innerheight);
var y_offset 			= innerheight%container_height;								if(DEBUG==1)console.log("        y_offset:"+y_offset);   
var y_index_o 			= 0; 														if(DEBUG==1)console.log("              yO:"+y_index_o);
var y_index_u 			= y_offset; 												if(DEBUG==1)console.log("              yU:"+y_index_u);

var scrollarea 			= new Fx.Scroll($('portfolio'));

var pagebrowserEls 		= $('pagebrowser').getElements('div');

var imgElements  		= $('overview').getElements('.project_mini_pic img');
var imgLinks 			= $('overview').getElements('.project_mini_pic a');

var stopper				= 0;														//if(DEBUG==1)console.log("         stopper:"+stopper);

// crossfade setup
// always pass an Element!
function xFade(nextEl) {
	if ( Browser.Platform.ipod != true ) $$('.active img').set('tween', {duration: '192'}).tween('opacity', 0.4);
	// crossfade the images
	$('content').set('tween', {
		onComplete: function() {
			imgLinks.removeClass('active');
			$('details').empty().setStyle('background', 'transparent');
			$('project_img').set('src',nextEl.get('href'));
			if( nextEl.getParent().getElement('.mini_desc') != null ) $('project_desc').set('html',nextEl.getParent().getElement('.mini_desc').get('html'));
			nextEl.addClass('active');
			$('content').set('tween');
			(function(){ 
				$('content').tween('opacity', 1); 
				if ( Browser.Platform.ipod != true ) { $$('.active img').set('tween', {duration: '192'}).tween('opacity', 1); }
			}).delay(512);
		}
	});
	$('content').tween('opacity', 0);
}


window.addEvent('domready',function(){

	//initalize the portfolio view
	if ( $('selected').getParent().id == 'Portfolio' ) $('details').set('styles', { 'background': 'none' });

	// adjust scrollarea height to the actual value
	$('overview').setStyle('height',innerheight);

	// reset scrollarea position on page (re)load
	$('portfolio').setStyle('overflow','hidden');
	$('portfolio').scrollTo( 0, 0 );
	
	
	// Mousemove logic, disabled for iphone.
	if ( Browser.Platform.ipod != true ) {
		var scrollElement = $('portfolio');
		var myScroller = new Scroller(scrollElement, {area: 192, velocity: 0.1});
		scrollElement.addEvent('mouseenter', myScroller.start.bind(myScroller));
		scrollElement.addEvent('mouseleave', myScroller.stop.bind(myScroller));	
	}
	// slidescroller logic
	$('arrow_up').addEvent('click',function(){
		
		if( y_index_u >= y_offset+container_height ) {
			y_index_o -= container_height;												//if(DEBUG==1)console.log("       y_index_o:"+y_index_o);
			y_index_u -= container_height;												//if(DEBUG==1)console.log("       y_index_u:"+y_index_u);
		}
		scrollarea.start( 0, y_index_u-container_height );
		
	});

	$('arrow_down').addEvent('click',function(){
		if(  y_index_o < (innerheight-y_offset) ) {
			y_index_o += container_height;												//if(DEBUG==1)console.log("       y_index_o:"+y_index_o);
			y_index_u += container_height;												//if(DEBUG==1)console.log("       y_index_u:"+y_index_u);
		}
		scrollarea.start( 0, y_index_o );
	});


	// thumbnail opacity setup, disabled for iphone.
	if ( Browser.Platform.ipod != true ) {
		imgElements.each(function(i) {
			i.set({'opacity':'0.4'});
		});

		imgElements.each( function(i) {
			i.addEvent('mouseenter',function(){
				i.set('tween', {duration: '192'});
				i.tween('opacity', 1);
			});
			i.addEvent('mouseleave',function(){
				if(i.getParent().hasClass('active')!=true) {
					i.set('tween', {duration: '192'});
					i.tween('opacity', 0.4);
				}
			});
		});
	}

	// pagebrowser opacity setup, disabled for iphone.
	if ( Browser.Platform.ipod != true ) {
		pagebrowserEls.each(function(i) {
			i.set({'opacity':'0'});
		});
		
		$('content').addEvent('mouseenter', function(){
			if( $('selected').getParent().id == 'Portfolio' ) {
				pagebrowserEls.each(function(i) {
					if( counter==0 )
					{ 
						$('next').fade(0.6);
					} else if ( counter == imgLinks.length-1 ) {
						$('prev').fade(0.6);
					} else {
						i.fade(0.6);
					}
				});
			}
		});
		$('content').addEvent('mouseleave', function(){ 
			if( $('selected').getParent().id == 'Portfolio' ) {
				pagebrowserEls.each(function(i) {
					i.fade('out');
				});
			}
		});	

	} else {
	
		if( $('selected').getParent().id == 'Portfolio' ) {
			pagebrowserEls.each(function(i) {
				if( counter==0 )
				{ 
					$('next').fade(0.6);
				} else if ( counter == imgLinks.length-1 ) {
					$('prev').fade(0.6);
				} else {
					i.fade(0.6);
				}
			});
		}
	}

	// pagebrowser mouseover behavior 
	pagebrowserEls.each( function(i) {
		i.addEvent('mouseenter',function(){
			i.set('tween', {duration: '192'});
			i.tween('opacity', 0.9);
		});
		i.addEvent('mouseleave',function(){
			if(i.getParent().hasClass('active')!=true) {
				i.set('tween', {duration: '192'});
				if( counter!=0 && counter!=imgLinks.length-1 ) 
				{
					i.tween('opacity', 0.6);
				} else {
					i.tween('opacity', 0);
				}
			}
		});
	});
	
	// image link setup
	imgLinks.each( function(i,index) {
		i.addEvent('click',function(e){
			e.stop();
			stopper		= 1;															//if(DEBUG==1)console.log("         stopper:"+stopper);

			xFade(i);
			
			counter = index;															//if(DEBUG==1)console.log("         counter:"+counter);

			// reset menu highlight to portfolio
			if( $('selected').getParent().id != 'Portfolio' ) 
			{
				$('selected').set({ id:'' });
				$('Portfolio').getElement('a').set('id','selected');
			}
		});
	});
	
	// pagebrowser setup	
	$('prev').addEvent('click',function(){
		if(  counter > 0  ) {
			counter--;																	if(DEBUG==1)console.log("         counter:"+counter);
			stopper		= 1;
			i = imgLinks[counter];
			xFade(i);
			// scroll pagebrowser to active thumbnail
			scrollarea.start( 0, counter * 46 );
		}
	});
	$('next').addEvent('click',function(){
		if(  counter < imgLinks.length-1 ) {
			counter++;																	if(DEBUG==1)console.log("         counter:"+counter);
			stopper		= 1;
			i = imgLinks[counter];														if(DEBUG==1)console.log("imgLinks[counter]:"+imgLinks[counter]);
			xFade(i);
			// scroll pagebrowser to active thumbnail
			scrollarea.start( 0, counter * 46 );
			//if ( !( url == "http://bc-horvath.de/portfolio/" ) || !( url == "http://www.bc-horvath.de/portfolio/" ) ) {
				// window.location = "http://bc-horvath.de/portfolio/";
			//}
		}
	});

});


window.addEvent('load',function(){
	// slideshow setup on page enter
	
	url = window.location;
	
	if ( ( url == "http://bc-horvath.de/" ) || ( url == "http://www.bc-horvath.de/" ) ) {	if(DEBUG==1)console.log("   window.location:"+window.location);
	
		imgLinks[0].addClass('active');

		var slide_items	= 10;
		var dur 		= 4000;

		var while_i=0; 
		while ( while_i<slide_items ) {
			(function(){ 
				if(stopper == 0)
				{
					counter++;															//if(DEBUG==1)console.log("         counter:"+counter);
					xFade( imgLinks[counter] );
					// scroll pagebrowser to active thumbnail
					scrollarea.start( 0, counter * 46 );
				}
			}).delay( (while_i+1)*dur );
			while_i++;
		}
	
	}
	
	if ( Browser.Platform.ipod != true ) { $$('.active img').set('tween', {duration: '192'}).tween('opacity', 1); }
	
	// deeplinks: get index of active item in array
	for(var n=0;n<imgLinks.length;n++){
		if( imgLinks[n].hasClass('active')){
			counter = n;
		}
	}
	// scroll pagebrowser to active thumbnail
	scrollarea.start( 0, counter * 46 );

});

Scroller.implement({
	scroll: function(){
		var size = this.element.getSize(), scroll = this.element.getScroll(), pos = this.element.getOffsets(), scrollSize = this.element.getScrollSize(), change = {'x': 0, 'y': 0};
		for (var z in this.page){
			if (this.page[z] < (this.options.area + pos[z]) && scroll[z] != 0)
				change[z] = (this.page[z] - this.options.area - pos[z]) * this.options.velocity;
			else if (this.page[z] + this.options.area > (size[z] + pos[z]) && scroll[z] + size[z] != scrollSize[z])
				change[z] = (this.page[z] - size[z] + this.options.area - pos[z]) * this.options.velocity;
		}
		if (change.y || change.x) {														//if(DEBUG==1)console.log("         change.y:"+change.y);

			this.fireEvent('change', [scroll.x + change.x, scroll.y + change.y]);
			scrolly = scroll.y/container_height;										//if(DEBUG==1)console.log("          scrolly:"+scrolly);
			scrolly = scrolly.toInt();													//if(DEBUG==1)console.log("          scrolly:"+scrolly);
			
			y_index_o = scrolly.toInt() * container_height;								//if(DEBUG==1)console.log("        y_index_o:"+y_index_o);
			y_index_u = ( scrolly.toInt() * container_height )+y_offset;				//if(DEBUG==1)console.log("        y_index_u:"+y_index_u);
				
		}
	}
});
