$(document).ready(function() {
	
	var maxWidth = 1120;
	
	// 	------------------------------
	//	RESIZE WINDOW
	// 	------------------------------
	function fixWidth()
	{
		if( $(window).width() < maxWidth )
		{
			$("#header").css('position', 'absolute');
			$("body.site").css('margin', '0px 0px 0px 145px');
		}else{
			$("#header").css('position', 'fixed');
			$("#header").css('top', '20px');
			$("body.site").css('margin', '0px auto');
		}
	}
	
	function setMenuPos()
	{
		if( $(window).width() < maxWidth )
		{
			y = $(window).scrollTop() + 20;
			$("#header").css('top', y+'px');
		}
	}
	
	$(window).bind('resize', function() {
		fixWidth();
		setMenuPos();
	});
	
	$(window).bind('scroll', function() {
		setMenuPos();
	});
	
	fixWidth();
	setMenuPos();
	
	// 	------------------------------
	//	CASE STUDY
	// 	------------------------------
	
	// Set case study
	function setCaseStudy(id) {
		$('#case_studies li').removeClass('on');
		$(id).addClass('on');
		
		switch( $(id).attr("id") ) {
			case "case_1" :
				$('#case_studies .image').css('background-position','0px 0px');
			break;
			case "case_2" :
				$('#case_studies .image').css('background-position','0px -300px');
			break;
			case "case_3" :
				$('#case_studies .image').css('background-position','0px -600px');
			break;
			case "case_4" :
				$('#case_studies .image').css('background-position','0px -900px');
			break;
			case "case_5" :
				$('#case_studies .image').css('background-position','0px -1200px');
			break;
		}
	}
	
	// Mouse over Case Study
	$('#case_studies li').mouseover(function() {
		setCaseStudy( $(this) );
	});
	
	// Open up random Case Study
	function randomCaseStudy( nr ) {
		var id	= $('#case_studies li');
		var ran	= Math.floor( Math.random()*4 );
		
		if( nr!=0 ) ran = nr-1;
		
		setCaseStudy( $(id[ran]).addClass('on') );
	}
	
	
	randomCaseStudy(casestudy);
	
	// 	------------------------------
	//	SEARCH
	// 	------------------------------
	
	// Search projects
	$.expr[":"].doSearch = function(el, i, m) {
		var search = m[3];
		if (!search) return false;
		return eval("/" + search + "/i").test($(el).text());
	};  
	
	$('#search').keyup(function () {
		var search_string = $(this).val();
		
		if( !search_string ) {
			$("#verkefni .item").show();
			
			createEmptyItems();
		}else{
			$("#verkefni .item").hide();
			items = $("#verkefni .item:doSearch("+search_string+")").show();
			
			createEmptyItems( items.length );
		}
	});
	
	function createEmptyItems(nr) {
		nr = nr==null ? $("#verkefni .item").length : nr;
		
		$("#verkefni .none").remove();
		
		var calc	= 4-(nr%4);
		calc		= calc==4 ? 0 : calc;
		
		if(nr==0) calc = 4;
		
		for(var i=0; i<calc; i++){
			$("#verkefni").append("<div class='none'></div>");
		}
	}
	
	createEmptyItems();
	
	// 	------------------------------
	//	SETTINGS
	// 	------------------------------
	
	$('.settings .list_view').mouseup(function() {
		$('.settings .thumb_view').attr('id', 'off');
		$(this).attr('id', 'on');
		
		$('#verkefni').removeClass('thumb');
		$('#verkefni').addClass('list');
	});
	
	$('.settings .thumb_view').mouseup(function() {
		$('.settings .list_view').attr('id', 'off');
		$(this).attr('id', 'on');
		
		$('#verkefni').removeClass('list');
		$('#verkefni').addClass('thumb');
	});
	
	$('.settings .search').mouseup(function() {
		if( $(this).attr('id') == 'on' )
			$(this).attr('id', 'off');
		else
			$(this).attr('id', 'on');
	});
	
});

// 	------------------------------
//	VIDEO PLAYER
// 	------------------------------

function video(list, type, seek)
{
	var url = window.location.href;
	
	for(var i=0; i<list.length; i=i+2)
	{ list[i] = url+list[i]; }
	
	switch(type)
	{
		case "c1" :
			document.write('<embed pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" src="/include/player/video_player.swf?type=c1&seek='+seek+'&list='+list+'" width="394" height="145" type="application/x-shockwave-flash"></embed>');
		break;
		case "c2" :
			document.write('<embed pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" src="/include/player/video_player.swf?type=c2&seek='+seek+'&list='+list+'" width="594" height="257" type="application/x-shockwave-flash"></embed>');
		break;
		case "c3" :
			document.write('<embed pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" src="/include/player/video_player.swf?type=c3&seek='+seek+'&list='+list+'" width="794" height="360" type="application/x-shockwave-flash"></embed>');
		break;
		case "c4" :
			document.write('<embed pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" src="/include/player/video_player.swf?type=c4&seek='+seek+'&list='+list+'" width="790" height="482" type="application/x-shockwave-flash"></embed>');
		break;
	}
}