/*var redirectagent = navigator.userAgent.toLowerCase();
var redirect_devices = ['vnd.wap.xhtml+xml', 'sony', 'symbian', 'nokia', 'samsung', 'mobile', 'windows ce', 'epoc', 'opera mini', 'nitro', 'j2me', 'midp-', 'cldc-', 'netfront', 'mot', 'up.browser', 'up.link', 'audiovox', 'blackberry', 'ericsson', 'panasonic', 'philips', 'sanyo', 'sharp', 'sie-', 'portalmmm', 'blazer', 'avantgo', 'danger', 'palm', 'series60', 'palmsource', 'pocketpc', 'smartphone', 'rover', 'ipaq', 'au-mic', 'alcatel', 'ericy', 'vodafone', 'wap1', 'wap2', 'teleca', 'playstation', 'lge', 'lg-', 'iphone', 'android', 'htc', 'dream', 'webos', 'bolt', 'nintendo'];
for (var i in redirect_devices) {
	if (redirectagent.indexOf(redirect_devices[i]) != -1) {
		location.replace("m/");
	}
}
*/
$(document).ready(function() {
	
	if ($.browser.msie)
		$('body').addClass('ie');
	
    /* Roll hover images */
	var preload_array = new Array();
	var preload = function(src) {
		var img = new Image();
		img.src = src;
		preload_array.push(img);
	}
	
    $('img.roll').each(function (i) {
        //let's preload
        preload(this.src.replace(/([_-])off([._-])/, '$1on$2'));
        $(this).hover(
            function () { // over
                $(this).attr('src',this.src.replace(/([_-])off([._-])/, '$1on$2'));
            },
            function () { // out
                $(this).attr('src',this.src.replace(/([_-])on([._-])/, '$1off$2'));
            }
        );
    });
	
	var window_height = $(window).height();
	var window_width = $(window).width();
	var on_resize = function() {
		window_height = $(window).height();
		window_width = $(window).width();
		
		// show / hide the footer with contact informations if the screen is too small
		/*if (window_height < 924)
			$('.foot').stop().animate({bottom:-150}, 500, function(){
				$('.foot .carbure').css('bottom', 150).find('img').attr('src','assets/img/carbure-logo-off.png');
			});
		else {
			$('.foot .carbure').css('bottom', 0).find('img').attr('src','assets/img/carbure-logo.png');
			$('.foot').stop().animate({bottom:0}, 500);
		}*/
		
		// adjust the menu to fit the screen
		if (window_width < 900) {
			$('.head').stop().animate({paddingLeft:20}, 500);
		} else if (window_width < 1000) {
			$('.head').stop().animate({paddingLeft:100}, 500);
		} else {
			$('.head').stop().animate({paddingLeft:200}, 500);
		}
		
	};
	$(window).resize(on_resize);
	on_resize();
	
	$(window).scroll(function(event) {
		var scroll_left = $( window ).scrollLeft();
		//console.log(  );
		
		if (scroll_left < 200) {
			$('.head .logo img').stop().animate({left:185}, 500);
			$('.head .logo').stop().animate({width:0, marginRight: -10}, 500);
		}
		else if ($('.head .logo').width() == 0) {
			$('.head .logo img').stop().animate({left:0}, 800);
			$('.head .logo').stop().animate({width:185, marginRight: 40}, 800);
		}
		
		$('.foot').stop().animate({left: scroll_left}, 300);
		
	});

	$('div.tabs .tab').click(function() {
		if ($(this).hasClass('current')) return;

		var container = $(this).closest('div.tabs');
		var content = $(this).next('.content');
		
		$('.tab', container).removeClass('current');
		$('.content', container).hide();
		
		$(this).addClass('current');
		content.show();
	});
	
	var local_scroll_settings = {easing:'easeOutExpo', onBefore:function(e,o){ $.address.value($(o).attr('id')); }};
	$.localScroll.defaults.axis = 'x';
	$.localScroll(local_scroll_settings);
	
	//$.localScroll.hash(local_scroll_settings);
	
	$.address.init(function(event) {
		try {
			var id = event.value.replace('/','').replace('#', '');
			var pos_left = parseInt($('#'+id).css('left'));
			$.scrollTo({top:0, left: pos_left + 100});
		}
		catch(err) {}
	});
	
	$.address.change(function(event) {

		try {
			var id = event.value.replace('/','').replace('#', '');
			$('.head .nav > li').removeClass('current');
			$('li.' + id).addClass('current');
			$(window).scrollTop(0);
		}
		catch(err) {}
	});
	
	$("a[rel=project]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">' + title + '</span>';
		}
	});
	$("a[rel=popup]").fancybox({
		'type'				: 'iframe',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titleShow' 		: false,
		'width'				: 580,
		'height'			: 500,
		'showNavArrows'		: false
	});
	$("a[rel=big]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titleShow' 		: false,
		'showNavArrows'		: false
	});
	
	var menu_contact_interval = null;
	
	//$('.head .nav > li').click(function(){ $('.head .nav > li').removeClass('current'); $(this).addClass('current'); });
	
	$('li.contact, li.careers').hover(
		function(){
			$(this).parent().find(">li").each(function(){
				$(this).removeClass('open');
				$('.content', this).hide();
			});
			
			$(this).addClass('open');
			$('.content', this).show();
			clearInterval(menu_contact_interval);
		},
		function() {
			var self = this;
			menu_contact_interval = setInterval(function(){
				$('.content', self).hide();//slideUp(function(){
				$(self).removeClass('open');
				//});
				clearInterval(menu_contact_interval);
		}, 500);
	});
	
	// map
	$("a.inline-map").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'titleShow' 		: false,
		'overlayShow'	:	false
	});
	
});

