$(function() {


	//make it scroll
	var margin = 10;
	var	imageWidth = 90;

	var images = Array();
	var index = 0;
	var pos = 0;
	var busy = false;
		
	function getImages() {
		$('#scrollarea img').each(function() {
			images.push(this);
		})
	}
		
	function align(animate) {
		for (i=0;i<images.length;i++){				
			if(animate)
				$(images[i]).animate({'marginLeft':pos});
			else
				$(images[i]).css('marginLeft',pos);
			pos += imageWidth+margin;
		}
	}
	
	
	function bindAnimation() {
		$('#scrollarea img').bind('click', function(e) {
				//order graphics
			if(busy == false){
					busy = true;
					
					var currentBanner = $('#col-2 #banner img:first');
					var newsrc = $(this).attr('src').replace('_small','_wide');
					var newBanner = '<img src="'+newsrc+'" height="470" />';
								
					if(images.length>2) {
							$(this).animate({'marginTop':-60}, { queue: false, duration: 500, easign: "swing", complete: function() {
								
								$(this).css('marginTop',-60);
								$(this).css('marginLeft',pos);
								$(this).css('marginTop',0);
								
								//show big images
								$('#col-2 #banner').prepend(newBanner);
										
								$(currentBanner).fadeOut("slow", function() {
									$(currentBanner).remove();
								});
				
								//search index
								for (i=0;i<images.length;i++){
									if(this == images[i])
										var index = i;
								}
										
								//reorder array
								images.splice(index,1)
								images.push(this);
				
								pos = 0;
								align(true);
								busy = false;
						}});
						e.stopPropagation();
				}else{
						$('#col-2 #banner').prepend(newBanner);
								
						$(currentBanner).fadeOut("slow", function() {
							$(currentBanner).remove();
						});
						busy = false;
				}
			}
			
		});
	}
		
	getImages();
	align(false);
	bindAnimation();
	
	//front page
	$('.projecttype-nav li a').each(function() {
		var catid = $(this).attr('id').split('-')[1];

		$(this).bind('mouseover', function() {
			//$('.projecttype-nav li').removeClass('hover');
			
			//$(this).parent().addClass('hover');
			$('#banner a img:not(.p-f-'+catid+')').each(function() {
				$(this).parent().parent().children('.project-front-image-red').fadeIn('fast');
			});
		});
		
		$(this).bind('mouseout', function() {
			resetFrontPageImages();
		});
	});
	
	function resetFrontPageImages() {
		$('.project-front-image-red').each(function() {
			$(this).hide();
		});
	}
	
	
	$('#banner img').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	
	$('#project-detail #col-3-bottom img').click(function() {
		$('#map').css('display','none');
		var src = $(this).attr('src').replace('_projectextrasmall','_projectextralarge');
		$('#col-2').css('background-image', 'url(' + src + ')');
		$('#col-3').css('background-image', 'url(' + src + ')');
		$('#col-3').css('background-position', '-614px 0px');

	});
	
	
	$('#gmap').bind('click', function() {
		$('#map').css('display','block');
		initialize();
	});
	
	$('#morebtn').bind('click', function() {

		if ($('#moretext').is(':visible')) {
    	$('#moretext').hide();
		} else {
			$('#moretext').show();
		}
	});
	
	
	$('#show_googlemap').bind('click', function() {
	
		$('#gmap_overview').fadeIn();	initialize();
		$('#show_googlemap').hide();
		$('#hide_googlemap').show();
	});
	
	
	$('#hide_googlemap').bind('click', function() {
		$('#gmap_overview').fadeOut();
		$('#hide_googlemap').hide();
		$('#show_googlemap').show();
	});
	
	
	
})