$(document).ready(function(){
	var path = '/';
	
	$(".info").css({display: 'none'});
	
	$(".imageBlock").prepend("<span></span>");
	
	//$(".imageBlock em").css({display: 'none'});
	
	//$("h1, h2, h3").not(".bquote").jHeader();

	$('#kontakt #overlay, #kontakt #map').hide();
	
	$('#success').fadeTo(2000, 1).fadeOut("normal", function(){$(this).remove()});
	
	$('#mottagare').selectbox();
	
	var i = (Math.floor(numFiles*Math.random())) + 1;
	var tot = numFiles;
	
	$(".quote").css('background-image','url('+path+'images/quotes/quote'+i+'.gif)'); 
	
	$(".face").mouseover(function(){
		$(".info").each(function(){ $(this).css({display: 'none'}); });
		//alert($(this).attr('alt'));
		$(".info."+$(this).attr('id')).fadeTo(500, 1).css({display: 'block'});
		$(this).css('background-image','url('+path+'images/faces/'+$(this).attr("id")+'_over.gif)');
	}).mouseout(function(){ $(".info."+$(this).attr('id')).fadeOut();$(this).css('background-image','url('+path+'images/faces/'+$(this).attr("id")+'.gif)'); });
	
	//$(".imageBlock").mouseover(function(){
	//	$('em', this).fadeIn("fast");
	//}).mouseout(function(){$('em', this).fadeOut("fast"); });
	
	$('#mapBtn').click(function(){
		$('#overlay').fadeTo(0, 0.5).show().css({height: $(document).height(), width: $(window).width()});
		$('#map').fadeIn();
		
		return false;
	});
	$('#overlay').click(function(){
		$('#map').fadeOut("normal", function(){$('#overlay').hide()});
	});
	
	$('a.image').click(function(){
		$.ajax({
		   type: "GET",
		   url: "includes/showImages.php",
		   data: "img="+$(this).attr('rel'),
		   success: function(msg){
		   	var scroll = getScrollXY();
		     	$('body').append('<div id="imgOverlay">'+msg+'</div>');
		    	$('#overlay').fadeTo(0, 0.5).css({height: $(document).height(), width: $(document).width()});
		    	//alert(scroll);
		    	$('#imgOverlay #holder').css({top: (scroll + ($(window).height() / 2)) - (($('#holder').height()/2) + 10)});
		    	$("#images").easySlider();
		    	
		    	$('#images li img, #overlay').click(function(){
		    		$('#holder').fadeOut("normal", function(){$('#imgOverlay').remove()});
		    	});
		   }
		 });
		 return false;
	});
	
	function getScrollXY() {
	    var x = 0, y = 0;
	    if( typeof( window.pageYOffset ) == 'number' ) {
	        // Netscape
	        x = window.pageXOffset;
	        y = window.pageYOffset;
	    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	        // DOM
	        x = document.body.scrollLeft;
	        y = document.body.scrollTop;
	    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	        // IE6 standards compliant mode
	        x = document.documentElement.scrollLeft;
	        y = document.documentElement.scrollTop;
	    }
	    return y;
	}
	
	$('form').submit(function(){
		var valid = 1;
		
		if($('#errors'))
		{
			$('#errors').remove();
			$('input').each(function(){
				$(this).css("border-color", "#CCC");
			});
		}
		
		var errors = '<ul id="errors">';
		
		if($('#name').val().length < 2)
		{
			errors += '<li>Du måste skriva in ett namn.</li>';
			$('#name').css("border-color", "#FF6600");
			valid = 0;
		}
		if($('#amne').val().length < 2)
		{	
			errors += '<li>Du måste skriva ett ämne.</li>';
			$('#amne').css("border-color", "#FF6600");
			valid = 0;
		} 
		if($('#meddelande').val().length < 10)
		{	
			errors += '<li>Du måste skriva ett meddelande.</li>';
			$('#meddelande').css("border-color", "#FF6600");
			valid = 0;
		} 
		if(!$('#epost').val().match(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/))
		{
			errors += '<li>Din epostadress är felaktig.</li>';
			$('#epost').css("border-color", "#FF6600");
			valid = 0;
		}
		
		errors += '</ul>';
		
		if(valid == 0)
		{
			$('#kontakta_oss').before(errors);
			return false;
		}
	});
	
	$('ul#portfolioMenu a').click(function() {  
        $(this).css('outline','none');  
        $('ul#portfolioMenu .current').removeClass('current');  
        $(this).parent().addClass('current');  
         
        var filterVal = $(this).text().toLowerCase().replace(' ','-');  
        
        if(filterVal == 'alla') {  
            $('.imageBlock.hidden').fadeIn('slow').removeClass('hidden');  
        } else {  
            $('.imageBlock').each(function() {  
                if($(this).hasClass(filterVal) == false) {  
                    $(this).fadeOut('normal').addClass('hidden');  
                } else {  
                    $(this).fadeTo(500, 1).fadeIn('slow').removeClass('hidden');  
                }  
            });  
        }  
          
        return false;  
    }); 
	
	
	var int = setInterval(function()
	{
		if(i >= (tot+1))
		{
			i = 1;
		}
		
		$(".quote").fadeOut(1000, function()
		{
			$(".quote").css('background-image','url(images/quotes/quote'+i+'.gif)');
			$(".quote").fadeIn(1000,function(){ i++; });
		}); 

	}, 8000);
	
});