var isFirstSlide = true, isOpen = true, isFirstRun = true;

var flashvars = {};
var params = {
	allowscriptaccess: "true",
	wmode: "transparent"
};
var attributes = {};

//swfobject.embedSWF("swf/banner.swf", "flashbanner", "940", "506", "9.0.0", flashvars, params, attributes);



function openBanner() {

	$.history.load("133");
	isFirstSlide = true;
	isOpen = true;
	return false;

}

$(document).ready(function(){	
	
	$("#big_banner").bind('click', openBanner);
	
	$(".columnized").columnize({columns: 4, balance: true, dontsplit: "p,li"});
	
	//Keyboard navigation
	
	$(document).bind('keydown', 'return', function(evt){
		$('#container .thumb .thumblink.current').click();
		return false;
	});
	
	$(document).bind('keydown', 'left', function(evt){
		$('#project_display .grid_16 .orbit-wrapper .slider-nav .left').click();
		return false;
	});
	
	$(document).bind('keydown', 'right', function(evt){
		$('#project_display .grid_16 .orbit-wrapper .slider-nav .right').click();
		return false;
	});
	
	$(document).bind('keydown', 'space', function(evt){
		toggleInfoWindow();
		return false;
	});
	
	$(document).bind('keydown', 'esc', function(evt){
		$('.bottom_project_separator').css("display", "none");	
		$('.project_view').animate({
			opacity: 0,
			height: 0
	  	}, 'fast', function(){
		
			$('.project_view').hide();
				
		});
		
		goTo(0);
	});
	
	$(document).bind('keydown', 'up', function(evt){
		goTo(0);
		return false;
	});
	
	$(document).bind('keydown', 'down', function(evt){
		var about = $('#about').offset().top;
		goTo(about);
		return false;
	});
	
	//////////////////
	
	$.history.init(initSubView);
	
	$("#top_button").hide();
	
	$("#top_button").click(function(){
		goTo(0);
	});
	
	$('.tinyviewer').imagesLoaded(function() {
		$('.tinyviewer').orbit({
			bullets: false,
			animation: 'horizontal-push',
			timer : true,
			animationSpeed: 300,
		});
		
		$('.slider-nav .left').fadeOut('fast');
	});
	
	$(window).scroll(function() {
		var scroll = $(this).scrollTop();
		if(scroll > 0) {
			$("#top_button").show();
			$(".headline").addClass('shadowed');
			$(".headline").addClass('closed');
		} else if($(".headline").hasClass('shadowed')) {
			$("#top_button").hide();
			$(".headline").removeClass('shadowed');
			$(".headline").removeClass('closed');
		}
	});
	
	$(".headline").hover(function() {
		if($(this).hasClass('shadowed')) {
			$(".headline").removeClass('closed');
		}
	}, function() {
		if($(this).hasClass('shadowed')) {
			$(".headline").addClass('closed');
		}
	});
	
	$("a[rel='history']").click(function(){
		//$.history.load(this.href.replace('http://192.168.1.50/bunkerpalace/projects#', ''));
		//var maReg = new RegExp( "(?:#)[0-9]+$", "gi" ) ;
		//var maReg = /(?:#)[0-9]+$/;
		var resultat = this.href.match(/[0-9]+$/);
		$.history.load(resultat);
		isFirstSlide = true;
		isOpen = true;
		return false;
	});
	
	$(".desaturated").bind("load", function () { 
		$(this).pixastic('desaturate');
	});
	
	$('#container').isotope({
	  	masonry : {
			columnWidth: 240
		},
		hiddenStyle: {
			opacity: 0,
			scale: 1
		},
		visibleStyle: {
			scale: 1,
			opacity: 1
		},
		animationOptions: {
			duration: 750,
			easing: 'linear',
			queue: false
		},
		itemSelector : '.thumb'
	});
	
	
	$('.filters li a').click(function(event){
		
		event.preventDefault();
		
		if ($(this).attr('rel') != 'no_ajax') {

			// Scroll to projects
			if ($.browser.safari) {
				var animationSelector = 'body:not(:animated)';
			} else {
				var animationSelector = 'html:not(:animated)';
			}
			
			var elem = $(this);
			var scroll = $(window).scrollTop();
			var time = scroll == 0 ? 0 : 500;
			
			$(animationSelector).animate({scrollTop: 0}, time, 'swing', function() {
				$('.filters li').addClass('unselected').removeClass('selected');
						elem.parent().removeClass('unselected').addClass('selected');
						
						// Tri Isotope
						var selector = elem.attr('data-filter');
						if(selector == '.all') {
							$(".banner").slideDown();
						} else {
							$(".banner").slideUp();
						}
						
						$('#container').isotope({ filter: selector });
						
						$('#container').isotope('reLayout'); 
						
						// Animations layout
						$('.project_view').animate({
							opacity: 0,
							height: 0
					  	}, 'fast').hide();;
				
						$('.bottom_project_separator').css("display", "none");
			});

			return false;
			
		} else {
			
		}
	});
	
	$('a[href*=#]').click(function(e) {
		e.preventDefault();
		
		// skip SmoothScroll on links inside sliders or scroll boxes also using anchors or if there is a javascript call
		if($(this).attr('class')=='filter_trigger' || $(this).attr('class') == 'thumblink' || $(this).attr('href').indexOf('javascript')>-1) return;

		// duration in ms
		var duration=500;

		// easing values: swing | linear
		var easing='swing';

		// get / set parameters
		var newHash=this.hash;
		var oldLocation=window.location.href.replace(window.location.hash, '');
		var newLocation=this;
		
		// make sure it's the same location		
		if(oldLocation+newHash==newLocation)
		{
			// get target
			var target=$(this.hash+', a[name='+this.hash.slice(1)+']').offset().top;

			// adjust target for anchors near the bottom of the page
			if(target > $(document).height()-$(window).height()) target=$(document).height()-$(window).height();			
			
			// set selector
			if($.browser.safari) var animationSelector='body:not(:animated)';
			else var animationSelector='html:not(:animated)';
			
			// animate to target and set the hash to the window.location after the animation
			$(animationSelector).animate({ scrollTop: target }, duration, easing, function() {

				// add new hash to the browser location
				window.location.href=newLocation;
			});

			// cancel default click action
			return false;
		}
	});
	
	var bunkerStyles = [
	/*{
	  featureType: "all",
	  stylers: [
	    { lightness: "-60" },
	    { saturation: "100" },
	    { hue: "#0bd66a" }
	  ]
	  
	}*/
	{ elementType: "geometry", stylers: [ { hue: "#0bd66a" }, { saturation: 100 }, { lightness: -60 } ] },{ featureType: "road", elementType: "geometry", stylers: [ { hue: "#0bd66a" }, { lightness: -40 }, { saturation: 100 } ] },{ featureType: "road", elementType: "labels", },{ elementType: "labels", stylers: [ { saturation: -95 } ] }
		];
		var bunkerMapType = new google.maps.StyledMapType(bunkerStyles,
	{name: "Plan"});
	
	var myLatlng = new google.maps.LatLng(49.478556,6.085329);
	var myOptions = {
		zoom: 18,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		panControl: false,
    	zoomControl: true,
    	scaleControl: false,
    	streetViewControl: false,
    	zoomControlOptions: {
			style: google.maps.ZoomControlStyle.SMALL,
			position: google.maps.ControlPosition.TOP_LEFT
		}
	};
	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	
	map.setOptions({styles: bunkerStyles});
		
	var image = '/img/puce.png';
	var myLatLng = new google.maps.LatLng(49.478556,6.085329);
	var beachMarker = new google.maps.Marker({
		position: myLatLng,
		animation: google.maps.Animation.DROP,
		map: map,
		icon: image
	});


});

function initSubView(hash) {
	
	var reg = new RegExp("[filter]","g");
	var hashstr = hash + "";
	
	if (hash && hash != "services" && hash != "about" && hash != "contact" && hash != "toppage" && !hashstr.match(reg)) {
		if ($(this).attr('href') != '') {
		
			// Scroll to projects
			if ($.browser.safari) {
				var animationSelector = 'body:not(:animated)';
			} else {
				var animationSelector = 'html:not(:animated)';
			}
			
			$(animationSelector).animate({scrollTop: $('#project_anchor').offset().top - 40}, 500, 'swing');
		
		
			// Init Project view
			//$url = $(this).attr('href');
			//$url = 'http://192.168.1.50/bunkerpalace/projects/display/' + hash;
			$url = '/projects/display/' + hash;
			//event.preventDefault();
			
			$('.project_view').animate({
				left: '-200px',
				opacity: 0
			
		  	}, 'fast', function() {
			
				$('.project_view').show();
			
				$('.project_view').css("left", "960px");
		
				$('.project_view').animate({
				    height: 705,
					opacity: 1
				}, 'fast', function() {
					
					$('.loader').show();
					$('.bottom_project_separator').css("display", "block");
				
					$('.project_view').load($url, function() {
					
						initProjectView();
				
					});
				});
			
			});
		
		} else {
		
		
		}
	}
}

function initProjectView() {
	
	$(".close_btn").hide();
	$('.loader').hide();

	$('.project_view').animate({
		opacity: 1,
		left: 0
	}, 'fast', function(){
		$('.project_view').show();
	});
	
	$('.viewer').imagesLoaded(function() {
		
		$('.viewer').orbit({
			bullets: false,
			animation: 'horizontal-push',
			timer : false,
			animationSpeed: 300,
			beforeSlideChange: function(active, number, direction) {
				if (jwplayer()!= undefined) {
					var j = $('div[id*="videospace"]').length;	
					for (var i = 0; i < j; i++) {
						jwplayer(i).stop();
					}
				}
				
				if (isFirstSlide == true) {
					$(".project_info_container").slideToggle(300);
					$(".close_btn").show();
					isFirstSlide = false;
					isOpen = false;
				};
				
				if(active+direction == number-1) {
					$('.slider-nav .right').fadeOut('fast');
				} else {
					$('.slider-nav .right').fadeIn('fast');
				}
				if(active+direction == 0) {
					$('.slider-nav .left').fadeOut('fast');
				} else {
					$('.slider-nav .left').fadeIn('fast');
				}
			}
		});
		$('.slider-nav .left').fadeOut('fast');
	});
	
	$('#hitarea').click(function(event){
		event.preventDefault();
		toggleInfoWindow();
	});
	
	$('.close_btn').click(function(event){
		event.preventDefault();
		toggleInfoWindow();
	});
	
}

function toggleInfoWindow() {

	isFirstSlide = false;

	if (isOpen == true) {
		$(".project_info_container").slideToggle(300);
		$(".close_btn").show();
		isOpen = false;
	} else {
		$(".project_info_container").slideToggle(300);
		$(".close_btn").hide();
		isOpen = true;
	}

}

function goTo(target) {
	if ($.browser.safari) {
		var animationSelector = 'body:not(:animated)';
	} else {
		var animationSelector = 'html:not(:animated)';
	}
	$(animationSelector).animate({scrollTop: target}, 500, 'swing');
}




