jQuery(function($){
	$(function(){
		$('#count1').show().animate({opacity:1}, 0);
		
		$('.wphoto').each(function(i){
			if(i!=0){
				$(this).css('opacity', .3);
			}
			$(this).attr('rel', i);
		});
		
		var leftpad = ($(window).width()-980)/2-0;
		$('#scroller #scrollbar').css('paddingLeft', leftpad);
		
		var currentItem = 0;
		var maxItems = $('.galleryitem').size();
		$('#picdesc').text($(".wphoto[rel='"+currentItem+"']").attr('title'));
		
		var startScroll = function(toNum){
			var nextEl = $('a.galleryitem[rel=' + toNum + ']');
			
			// OPTIONS GO HERE WINTERNET STYLE
			$('#scroller').scrollTo(nextEl, {
				'duration': 300,
				'offset': {left: (-($(window).width()-980)/2+0)},
				'axis' : 'x'
			});
			
			showCurrentText ();
		};
		
		
		$(document).keydown(function(e){
			if (e.keyCode==37) {
				slideleft();
			} else
			if (e.keyCode==39) {
				slideright();
			}
		});
		
		function slideleft() {
			// $(this).animate({'opacity':1}, 500);
			if( currentItem > 0 ) {
				$(".wphoto[rel='"+currentItem+"']").animate({'opacity':.3}, 500);
				currentItem--;
				startScroll(currentItem);
				$(".wphoto[rel='"+currentItem+"']").animate({'opacity':1}, 500);
			}
		};
		
		function slideright() {
			// $(this).animate({'opacity':1}, 500);
			if( currentItem < maxItems - 1 ) {
				$(".wphoto[rel='"+currentItem+"']").animate({'opacity':.3}, 500);
				currentItem++;
				startScroll(currentItem);
				$(".wphoto[rel='"+currentItem+"']").animate({'opacity':1}, 500);
			}
		};
		
		function showCurrentText () {
			$('#picdesc').fadeOut(function(){
				if ($(".wphoto[rel='"+currentItem+"']").attr('title') != "") {
					$(this).text($(".wphoto[rel='"+currentItem+"']").attr('title')).fadeIn()}
				}
			);
		} 
		
		$('#scroll-left').click(slideleft);
		
		$('#scroll-right').click(slideright);
		
		$('.single').click(function(){
			$('#scroller').scrollTo(this, {
				'duration': 500,
				'offset': {left: (-($(window).width()-980)/2+0)},
				'axis' : 'x'
			});
			// $(this).animate({'opacity':1}, 500);
			currentItem = $(this).attr('rel');
		});
		
		$('.wphoto').click(function(){
			if( currentItem == $(this).attr('rel')) {					
			}
			else{
				// $('.workdesc').stop().animate({'opacity':0}, 500, function () {$(this).hide()});
				$('#scroller').scrollTo(this, {
				'duration': 500,
				'offset': {left: (-($(window).width()-980)/2+0)},
				'axis' : 'x'
				});
				$(this).animate({'opacity':1}, 500);
				$(".wphoto[rel='"+currentItem+"']").animate({'opacity':.3}, 500);
				currentItem = $(this).attr('rel');
				showCurrentText ();
				return false
			}
		});
	});


// Nav Trigger
	
        $(function(){
            
            navheight = $('#nav').height();
            $('#nav').css('height', '0');
            $('#nav div').css('display', 'none');
            

            $('#menutrigger').click(function(){
                $('#nav div').css('display', 'block');
                $('#nav').animate({height: navheight + "px", marginBottom: '30px'});
                $('#menutrigger').slideUp();
                return false;
            });
            
            $('#closenav').click(function(){
                $('#nav').animate({height: '0', marginBottom: '0'}, function(){$('#nav div').css('display', 'none')});
                $('#menutrigger').slideDown();
                return false;
            });
        });
});




// DIMMNING

             
        $(document).ready(function(){
        
            $("#skugga").css("height", $(document).height()).hide();
            $(".switch").click(function(){
                if ($("#skugga").is(":hidden"))
                    $("#skugga").fadeIn("slow"),
                    $(this).html("Dimma upp").addClass("turnedOff");
                 else
                    $("#skugga").fadeOut("slow"),
                    $(this).html("Dimma ner").removeClass("turnedOff");
            });
            

           
        });
        



