
// JavaScript Document

$(function(){
	//lazy-load images
	$("img").lazyload({threshold:400, effect:"fadeIn"});
	
	//$(":focus").css({outline:"none"});
	$h = $(document).height(); 
	if ($h > 800) {$('#page').css("height","800px");$bh = $h+"px";}
	else {$('#page').css("height",$bh);}
	
	//Anti-spam emailing
	makemail('#seansemail',"spkelland@gmail.com");
	makemail('#jensemail',"jlkelland@gmail.com");
	
	//Image-click & scroll
	$last = $('#maingallery li:last').attr("id");
	
	//fade-in/out affects on pngs, so only feed it to browsers that play nice.
	if ($.support.opacity){$("#homegallery li a").removeClass(".nojs").children("span").css({display:"block",opacity:0});
		$("#homegallery li a").hover(function(){$(this).children("span").animate({opacity:1},800)},function(){$(this).children("span").animate({opacity:0},800)});
		$("#gallerynav").css({opacity:"0.3", top:"-40px"}).hover(function(){$(this).animate({opacity:1,top:"-20px"},800)},function(){$(this).animate({opacity:0.3, top:"-40px"},800)});
		$("#gallerynav li a").hover(function(){$(this).animate({opacity:0.5},800)},function(){$(this).animate({opacity:1},800)})
		$("#maingallery li a span").css({opacity:0,backgroundPosition:'95% 95%'}).hover(function(){$(this).animate({opacity:1},800)},function(){$(this).animate({opacity:0,backgroundPosition:'95% 95%'},800)});
		$("#goback").hover(function(){$(this).animate({opacity:0.5},800)},function(){$(this).animate({opacity:1},800)})
	}
	
	//Window scroll
	scrollElement = browserTest();
	
	$("#gallerynav li a").click(function(){
		$current = $(this).attr("id");
		switch($current) {
			case "gn_first" : 
				$w = 0; //offset to the beginning
				$(scrollElement).animate(
					{scrollLeft: $w},
					2000);
				break;
			case "gn_prev" :  
				$w = windowScroll(this,1);
				$(scrollElement).animate(
					{scrollLeft: $w},
					700);
				break;
			case "gn_next" : 
				$w = windowScroll(this,0);
				$(scrollElement).animate(
					{scrollLeft: $w},
					700);
				break;
			case "gn_last" : 
				$w = $("#page").width();
				$(scrollElement).animate(
					{scrollLeft: $w},
					2000);
				break;
			}
		return false;	
	});
	
	
	$("#goback").click(function() {
		$w = windowScroll(this,-1);
		$(scrollElement).animate(
			{scrollLeft: $w},
			2000);
		return false;
	})
	internalScroll();
});

function makemail($e,$email) {
		$linkemail = "<a href='mailto:"+$email+"'>"+$email+"</a>";
		$($e).children("span.email").replaceWith($linkemail);
}

function windowScroll($e,$d) {
	switch ($d) {
		case 0 : $dir = 1; break;
		case 1 : $dir = -1; break;
		default : $dir = 1;
	}	
	$win = $(window).width();
	$currOff = $($e).offset().left;
	$w = $currOff + $win * $dir - ($win - 100);
	return $w;
}

function browserTest() {
		var	scrollElement;
		$('html, body').each(function () {
        var initScrollLeft = $(this).attr('scrollLeft');
        $(this).attr('scrollLeft', initScrollLeft + 1);
        if ($(this).attr('scrollLeft') == initScrollLeft + 1) {
            scrollElement = this.nodeName.toLowerCase();
            $(this).attr('scrollLeft', initScrollLeft);
            return  false;
        }    
    	});
		return scrollElement;
	}
	
function internalScroll() {
    function filterPath(string) {
        return string
                .replace(/^\//,'')
                .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
                .replace(/\/$/,'');
    }

    var locationPath = filterPath(location.pathname);
    
    var scrollElement = 'html, body';
	scrollElement = browserTest();
		
    $('a[href*=#]').each(function() {
        var thisPath = filterPath(this.pathname) || locationPath;
        if  (   locationPath == thisPath
                && (location.hostname == this.hostname || !this.hostname)
                && this.hash.replace(/#/, '')
            ) {
                if ($(this.hash).length) {
                    $(this).click(function(event) {
						
						var offsetNow = 1;
                        var targetOffset = $(this.hash).offset().left - ($(window).width()-$(this).parent("li").next().width())/2 -20;
                        var target = this.hash;
						var duration = 1;
				
						if (location.hash) {offsetNow = $(location.hash).offset().left;}
						
						duration = Math.abs(offsetNow-targetOffset) * duration;
		
                        event.preventDefault();
                        $(scrollElement).animate(
                            {scrollLeft: targetOffset},
                            duration,
                            function() {
                                location.hash = target;
                        });
						return false;
                    });
                }
        }
    });
}
