$(document).ready(function(){

/*
	$(document).bind("contextmenu",function(e){
        return false;
    });
*/

	var currentPosition = 0;
	var slideWidth = 560;
	var slides = $('.vimeoSlide');
	var numberOfSlides = slides.length;
	var fastAnim = 500;
	var slowAnim = 4000;
	var animateTime;
	var embedRelAttr;
	var the_title;
	var playCounter = 0;
	
	
	$('#loader').load('../html/imprint.html');
	
	// show fake-preloading overlay	
	$('#overlay').show();
	// apply controls to vimeoSlideshow DOM-Element
	$('#vimeoSlideshow')
		.prepend('<span class="vimeoControl" id="vimeoLeftControl" rel="back"></span>')
		.append('<span class="vimeoControl" id="vimeoRightControl" rel="for"></span>');
	// hide controls to fade in gently later
	$('.vimeoControl').hide();
	$('.twitter').hide();	
	// initialize page after fake-preloader timeshift
   	setTimeout(function() {
   		// fade out loading-gif
   		$('.loading').fadeOut(500, function(){
   			// show wrap and fade out fake-preloader
   			$('#dɐɹʍ').show(1);
   			$('#overlay').fadeOut(500, function(){

	   			embedRelAttr = $(document).find('#vimeoSlidesContainer div.vimeoSlide:first-child embed');
	   			embedRelAttr = embedRelAttr.attr('rel').split('{+☠+}');
	   			the_title = (embedRelAttr[1].length >= 36) ? embedRelAttr[1].substr(0,36)+'...' : embedRelAttr[1];
	   			$('.vimeotxt').html('<img src="'+embedRelAttr[5]+'" style="float:left; padding:0 7px 10px 0;"/><h1 title="'+embedRelAttr[1]+'">'+the_title+'</h1><h2>'+embedRelAttr[2]+'</h2><h3><a href="'+embedRelAttr[3]+'">'+embedRelAttr[3]+'</a></h3><span>'+embedRelAttr[7]+' '+embedRelAttr[4]+' '+embedRelAttr[6]+'</span>');
	   			
	   			$('.twitter a').attr('title', 'tweet a link to "'+embedRelAttr[1]+'" on twitter.com');
	   			$('.twitter a').attr('href', 'http://twitter.com/home?status='+embedRelAttr[3]+' another great video by '+embedRelAttr[2]+' (found at http://gfries.de)');
	   			
				$('body').animate({ backgroundColor: embedRelAttr[0] }, 500, function(){
					// fade in navigational elements
					$('.vimeoControl').fadeIn(500, function(){
						$('.twitter').fadeIn(500);
					});
					
				});
				
				
/*
			$('#infoBox').bind('click', function(){
	    		window.open(embedRelAttr[3]);
	   		})
*/
				
   			});
   		});
   	}, 2500);
   	// setup slider container
	$('#vimeoSlidesContainer').css('overflow', 'hidden');
	// wrap slides
	$('.vimeoSlide').wrapAll('<div id="vimeoSlideInner"></div>').css({
		'float' : 'left',
	    'width' : slideWidth
	});
	$('#vimeoSlideInner').css('width', slideWidth * numberOfSlides);
	// control hover slides
	$('#vimeoLeftControl').bind('mouseenter', function(){
		hoverSlide(this, 0, 'in', fastAnim, 5);
	}).bind('mouseleave', function(){
		hoverSlide(this, 0, 'out', 200, 5);
	});
	$('#vimeoRightControl').bind('mouseenter', function(){
		hoverSlide(this, 1, 'in', fastAnim, 5);
	}).bind('mouseleave', function(){
		hoverSlide(this, 1, 'out', 200, 5);
	});	
	
	// "HYPERLINKS"
	$('#infoBox').bind('click', function(){
	    window.open(embedRelAttr[3]);
	})
	
	$('h1.vimeo').bind('click', function(){
	    window.open('http://vimeo.com/gerritfries/likes');
	})
	
	$('h1.twww').bind('click', function(){
	    window.open('http://twitter.com/since1985de');
	})
	
	$('h1.lfm').bind('click', function(){
	    window.open('http://www.lastfm.com/user/boringplanet');
	})
	
	$('h1.gowalla').bind('click', function(){
	    window.open('http://gowalla.com/users/gerritfries');
	})
	
	// preload images
	$(document).find('#vimeoSlidesContainer div.vimeoSlide embed').each(function(){
		embedRelAttr = $(this).attr('rel').split('{+☠+}');
		$('<img src="'+embedRelAttr[5]+'" />').appendTo('#image_container');
	});
	var the_title;
	
	
	$('.lastfm_track').bind('click', function(){
		//alert($(this).attr('rel'));
		window.open($(this).attr('rel'));
	});
	
	// control click function
	$('.vimeoControl').bind('click', function(){
		// animate slide
		currentPosition = ($(this).attr('id') == 'vimeoRightControl') ? currentPosition+1 : currentPosition-1;
		var reloadPosition = ($(this).attr('id') == 'vimeoRightControl') ? currentPosition : currentPosition+2;
	    if(currentPosition >= numberOfSlides){
	    	currentPosition = 0;
	    	animateTime = slowAnim;
	    } else if(currentPosition < 0){
	    	currentPosition = numberOfSlides - 1;
	    	animateTime = slowAnim;
	    } else {
	    	animateTime = fastAnim;
	    }
	    $('#vimeoSlideInner').animate({ marginLeft: slideWidth*(-currentPosition)}, animateTime);
	    
	    	    
	  	// animate background color
	  	var domPos = (currentPosition + 1);
	  	embedRelAttr = $(document).find('#vimeoSlidesContainer div.vimeoSlide:nth-child('+domPos+') embed');
	  	$('#infoBox img').fadeOut(150, function(){
	    	$(this).fadeIn(600);
	    });
	  	embedRelAttr = embedRelAttr.attr('rel').split('{+☠+}');
	  	setTimeout(function() {
	  	
	  		the_title = (embedRelAttr[1].length >= 36) ? embedRelAttr[1].substr(0,36)+'...' : embedRelAttr[1];
	  	
	    	$('body').animate({ backgroundColor: embedRelAttr[0] }, 'slow');
	    	$('.vimeotxt').html('<img src="'+embedRelAttr[5]+'" style="float:left; padding:0 7px 10px 0;"/><h1 class="the_title"  title="'+embedRelAttr[1]+'">'+the_title+'</h1><h2>'+embedRelAttr[2]+'</h2><h3><a href="'+embedRelAttr[3]+'" class="vimeo_link">'+embedRelAttr[3]+'</a></h3><span>'+embedRelAttr[7]+' '+embedRelAttr[4]+' '+embedRelAttr[6]+'</span>');
	    	$('.twitter a').attr('title', 'tweet a link to "'+embedRelAttr[1]+'" on twitter.com');
	    	$('.twitter a').attr('href', 'http://twitter.com/home?status='+embedRelAttr[3]+' another great video by '+embedRelAttr[2]+' (found at http://gfries.de)');
	    }, 200);
	    
/*
	    $('#infoBox').bind('click', function(){
	    	window.open(embedRelAttr[3]);
	    })
*/
	    
	    
	  	// reload last viewed video dom-element to prevent playing multiple videos at once
	  	if(playCounter === 1){	  	
			setTimeout(function() {
				var playercopy = $(document).find('#vimeoSlidesContainer div.vimeoSlide:nth-child('+reloadPosition+') embed').clone();
				$(document).find('#vimeoSlidesContainer div.vimeoSlide:nth-child('+reloadPosition+') embed').remove();
				$(document).find('#vimeoSlidesContainer div.vimeoSlide:nth-child('+reloadPosition+')').html(playercopy);  	
			}, fastAnim);
			playCounter = 0;
		}

	});
	
	$('embed').bind('click', function(){
		playCounter = 1;
	});
	
	if(connectionError === true){
		$('.vimeoControl').remove();
		$('embed').hide();
	}
});


function hoverSlide(rel, direction, inout, time, amount){

	switch(inout){
	case 'in':
	(direction === 0) 
	? $(rel).animate({ marginLeft: '-='+amount+'px', opacity: 0.5 }, time) 
	: $(rel).animate({ marginRight: '-='+amount+'px', opacity: 0.5 }, time);
	break;
	case 'out':
	(direction === 0) 
	? $(rel).animate({ marginLeft: '+='+amount+'px', opacity: 1 }, time) 
	: $(rel).animate({ marginRight: '+='+amount+'px', opacity: 1 }, time);
	break;
	}

}


