// define all works
var allworks = {
	bureau347: {
		logo: 'basic',
		title: '#ff8dd2',
		// thumbnails: 'img/work_thumb_intro.jpg',
		services: []
	},
	dim_1: {
		logo: 'white',
		title: '#2fc6fd',
		website: 'http://www.lepluslongcompliment.be/',
		thumbnails: 'img/work_thumb_dim_1.jpg',
		services: ['a_1', 'a_2' , 'b_1', 'b_2', 'b_3', 'b_4', 'c_2', 'c_4']
	},
	jojo_1: {
		logo: 'white',
		title: '#4da0e5',
		website: 'http://www.jojoproject.com/',
		thumbnails: 'img/work_thumb_jojo_1.jpg',
		services: ['a_1', 'a_2' , 'a_3' , 'a_4' , 'b_1', 'b_2', 'b_3', 'c_1', 'c_2', 'c_3']
	},
	bouygues_1: {
		logo: 'white',
		title: '#fe7000',
		website: 'http://www.bouygues-immobilier.be/',
		thumbnails: 'img/work_thumb_bouygues_1.jpg',
		services: ['a_1', 'a_4' , 'b_1', 'b_2', 'b_3', 'b_4', 'c_1', 'c_2', 'c_3']
	},
	renault_1: {
		logo: 'white',
		title: '#3863fe',
		website: 'http://www.cliotube.com/',
		thumbnails: 'img/work_thumb_renault_1.jpg',
		services: ['a_1', 'a_2', 'a_4', 'b_1', 'b_2', 'b_4', 'c_3', 'c_4']
	},
	renault_2: {
		logo: 'white',
		title: '#cfd0be',
		website: 'http://www.4control.be/',
		thumbnails: 'img/work_thumb_renault_2.jpg',
		services: ['a_1', 'a_4', 'b_1', 'b_2', 'c_1', 'c_3']
	},
	dassault_1: {
		logo: 'white',
		title: '#a1bdd3',
		website: 'http://www.3ds.com/',
		thumbnails: 'img/work_thumb_dassault_1.jpg',
		services: ['a_1', 'a_4', 'b_1', 'b_2', 'b_3', 'c_3']
	},
	panique_1: {
		logo: 'white',
		title: '#2e8268',
		website: 'http://www.paniqueauvillage.com/',
		thumbnails: 'img/work_thumb_panique_1.jpg',
		services: ['a_1', 'a_3', 'b_1', 'b_2', 'c_1', 'c_2', 'c_4']
	},
	wonderbra_1: {
		logo: 'white',
		title: '#fdec2c',
		website: 'http://wonderbra.be/7',
		thumbnails: 'img/work_thumb_wonderbra_1.jpg',
		services: ['a_1', 'a_2', 'a_3','a_4',  'b_1', 'b_3', 'b_4', 'c_3', 'c_4']
	},
	belgacom_1: {
		logo: 'white',
		title: '#2496cf',
		website: 'http://www.pingping.be/',
		thumbnails: 'img/work_thumb_belgacom_1.jpg',
		services: ['a_2', 'b_1', 'b_2', 'c_2', 'c_3']
	},
	wonderbra_2: {
		logo: 'white',
		title: '#fdec2c',
		website: 'http://www.youtube.com/watch?v=v0jlRceGBOI',
		thumbnails: 'img/work_thumb_wonderbra_2.jpg',
		services: ['a_1', 'a_3', 'b_1']
	},
	lesjeux_1: {
		logo: 'white',
		title: '#e82219',
		website: 'http://www.jeuxdhiver.be/patio/',
		thumbnails: 'img/work_thumb_lesjeux_1.jpg',
		services: ['a_2', 'b_1', 'b_4', 'c_4']
	}
	
	// no comma after last element
};
// highlight the services
function highlightServices(s) {
	jQuery('#services li').removeClass('selectedItem');
	if (s != null) {
		for (var i = 0; i < s.length; i++) {
			jQuery('#services li#'+s[i]).addClass('selectedItem');
		}
	}
}
// function called before the animation
function beforeAnim(el) {
	// hide titles and services
	jQuery(el).find('hgroup').hide();
	highlightServices(null);
	jQuery('#slidePosition ul li a').removeClass('active');
}
// function called after the animation
function afterAnim(el) {
	// show titles
	jQuery(el).find('hgroup').show();
	// get id of work
	var id = jQuery(el).attr('id');
	// find services and clients for the work
	if (allworks[id] != undefined) {
		highlightServices(allworks[id].services);
		// set logo
		jQuery('header h1').removeAttr('class');
		if (allworks[id].logo != undefined && allworks[id].logo != '') {
			jQuery('header h1').addClass(allworks[id].logo);
		}
		if (allworks[id].title != undefined && allworks[id].title != '') {
			jQuery(el).find('h3').css('color', allworks[id].title);
		}
	}
	// set the corect url to the bullet
	setPositionHref();
	// set the hash of the location
	if (id=='bureau347') {
		window.location.hash = '';
		jQuery("nav").css('display', 'inherit');
		jQuery("#moreWork").slideUp("fast");// .css('display', 'none');

	} else {
		window.location.hash = '/work/'+id;
		jQuery("nav").css('display', 'none');
		jQuery("#moreWork").slideDown("fast");  //.css('display', 'inherit');
	}
}

function setPositionHref() {
	// reset all href
	jQuery('#slidePosition a').each(function (idx){
		var idx = parseInt(jQuery(this).attr('id').substr(5))-1;
		var id = jQuery('#viewport ul li:eq('+idx+')').attr('id');
		if (jQuery(this).hasClass('active')) {
			if (allworks[id] != undefined && allworks[id].website != undefined && allworks[id].website != '') {
				jQuery(this).attr('href', allworks[id].website);
			} else {
				jQuery(this).attr('href','#/work/'+id);
			}
		} else {
			jQuery(this).attr('href','#/work/'+id);
		}
	});
}
		

function slide(){
	jQuery('.next').click();
}

var linePositioning = function() {
	//scrollbar width
	var div = jQuery('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
	// Append our div, do our calculation and then remove it
	jQuery('body').append(div);
	var w1 = jQuery('div', div).innerWidth();
	div.css('overflow-y', 'scroll');
	var w2 = jQuery('div', div).innerWidth();
	jQuery(div).remove();
	scrollbarWidth = w1 - w2;
	
	// windows width
	var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth - scrollbarWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
									
	widowsWidth = myWidth;
	marginWidth =  (widowsWidth - 868)/2;
	randomNumber = Math.round(Math.random() * 15)*18; // random number between 0 and 270 multiple of 18
	linePosition = (randomNumber + marginWidth) -1;
	jQuery('.line').css('left', linePosition+"px");
};

var preloader = function() {
	jQuery.each(allworks, function(idx, work){
		if (work['thumbnails'] != undefined) {
			jQuery("<img>").attr("src", work['thumbnails']);
		}
	});
}
 
jQuery(document).ready(function(){
	
	linePositioning();
	preloader();
	jQuery(window).resize(function() {
		linePositioning();
    });
	
	// get the work passed in the url
	var work_id = '';
	if (window.location.hash!=undefined && window.location.hash.indexOf('/work/') > -1) {
		work_id = window.location.hash.substr(window.location.hash.indexOf('/work/')+6);
	}
	
	var car = jQuery('#viewport').carousel('#previous', '.next', {beforeAnimate: beforeAnim, afterAnimate: afterAnim, startId: work_id});
	// hide all titles
	jQuery('#viewport hgroup').hide();
	// handle left/right or p/n keypress
	jQuery(window).keypress(function(e) {
		var keycode = (e == null ? event.keyCode : e.keyCode);
		if (keycode==37 || keycode==112) {
			jQuery('#previous').click();
		} else if (keycode==39 || keycode==110) {
			jQuery('.next').click();
		}
	});
	
	jQuery("#twitter-link").hover(function(){
	   jQuery("#twitter-link span").addClass("hover");
	 },function(){
	   jQuery("#twitter-link span").removeClass("hover");


	 });
});