/*                                     JQUERY ON DOC READY
__________________________________________________________
*/

$(document).ready(function(){



	/* ---SMOOTH SCROLL----------------------------- */
	
	$('div#navbar a').smoothScroll({
        afterScroll: function() {
        	/* location.hash = this.hash; */
        }
    });
    
    $('div#projekt a').smoothScroll({
        afterScroll: function() {
        	/* location.hash = this.hash; */
        }
    });

   $('div#work a').smoothScroll({
        afterScroll: function() {
        	/* location.hash = this.hash; */
        }
    });
    
    $('div#services a').smoothScroll({
        afterScroll: function() {
        	/* location.hash = this.hash; */
        }
    });   

    $('div#home a').smoothScroll({
        afterScroll: function() {
        	/* location.hash = this.hash; */
        }
    });       

	/* ---NAV BAR ANIMATION------------------------- */

   	var w = $(window).width();
    
   	var logo = (w/2)-1599;
   	var omoss = (w/2)-1352;
   	var projekt = (w/2)-1252;
   	var vadvigjort = (w/2)-1154;
   	var vadvikan = (w/2)-1050;
   	var kontakt = (w/2)-946;
   	
   	var lastclicked = 1599;
	
    $('div#navbar').css({backgroundPosition: logo +'px 0'});
    
    
    
    /* ---FOOTER HEIGHT FIX------------------------- */
    
    var h = $(window).height();
    $("div#contact").css("min-height", h - 334 +'px');



	/* ---NAV BAR ANIMATION------------------------- */
	
	function isScrolledIntoView(elem) {
        var docViewTop = $(window).scrollTop();
        var docViewBottom = docViewTop + $(window).height();

        var elemTop = $(elem).offset().top;
        var elemBottom = elemTop + $(elem).height();

        return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
    }

    $(window).scroll(function() {
        if(isScrolledIntoView('#home')) {
        	$('div#navbar').stop().animate({backgroundPosition: logo +'px 0'}, {duration:300});
       		lastclicked = 1599;
        } else if(isScrolledIntoView('#about')) {
        	$('div#navbar').stop().animate({backgroundPosition: omoss +'px 0'}, {duration:300});
       		lastclicked = 1352;
        } else if(isScrolledIntoView('#projekt')) {
        	$('div#navbar').stop().animate({backgroundPosition: projekt +'px 0'}, {duration:300});
       		lastclicked = 1252;
        } else if(isScrolledIntoView('#work')) {
            $('div#navbar').stop().animate({backgroundPosition: vadvigjort +'px 0'}, {duration:300});
       		lastclicked = 1154;
        } else if(isScrolledIntoView('#services')) {
       		$('div#navbar').stop().animate({backgroundPosition: vadvikan +'px 0'}, {duration:300});
        	lastclicked = 1050;
        } else {
        	$('div#navbar').stop().animate({backgroundPosition: kontakt +'px 0'}, {duration:300});
        	lastclicked = 946;
        }
    });



	/* ---BROWSER RESIZE - NAVBAR FIX--------------- */
	
    $(window).resize(function() {
    	var w = $(window).width();
        
   	var logo = (w/2)-1599;
   	var omoss = (w/2)-1352;
   	var projekt = (w/2)-1252;
   	var vadvigjort = (w/2)-1154;
   	var vadvikan = (w/2)-1050;
   	var kontakt = (w/2)-946;
    	
		$('div#navbar').css({backgroundPosition: (w/2)-lastclicked +'px 0'});
		
		$(window).scroll(function() {
	        if(isScrolledIntoView('#home')) {
	        	$('div#navbar').stop().animate({backgroundPosition: logo +'px 0'}, {duration:300});
	       		lastclicked = 1599;
	        } else if(isScrolledIntoView('#about')) {
	        	$('div#navbar').stop().animate({backgroundPosition: omoss +'px 0'}, {duration:300});
	       		lastclicked = 1352;
	        } else if(isScrolledIntoView('#projekt')) {
                $('div#navbar').stop().animate({backgroundPosition: projekt +'px 0'}, {duration:300});
                lastclicked = 1252;
            } else if(isScrolledIntoView('#work')) {
	            $('div#navbar').stop().animate({backgroundPosition: vadvigjort +'px 0'}, {duration:300});
                lastclicked = 1154;
	        } else if(isScrolledIntoView('#services')) {
                $('div#navbar').stop().animate({backgroundPosition: vadvikan +'px 0'}, {duration:300});
                lastclicked = 1050;
	        } else {
	        	$('div#navbar').stop().animate({backgroundPosition: kontakt +'px 0'}, {duration:300});
	        	lastclicked = 946;
	        }
        });
        
        
	    // Footer height fix after resize
	    var h = $(window).height();
    	$("div#contact").css("min-height", h - 334 +'px');
	    
    });



	/* ---HOMEPAGE FANCYBOX------------------------- */
	
	$("a[rel=thumbs]").fancybox({
		'titleShow': true,
		'titlePosition'	: 'over'
	});



	/* ---FADE BIO ON HOVER------------------------- */

	$("div#fadebox").hover(function(){
		$("div#bio").stop().fadeTo(600, 1);
	},function(){
		$("div#bio").stop().fadeTo(800, 0);
	});
	
	$('div#bio').hide();



	/* ---LAYOUT SWITCH----------------------------- */
	
	$("a#switch").toggle(function(){
	
		$("a#switch").css("background-position", "bottom left");
		$("div#work, div#services, div#contact").css("width", "auto");
		$("div#container").stop().animate({width: "850px"}, 300);
		$("div#bio").css("margin-left", "435px");
		$("div.webwork, div.logowork").css("margin-left", "21px");
		$("div#bio").stop().fadeTo(800, 1);
		
		$("div#fadebox").hover(function(){
			$("div#bio").stop().fadeTo(600, 1);
		},function(){
			$("div#bio").stop().fadeTo(0, 1);
		});
		
		$("a#switch").hover(function(){
		$("a#switch").css("background-position", "bottom right");
			},function(){
		$("a#switch").css("background-position", "bottom left");
		});
		
		$("div#contact_form").css("width", "826px");
		$("div#contact_form label input").css("width", "393px");
		$("div#contact_form label textarea").css("width", "810px");
		
	},function(){

		$("a#switch").css("background-position", "top left");
		$("div#container").stop().animate({width: "420px"}, 300);
		$("div#work, div#services, div#contact").stop().animate({width: "390px"}, 300);
		$("div#bio").css("margin-left", "5px");
		$("div#bio").css("background-image", "url(img/bg-bio.gif)");
		$("div.webwork, div.logowork").css("margin-left", "0px");
		$("div#bio").hide();

		$("div#fadebox").hover(function(){
		$("div#bio").stop().fadeTo(600, 1);
			},function(){
		$("div#bio").stop().fadeTo(800, 0);
		});
		
		$("a#switch").hover(function(){
		$("a#switch").css("background-position", "top right");
			},function(){
		$("a#switch").css("background-position", "top left");
		});
		
		$("div#contact_form").css("width", "auto");
		$("div#contact_form label input").css("width", "175px");
		$("div#contact_form label textarea").css("width", "374px");
		
	});



	/* ---SORT WORK DROPDOWN------------------------ */

	$('div.logowork').hide();
	$('select#sortwork').val('webwork');
	
	$('select#sortwork').change(function() {
	    $('div.webwork').hide();
	    $('div.logowork').hide();
	    $('div.' + $(this).val()).stop().fadeTo(600, 1);
	});



	/* ---FADE CAPTION ON HOVER--------------------- */	

	$('div.webwork, div.logowork, div.prowork').hover(function(){
		$(this).children('div.caption').stop().fadeTo(400, 1);
	},function(){
		$(this).children('div.caption').stop().fadeTo(600, 0);
	});
	
	$('div.caption').hide();



	/* ---ALL EXTERNAL LINKS OPEN IN NEW WINDOW----- */	
	
	jQuery("a[href^='http:']").not("[href*='izg.se']").attr('target','_blank');

	/* ---SPAM BLOCKER------------------------------ */	

	$('a#email').each(function(){
		e = this.rel.replace('/','@').replace('site','izg.se');
		this.href = 'mailto:' + e;
		// $(this).text(e);
	});

	/* ---KONTAKT FORM------------------------------ */
	
	$('form#ajax_form .submit').click(function(){

		$('#ajax_form .error').hide();	//If error visibile, hide on new click
		
		var name = $('input#name').val();
		if (name == "" || name == " " || name == "Namn") {
		    $('#form_contain').focus().before('<div class="error"><p>Namn, vem är du!?</p></div>');
		    return false;
		}
		
		var email_test = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
		var email = $('input#email').val();
		if (email == "" || email == " ") {
		   $('#form_contain').focus().before('<div class="error"><p>Du glömde en.</p></div>');
		   return false;
		} else if (!email_test.test(email)) {
		   $('#form_contain').select().before('<div class="error"><p>Tror din e-post adress är fel.</p></div>');
		   return false;
		}
		
		var message = $('#message').val();
		if (message == "" || message == " " || message == "Meddelande") {
		    $('#form_contain').focus().fadeIn('slow').before('<div class="error"><p>Du glömde skriva ett meddelande</p></div>');
		    return false;
		}
		
		var data_string = $('form#ajax_form').serialize();

		$.ajax({
		    type:       "POST",
		    url:        "email.iz",
		    data:       data_string,
		    success:    function() {

		$('form#ajax_form').fadeTo(400, 0).before('<div id="success"></div>');
		$('#success').html('<p><strong>Meddelande skickat!</strong><br />Vi återkommer så snabbt vi bara kan!</p>');

		    } //End success function


		}) //End AJAX call

		return false;


	}) //End click function
	
	var current_data = new Array();

	$('.empty').each(function(i){
		$(this).removeClass('empty').addClass('empty'+i);
		current_data.push($(this).val());

		$(this).focus(function(){
			if($(this).val() == current_data[i]) {
				$(this).val('');
			}
		});
		$(this).blur(function(){
			var stored_data = current_data[i];
			if($(this).val()==''){
				$(this).val(stored_data);
			}
		})
	});



	/* ---IE FIXES---------------------------------- */
	
	if ($.browser.msie && $.browser.version.substr(0,1)<=7) {
		$("a#switch").css("margin-top", "-35px");
		$("div#extras").css("background-position", "0 150px");
	}
	
	else {
		$("a#switch").css("margin-top", "6px");
		$("div#extras").css("background-position", "0 192px");
	}
	

});



