$(document).ready(

	function()
	{
		initAccordions();
		initCarousel();
		initMailto();
		initLogImages();
		initializeMap();
		//initGridToggle();
		initProjectCarousel();
		initWork();
    }	

);


function initLogImages()
{
	var count = 0;
	
	$(".blog img").each(
	
		function(i)
		{
			var src = $(this).attr("src");
			var id = "blog-image-" + i;
			
			$(this).replaceWith('<div class="blog-image"><div id="' + id + '"></div></div>');
			
			initLogSwf( id, src );
			
		}
	);	
	
}

function initLogSwf(id,src)
{
	if ( !document.getElementById( id ) ) return;
		
	var flashvars = { src: src };
	var params = { wmode: "transparent" };
	var attributes = {};
	
	swfobject.embedSWF( "/flash/blogimage.swf", id, "312", "80", "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes );
}


function initMailto()
{
	$(".meel").html('<a href="mailto:info@angrybytes.com">info@angrybytes.com</a>');
}


function initProjectCarousel()
{
	var numItems = $(".image-carousel img").size();
	var max = numItems - 1;
	var currentIndex = 0;
	var previousIndex = 0;
	
	var ul = getPager(max);
	
	$(".image-carousel img:gt(0)").hide();
	
	$(".project-carousel .pager").prepend(ul);
	
	
	/*
		pager instellen
	*/
	
	$(".project-carousel .pager ul a").each(
		
		function( index )
		{
			$(this).click(
			
				function()
				{
					if ( index != currentIndex )
					{
						previousIndex = currentIndex;
						currentIndex = index;
						
						fade();
					}
				}				
			);
		}
	);
	
	function fade()
	{
		setHilite( ".project-carousel", currentIndex, max );
		
		if ( currentIndex != previousIndex )
		{
			$(".image-carousel img:eq(" + previousIndex + ")").fadeOut();
			$(".image-carousel img:eq(" + currentIndex + ")").fadeIn();
		}
	}
	
	/*
		next & previous
	*/
	
	$(".project-carousel .prev").addClass("dimmed");
	
	$(".project-carousel .prev").click(
		
		function()
		{
			previousIndex = currentIndex;
			currentIndex--;
			currentIndex = currentIndex <= 0 ? 0 : currentIndex;
			
			fade();
		}
	);
	
	$(".project-carousel .next").click(
		
		function()
		{
			previousIndex = currentIndex;
			currentIndex++;
			currentIndex = currentIndex >= max ? max : currentIndex;
			
			fade();
		}
	);
}

function initCarousel()
{
	/*
		settings
	*/
	
	var duration = 300;
	var rollOverEaseType = "easeOutExpo";
	var scrollEaseType = "easeInOutExpo";
	var distance = 336;
	var currentIndex = 0;
	var previousIndex = 0;
	var numItems = $(".carousel .project").size();
	var max = numItems - 3;
	
	
	/*
		pager aanmaken
	*/
	
	var ul = getPager(max);
	
	$(".carousel .pager").prepend(ul);
	
	
	
	function scroll()
	{
		setHilite( ".carousel", currentIndex, max );
		
		var x = -(currentIndex * distance) + "px";
		var scrollDuration = 400 + ( Math.abs( previousIndex - currentIndex ) * 150 );		
		
		$(".carousel-items").animate( { left: x }, { duration: scrollDuration, easing: scrollEaseType } );
	}
	
	/*
		pager instellen
	*/
	
	$(".carousel .pager ul a").each(
		
		function( index )
		{
			$(this).click(
			
				function()
				{
					if ( index != currentIndex )
					{
						previousIndex = currentIndex;
						currentIndex = index;
						
						scroll();
						
					}
				}
				
			);
		}
	);
	
	/*
		next & previous
	*/
	
	$(".carousel .prev").addClass("dimmed");
	
	$(".carousel .prev").click(
		
		function()
		{
			previousIndex = currentIndex;
			currentIndex--;
			currentIndex = currentIndex <= 0 ? 0 : currentIndex;
			
			scroll();
		}
	);
	
	$(".carousel .next").click(
		
		function()
		{
			previousIndex = currentIndex;
			currentIndex++;
			currentIndex = currentIndex >= max ? max : currentIndex;
			
			scroll();
		}
	);


	/*
		rollovers
	*/
	
	$(".carousel-items .item").each(
	
		function(i)
		{
			$(this).css( { position: "absolute", top: 0, left: (i * distance ) + "px" } );
		}
	);
	
	$(".carousel-items .box").each(
		
		function()
		{
			$(this).hover(
				
				function()
				{				
					$(this).find("div").animate( { top: "-192px" }, { duration: duration, easing: rollOverEaseType } );
				},
				
				function()
				{				
					$(this).find("div").animate( { top: "0" }, { duration: duration, easing: rollOverEaseType } );
				}
			);
			
		}
	);	
	
}

function initAccordions()
{
	/* alle accordion lists op de pagina door loopen en unieke id toewijzen */
	$(".accordion").each(
		
		function(i)
		{
			var id = "accordion" + i;
			
			$(this).attr({ id: id });
			
			/*
				vanuit de toegewezen id behaviours toewijzen
				die alleen op de betreffende accordion van toepassing zijn
				zodat accordion0 niet inklapt als je in accordion1 een panel opent
			*/
			
			id = "#" + id;
			
			/*
				alle panels sluiten behalve de 1e
			*/
			$( id + " dl:gt(0)").hide();
			
	
			
			/*
				classes toewijzen ivm layout (uitgeklapt/ingeklapt)
			*/
			$( id + " .button:gt(0)").addClass('collapsed');
			$( id + " .button:first").addClass('first');
			
			
			/*
				button behaviour
				

				- zichzelf openen wanneer niet geopend
				- huidige geopende sluiten
				- classes switchen ivm layout
			*/
			
			

			$( id + " .button").click(
			
				function()
				{			
					if( $(this).is(".collapsed") )
					{	
						$( id + " .button").addClass('collapsed');
						
						$( id + " dl:visible").slideUp("slow");
						
						$(this).next().slideDown('slow');
						
						$(this).removeClass("collapsed");
					}
					return false;
				}			
			);	
		}	
	);
}


function setHilite( root, currentIndex, max )
{
	if ( currentIndex == 0 )
	{
		$( root + " .prev").addClass("dimmed");
	} else {
		$( root + " .prev").removeClass("dimmed");
	}
	
	if ( currentIndex == max )
	{
		$( root + " .next").addClass("dimmed");
	} else {
		$( root + " .next").removeClass("dimmed");
	}
	
	$( root + " .pager ul a.current").removeClass("current");
	
	$( root + " .pager ul a:eq(" + currentIndex + ")").addClass("current");	
}

function getPager(max)
{
	var ul = $("<ul></ul>");
	
	for( var i = 0; i <= max; i++)
	{
		var a = $("<a></a>").attr( { href : "#" } ).text(i);
		
		if ( i == 0 ) a.addClass("current");
		
		var li = $("<li></li>").append( a );
		
		ul.append(li);
	}
	
	return ul;
}
	

function initWork()
{
	/*
		settings
	*/
		
	var duration = 300;
	var rollOverEaseType = "easeOutExpo";
	var scrollEaseType = "easeInOutExpo";
	var distance = 336;
	var currentIndex = 0;
	var previousIndex = 0;
	var numItems = $(".portfolio .row").size();
	var numRowsPerPage = 2;
	var rowHeight = 168;
	var pagerHeight = 56;
	var max = Math.ceil( numItems / numRowsPerPage ) - 1;
	
	
	var ul = getPager(max);
	
	var rh = ( numRowsPerPage * rowHeight )
	var h = rh + pagerHeight;
	
	$(".portfolio .pager").prepend(ul);
	$(".portfolio .pager").css( { position: "absolute", left: 0, top: (rh+16) + "px" } );
	
	$(".portfolio .row:gt(1) .project").hide();
	
	$(".portfolio").css( { height: (  h + "px") } );
	
	$(".portfolio .row").each(
	
		function(i)
		{
			var y = 16 + ((i % 2) * rowHeight ) + "px";
			
			$(".portfolio .row:eq(" + i + ") .project").each(
			
				function(j)
				{
					var x = (j * 336) + "px";
					$(this).css( { position: "absolute", left: x, top: y } );
				}
			);
			
		}	
	);
	
	
	function switchView()
	{
		setHilite( ".portfolio", currentIndex, max );
		
		var start = ( previousIndex * numRowsPerPage );
		var end = start + numRowsPerPage;
		
		
		for( var i = start; i < end; i++)
		{
			var r = ".portfolio .row:eq(" + i + ") .project";
			
			$(r).hide();
		}
		
		var start = ( currentIndex * numRowsPerPage );
		var end = start + numRowsPerPage;
		
		for( var i = start; i < end; i++)
		{
			var r = ".portfolio .row:eq(" + i + ") .project";
			
			$(r).show();
		}
		
	}
	
	/*
		pager instellen
	*/
	
	$(".portfolio .pager ul a").each(
		
		function( index )
		{
			$(this).click(
			
				function()
				{
					if ( index != currentIndex )
					{
						previousIndex = currentIndex;
						currentIndex = index;
						
						switchView();
						
						return false;
					}
				}
				
			);
		}
	);
	
	/*
		next & previous
	*/
	
	$(".portfolio .prev").addClass("dimmed");
	
	$(".portfolio .prev").click(
		
		function()
		{
			previousIndex = currentIndex;
			currentIndex--;
			currentIndex = currentIndex <= 0 ? 0 : currentIndex;
			
			switchView();
			
			return false;
		}
	);
	
	$(".portfolio .next").click(
		
		function()
		{
			previousIndex = currentIndex;
			currentIndex++;
			currentIndex = currentIndex >= max ? max : currentIndex;
			
			switchView();
			
			return false;
		}
	);
}


function initializeMap()
{
	if ( ! document.getElementById("map") ) return;
	
	if ( typeof GBrowserIsCompatible == 'function' && GBrowserIsCompatible() ) 
	{
		var map = new GMap2( document.getElementById("map") );
		map.setCenter( new GLatLng( 52.225081, 5.168738 ), 15 );

        map.addControl( new GMapTypeControl() );
        map.addControl( new GSmallMapControl() );

		var latlng = new GLatLng( 52.225081, 5.168738 );
		
		var icon = new GIcon();
		icon.image = "/img/abmarker.png";
				
		icon.iconSize = new GSize( 50, 36 );		
		icon.iconAnchor = new GPoint( 22, 42 );
		
		map.addOverlay( new GMarker(latlng, icon) );
	}
}
