/*
 * copyright 2011 Albino bvba - http://albino.be
 *
 */
 
 
//global albino vars
$.albn = {
	location : {host : 'http://albino.be', path : '', hashbang : '/#!', hash : '', hashparts : '', previous : '', current : '/', target : '' }, 
    opacityFloor	: 0.2, 
    opacityCeil		: 1,
    opacityMiddle	: 0.4,
    speedHover		: 500,
    speedAccordion	: 400,
    speedSlider		: 800,
    speedScroll		: 1200,
	easeTypeSlider	: 'easeOutExpo',
	easeTypeScroll	: 'easeOutExpo',
	windowWidth		: 960,
	baseUrl			: 'http://albino.be/'    
};

$(document).ready(function(){
		
	if($('body').hasClass('home')){
		
		hashParser();
		
		$('a:not(.nohash)').live('click', function(e){
			e.preventDefault();
			hashSetter($(this).attr('href'));
		});
		
		ajaxForm();
		
		$(window).scroll(function () {
	    	floatingmenu();
		});
		
		$.albn.windowWidth = $(window).width();
		//fade en close all projects, then scroll to header	
		$('.project').css('opacity','0.2');				
		
		//trigger hover and fade project
		$('.project:not(.active)').live('mouseenter mouseleave', function(e){
			var opacity_level =  (e.type == 'mouseenter' ) ? $.albn.opacityCeil : $.albn.opacityFloor;	
			$(this).stop().fadeTo($.albn.speedHover, opacity_level);
		});
			
		initSlider();
				
		$(window).resize(function() {
			// resize slider on window resize
			$.albn.windowWidth = $(window).width();
			initSlider();
		}); 
		
	}
	
});

/*
 * set hash functions
 *
 */

function hashSetter($hs){
	$hs = $hs.replace( '#', '');
	$hs = $hs.replace( $.albn.location.host, '');
	$hs = $hs.replace( 'latest-project', 'projects/latest');
	if($hs.substring(0,1) != '/') $hs = '/'+$hs;
	if($hs.substr(-1,1) != '/') $hs = $hs+'/';
	if($hs == '/header/') $hs = '/';
	window.location = $.albn.location.host+$.albn.location.hashbang+$hs;
}

function hashRedirect($hs){
	window.location.replace($.albn.location.host+$.albn.location.hashbang+$hs);
}

function hashParser() {
	
	$(window).hashchange( function(){
		// check if hash could be valid redirect if not	
		$.albn.location.hash = location.hash.replace( /^#!/, '' );
		$.albn.location.path = $.albn.location.host+$.albn.location.hash;
		$.albn.location.target = $.albn.location.hash;
		$.albn.location.hashparts = $.albn.location.hash.split('/');
		if($.albn.location.hashparts.constructor != Array || $.albn.location.hashparts.length < 2){
			hashRedirect('/');
		}
		// check for pattern in hash redirect if not
		else{
			$.albn.location.previous = $.albn.location.current;
			$.albn.location.current = $.albn.location.target;
			$.albn.location.hashparts.shift();
			if($.albn.location.hashparts[$.albn.location.hashparts.length-1] == '' && $.albn.location.hashparts.length > 1) $.albn.location.hashparts.pop();
			switch($.albn.location.hashparts[0]){
				case '': 			closeProjects(true);	break;
				case 'about':		doScroll('about');	break;
				case 'newsletter':	doScroll('newsletter');	break;
				case 'contact':		doScroll('contact');	break;
				case 'projects':	initProjects();	break;
				case 'jobs':		doScroll('jobs');	break;
				default:			hashRedirect('/');
			}
		}
	});

	$(window).hashchange(); 
}

/* END of hash functions */

function doScroll($target,$offset) {
	if ($offset === undefined ) { $offset=0}
	var $targetPosition = $('#'+$target).offset().top+$offset;
	$('html, body').stop().animate({scrollTop: $targetPosition}, $.albn.speedScroll, $.albn.easeTypeScroll);

}				
				
				

/*
 * handle projects
 *
 */

function initProjects() {
	
	if($.albn.location.hashparts.length > 1 && $.albn.location.hashparts[1] != 'latest'){
		var $target = $('a[href$="'+$.albn.location.path+'"]').parent().parent(); 
		projectProcess($target);
	}
	else if($.albn.location.hashparts.length > 1 && $.albn.location.hashparts[1] == 'latest'){
		doScroll('latest-project',-120);
	}
	else {
		closeProjects(false);	
	}
	
}

function projectProcess($target) {
	
	if($target.children(".project-info").is(':hidden') ) { //actie starten
							
				closeProjects(false);
				$target.stop().fadeTo($.albn.speedHover, $.albn.opacityCeil );
				
				$targetUrl = $target.children('.project-img').children('a').attr('href');				
				$targetUrl =  $targetUrl.replace($.albn.baseUrl, '');
				window.location.hash = '#!/'+$targetUrl;
							
				$target.addClass('active').children(".project-info ,.project-bottom").slideDown($.albn.speedAccordion, function(){
										
					$target.children(".project-img").fadeTo(200, 0, function(){				
						$target.children(".slideshow").fadeTo(300, 1, function(){
							var $targetPosition = $target.offset().top - 60;	
							$('html, body').stop().animate({scrollTop: $targetPosition}, 400);
						}).show();
					});					
				});
	
				$('.project:not(.active)').stop().fadeTo($.albn.speedAccordion, $.albn.opacityFloor);
			}
}


function closeProjects($callback){
	var $list = $('#projects');	
	var $activeProjects = $list.find('.project.active')
	
	if($activeProjects.length > 0){
		$activeProjects.each(function(){

			$project = $(this);
			$project.removeClass('active');
			$project.children(".slideshow").fadeTo($.albn.speedAccordion, 0 , function(){
				$project.children(".slideshow").hide()
				$project.children(".project-img").fadeTo(0, 1);
				$project.fadeTo($.albn.speedAccordion, $.albn.opacityFloor)
				$project.children(".project-info ,.project-bottom").slideUp($.albn.speedAccordion, function(){
					if($callback){
						var $targetPosition = $('#header').offset().top;
						$('html, body').stop().animate({scrollTop: $targetPosition}, $.albn.speedScroll, $.albn.easeTypeScroll);				
					}			
				});								
			});
		
		});
	}else if($callback){
		var $targetPosition = $('#header').offset().top;
		$('html, body').stop().animate({scrollTop: $targetPosition}, $.albn.speedScroll, $.albn.easeTypeScroll);				
	}

}

/* END handle projects */


/*
 * extra animation functions
 *
 */

function ajaxForm() {
	$('form#cm_ajax_shortcode_1 button').click(function() {
		$('form#cm_ajax_shortcode_1 button').hide();
		$('form#cm_ajax_shortcode_1 .cm_ajax_success').hide();
		$('form#cm_ajax_shortcode_1 .cm_ajax_failed').hide();
		$('form#cm_ajax_shortcode_1 .cm_ajax_loading').show();
		$.ajax({ 
			type: 'POST',
			data: $('form#cm_ajax_shortcode_1').serialize()+'&cm_ajax_response=ajax',
			success: function(data) {
				$('form#cm_ajax_shortcode_1 .cm_ajax_loading').hide();
				if (data == 'SUCCESS') {
					$('form#cm_ajax_shortcode_1 .cm_ajax_success').show();
				} else {
					$('form#cm_ajax_shortcode_1 button').show();
					$('form#cm_ajax_shortcode_1 .cm_ajax_failed').show();
				}
			}
		});
		return false;
	});
} 


function floatingmenu(){

	$currentPosition = $("body").scrollTop();
	if($currentPosition == 0) $currentPosition = $("html").scrollTop();
	$headerPosition = $('#header').offset().top - 500;
	$floatingMenu = $('#home');
	if($currentPosition > $headerPosition && $currentPosition < $headerPosition+2000){
		if($floatingMenu.is(':visible')){ $floatingMenu.stop().fadeTo(400, 0, function(){ $floatingMenu.hide(); }); };
	}
	else if($floatingMenu.is(':hidden')){
		$floatingMenu.stop().fadeTo(900, 1, function(){
			$floatingMenu.show();
		});    
    }
}

/* END extra animation functions */


/*
 * project slideshow
 *
 */

function initSlider(){
	var $list = $('#projects');	
	$list.find('.slideshow').each(function(){
		
		var $slideshow = $(this);
		$slideshow.unbind('mousemove');
		$slideshow.css('width',$.albn.windowWidth);
					
		var $slider	= $slideshow.children('.slider');
		var $totalWidth = $slider.find('img').length*645;
					
		if($totalWidth>$.albn.windowWidth){
			$totalWidth = 400 + $totalWidth;		
			$slider.css({'width' : $totalWidth + 'px', 'margin' : '0', 'padding-left' : '200px', 'padding-right' : '200px', 'left': '0'});			
			scrollSlider($slideshow,$slider);
		}
		else{
			$slider.css({'width' : $totalWidth + 'px', 'margin' : 'auto', 'padding':'0', 'left': '0'});
		}
			
	});
	
}


function scrollSlider($outer, $inner){	
					
	$outer.scrollLeft(0);
	$outer.unbind('mousemove').bind('mousemove',function(e){
		var $innerWidth = $inner.width();
		var mouseCoords=(e.pageX);
		var mousePercentX=mouseCoords/$.albn.windowWidth;
		var destX=-((($innerWidth-($.albn.windowWidth))-$.albn.windowWidth)*(mousePercentX));
		var newPosition = Math.round(destX-mouseCoords);
		var customSpeed = Math.abs(newPosition);
		$inner.stop().animate({left: newPosition}, customSpeed, $.albn.easeTypeSlider);
	});	
			
}

/* END project slideshow */
