function updateHash(page, tag) {
	var hash = '#'+escape(tag)+"&"+page;
	window.location.hash = hash;
}

function fixProjects() {
	var hash = window.location.hash.substr(1);
	hash = hash.split('&');
	
	var tag = unescape(hash[0]);
	var page = hash[1];
	
	if (isNaN(page)) {
		page = 1;
	}
	
	$('.project').each(function(){
		$(this).show();
		if (tag!='') {
			var show = false;
			$(this).children('.work_info').children().each(function(){
				if (tag==$(this).html()) {
					show = true;
				}
			});
			if (!show) {
				$(this).hide();
			}
		}
	});
	
	var numpages = Math.ceil($('.project:visible').length / 5);
	var lis = "";
	$('.pagination').html('').hide();
	if (numpages>1) {
		lis = "<ul>";
		for (var i=1; i<=numpages; i++) {
			lis += '<li'+(i==1?' class="first"':'')+(i==numpages?' class="last"':'')+'>'+i+'</li>';
		}
		lis += "</ul>";
		$('.pagination').html(lis).show();
	}
	
	$('select[name="tags"]').children().each(function(){
		if ($(this).val()==tag) {
			$('select[name="tags"]').children().removeAttr('selected');
			$(this).attr('selected', 'selected');
			$('select[name="tags"]').trigger("liszt:updated");
		}
	});
	
	$('.pagination ul li').each(function(){
		if (parseInt($(this).html())==page) {
			$(this).addClass('selected');
		}
	});
	
	var offset = (page - 1) * 5;
	var counter = 0;
	$('.project:visible').each(function(){
		if (counter<offset || counter>=(offset+5)) {
			$(this).hide();
		}
		counter++;
	});
	
	document.title = (tag==''?'':tag+' | ')+"Projekt | MWi AB";
}

$(document).ready(function(){
	$('.project_image').hover(
		function(){
			$(this).children('.work_visit').stop(true, true).css({'opacity': '0'}).show().animate({'opacity': '0.8'}, 'slow');
			//$(this).children('.work_visit').stop(true, true).hide().fadeIn('slow');
		},
		function(){
			$(this).children('.work_visit').stop(true, true).animate({'opacity': '0'}, 'slow', function(){
				$(this).hide();
			});
			//$(this).children('.work_visit').stop(true, true).show().fadeOut('slow');
		}
	);
	
	$('#projects .project_thumbnails .thumbnail').append('<div class="view_thumbnail">');
	$('.thumbnail').hover(
		function(){
			$(this).children('.view_thumbnail').stop(true, true).fadeIn('slow');
		},
		function(){
			$(this).children('.view_thumbnail').stop(true, true).fadeOut('slow');
		}
	);
	
	$('.view_thumbnail').mouseenter(function(){
		var img = $(this).parents('.project_info').siblings('.project_image').children('img');
		$(img).hide().attr('src', $(this).siblings('span').children('img').attr('src').replace('/thmb/', '/org/')).fadeIn(700);
	});
	
	$(".toggler").click(function(){
		$('.hist_content').slideToggle();
	});
	
	$(".twittermikael").tweet({
		username: "mwahss",
		join_text: "auto",
		avatar_size: 40,
		count: 1,
		auto_join_text_default: "",
		auto_join_text_ed: "",
		auto_join_text_ing: "",
		auto_join_text_reply: "",
		auto_join_text_url: "",
		loading_text: "laddar tweets..."
	});
	
	$(".twittermartin").tweet({
		username: "martinberggren",
		join_text: "auto",
		avatar_size: 40,
		count: 1,
		auto_join_text_default: "",
		auto_join_text_ed: "",
		auto_join_text_ing: "",
		auto_join_text_reply: "",
		auto_join_text_url: "",
		loading_text: "laddar tweets..."
	});
	
	$('.select_project').show();
	
	$('select[name="tags"]').change(function(){
		updateHash(1, $(this).val());
	});
	
	$('select[name="tags"]').chosen();
	
	$('.project_tag').click(function(e){
		e.preventDefault();
		
		var tag = $(this).html();
		$('select[name="tags"]').children().each(function(){
			if ($(this).val()==tag) {
				$('select[name="tags"]').children().removeAttr('selected');
				$(this).attr('selected', 'selected');
				$('select[name="tags"]').trigger("liszt:updated");
				$('select[name="tags"]').trigger('change');
			}
		});
	});
	
	$(".contacting").validate({
		rules:		{
						epost:		{
										email: true,
										required: true
									},
						meddelande:	{
										required: true
									}
					},
		messages:	{
						epost:		"Du m&#229;ste skriva en giltig e-post",
						meddelande:	"Lite v&#228;l kortfattat :)"
					}
	});
	
	$('input[name="cm-tdkujr-tdkujr"]').focus(function(){
		if ($(this).val()=='Din e-postadress') {
			$(this).val('');
		}
	});
	
	$('input[name="cm-tdkujr-tdkujr"]').blur(function(){
		if ($(this).val()=='') {
			$(this).val('Din e-postadress');
		}
	});
	
	$('.work_info a').click(function(e){
		e.preventDefault();
	});
	
	$('.pagination ul li').live('click', function(){
		var page = parseInt($(this).html());
		updateHash(page, $('select[name="tags"]').val());
	});
	
	$('.service').hover(
		function(){
			$(this).children('.back_to_top').show();
		},
		function(){
			$(this).children('.back_to_top').hide();
		}
	);
	
	$('.back_to_top').click(function(){
		$.scrollTo('#header', 'slow');
	});
	
	$('#services_navigation ul li a').click(function(e){
		e.preventDefault();
		
		var href = '#'+$(this).attr('href').replace("/tjanster/", "");
		
		if ($('.seo_special').length==0) {
			$.scrollTo(href, 'slow', function(){
				var img = $(href).children('.service_info').children('img');
				$(img).toggle('blind', {'direction': 'horizontal'}, 'slow', function(){
					$(document).bind('scroll.services', function(){
						$(img).toggle('blind', {'direction': 'horizontal'}, 'slow');
						$(document).unbind('scroll.services');
						$(window).unbind('scroll.services');
					});
					
					$(window).bind('scroll.services', function(){
						$(img).toggle('blind', {'direction': 'horizontal'}, 'slow');
						$(document).unbind('scroll.services');
						$(window).unbind('scroll.services');
					});
				});
			});
		}
		else {
			window.location.href = '/tjanster'+href;
		}
	});
	
	$('.project_info h2').each(function(){
		var html = $(this).children('a').html();
		$(this).html(html);
	});
	
	if ($('.select_project').length>0) {
		$(window).hashchange(function(){
			fixProjects();
		});
		
		$(window).hashchange();
	}
	else {
		$('.project').show();
	}
});
