window.$Life = function() {
    var $this = this;
    $(document).ready(function() {
   
        $this.initFancyBox(".photo a:first", 'elastic');
        //$this.initFancyBox("#webcam", 'elastic');
        
        $this.initFancyBox("#infobar a.imgThumb", 'elastic');
        
        $this.initMasonry('.posts', 'article', 5);
        var erectionDate = new Date(2012, 10, 1, 12);
        $this.countDownTo(erectionDate, '#countdown');
        $this.mapheight("#map");
        var colorClasses = ['green1', 'green2', 'green3', 'green4', 'green5', 'yellow1', 'orange1', 'orange2', 'orange3', 'orange4'];
        $this.randomClasses(".posts article.quote q, .posts article.link a.mainLink", colorClasses);
        $this.readMore(".readmorelink span", "p:not('.read-more')");
        $this.faderOuter(".page #sidebar");
        $this.tagClasses(".posts article .tags li");
        $this.readMoreSidebar();
        $this.smoothScroll("body.page .onPage");
    });
    $this.browserDetection = function() {
        $this.isIpad = false;
        $this.isIphone = false;
        $this.isIe = ($.browser.msie == true);
        $this.isIeLt9 = ($this.isIe && parseInt($.browser.version) < 9);
        $this.isIeLt8 = ($this.isIeLt9 && parseInt($.browser.version) < 8);
        $this.isIeLt7 = ($this.isIeLt8 && parseInt($.browser.version) < 7);
        if (!$this.isIe) {
            $this.isIpad = navigator.userAgent.match(/iPad/i) != null;
            $this.isIphone = navigator.userAgent.match(/iPahone/i) != null;
        }
    };
    $this.initFancyBox = function(selector, transition) {
        $(selector).fancybox({
            titleShow: false,
            overlayShow: true,
            overlayOpacity: 0.6,
            overlayColor: '#000',
            titlePosition: transition,
            transitionIn: transition,
            transitionOut: "fade"
        });
    };
    $this.initMasonry = function(wrapper, items, columns) {
    
    	$(wrapper).masonry({
                columnWidth: columns,
                itemSelector: items
         });
    	
    
        $(wrapper + " img").load(function() {
            $(wrapper).masonry({
                columnWidth: columns,
                itemSelector: items
            });
        });
        var $wall = $(wrapper);
       /*
 if ($wall.length != 0) {
            $wall.infinitescroll({
                navSelector: '#blogNavigation',
                nextSelector: '#blogNavigation .nextLink',
                itemSelector: '#content article:not(.first)',
                loadingImg: '../styles/img/ajax-loader.gif',
                donetext: 'No more pages to load.',
                debug: false,
                errorCallback: function() {
                    $('#infscr-loading').animate({
                        opacity: 0.8
                    }, 100).fadeOut('normal');
                }
            }, function(newElements) {
                $(this).masonry({
                    appendedContent: $(newElements)
                });
                stButtons.locateElements();
            });
        }
*/
    };
    $this.countDownTo = function(moveTime, container) {
        $(container).countdown({
            until: moveTime,
            format: 'dHM'
        });
    };
    $this.mapheight = function(mapselector) {
        var $map = $(mapselector);
        var mapurl = "http://maps.google.se/maps/ms?ie=UTF8&hl=sv&msa=0&msid=204269856992440083579.00048b305e429204d9f4b&ll=55.72247,13.285217&spn=0.524395,1.035461&z=10&output=embed";
        
        var topHeight = $("#top").height();
        var wiggleRoom = 2;
        if ($map.length) {
            $map.height($(window).height() - topHeight - wiggleRoom);
            $map.find("iframe").attr("src", mapurl);
        }
        $(window).bind("resize", function() {
            if ($map.length) {
                $map.height($(window).height() - topHeight - wiggleRoom);
                $map.find("iframe").attr("src", mapurl);
            }
        });
    };
    $this.randomClasses = function(selector, cssClasses) {
        $(selector).each(function() {
            $(this).addClass(cssClasses[Math.floor(Math.random() * cssClasses.length)]);
        });
    };
    $this.readMore = function(trigger, content) {
        $(".readmorelink").bind("click", function() {
            e.preventDefault();
        });
        var $el, $ps, $up, totalHeight;
        $(trigger).click(function() {
            totalHeight = 0;
            $el = $(this);
            $p = $el.parent();
            $up = $p.parent();
            $ps = $up.find(content);
            $ps.each(function() {
                totalHeight += $(this).outerHeight();
            });
            console.log(totalHeight);
            $up.animate({
                "height": totalHeight
            });
            $p.fadeOut();
            return false;
        });
    };
    
  	 $this.readMoreSidebar = function() {
  	 
  	 	$(".hidden").hide();
  	 
        $("a.readmore").bind("click", function(e) {
        
            e.preventDefault();
            $(this).next(".hidden").slideDown().append("<a href=\"#\" class=\"closeShown\">Stäng</a>");
            
            
            $(this).hide();
        });
        
        $("a.closeShown").live("click", function(){
        
        	$(this).parent(".hidden").prev("a.readmore").show();
        	$(this).parent(".hidden").hide("fast");
        	$(this).remove();
        
        })
        
        
        
        
    };
    
    
    $this.faderOuter = function(selector) {
        $(selector).fadeTo('fast', 0.5).hover(function() {
            $(selector).fadeTo('fast', 1);
        }, function() {
            $(selector).fadeTo('fast', 0.5);
        });
    };
    $this.tagClasses = function(selector) {
    
    $(selector).each(function(){
    
    	/*
var oldclass = $(this).attr("class");
    	var newclass = oldclass.replace(/[^a-z0-9]/i, '_'); 
*/
    	
    	$(this).attr("class", $(this).attr("class").replace(/[^a-z0-9]/i, '_'));
    
    });
    
	}
	$this.smoothScroll = function(selector){
	
	$(selector + ' a[href*=#]').click(function() {
 
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
&& location.hostname == this.hostname) {
 
var $target = $(this.hash);
 
$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
 
if ($target.length) {
 
var targetOffset = $target.offset().top;
 
$('html,body').animate({scrollTop: targetOffset}, 1000);
 
return false;
 
}
 
}
 
});
	
	};
}();

