var ShowCase = {    
    init: function(){
        this.prepareDialoges()
        this.prepareSliders()
    },
    
    prepareSliders: function(){
      $('#arrow-left').click(this.previousCase)
      $('#arrow-right').click(this.nextCase)
        
    },
    
    
    previousCase: function(event){
        event.preventDefault()
        $('#arrow-right').removeClass("disabled")
        var el = $('#showcase .showcase-item').filter('.active')
        if (!$('#showcase .showcase-item:first').is('#showcase .showcase-item.active')) {
            el.fadeOut(function(){
                $(this).removeClass('active')
                $(this).prev().fadeIn().addClass('active')
                
                    if ($('#showcase .showcase-item:first').is('#showcase .showcase-item.active')) {
                        $('#arrow-left').addClass("disabled")
                    } else {
                        $('#arrow-left').removeClass("disabled")
                    }  
            })
        }
    },
    
    nextCase: function(event){
        event.preventDefault()
        $('#arrow-left').removeClass("disabled")
        var el = $('#showcase .showcase-item').filter('.active')
        if(!$('#showcase .showcase-item:last').is('#showcase .showcase-item.active')){
            el.fadeOut(function(){
                $(this).removeClass("active")
                $(this).next('.showcase-item').fadeIn().addClass('active')
                  if ($('#showcase .showcase-item:last').is('#showcase .showcase-item.active')) {
                      $('#arrow-right').addClass("disabled")
                  } else {
                      $('#arrow-right').removeClass("disabled")
                  }
            })          
            
        }
    },
    
    prepareDialoges: function(){
        $('.showcase-item .showcase-hover').css({display: 'none', opacity: 0, bottom: -25})
        $('.showcase-item').hover(this.showHover, this.hideHover)
    },
    
    showHover: function(){
        $('.showcase-hover', this).stop().css({display: 'block', opacity: 0}).animate({opacity: 1, bottom: -50})
    },
    
    hideHover: function(){
        $('.showcase-hover', this).stop().animate({opacity: 0, bottom: -25}, function(){
            $(this).hide()
        })
    }
}


var ByTheWay = {
    init: function(parent){
        this.addHovers(parent)
        $(parent + ' .read-link a').css({opacity: 0.5})
    },
    addHovers: function(parent){
        $(parent + ' .entry').hover(this.showLink, this.hideLink)
    },
    
    showLink: function(){
        $(' .read-link a', this).stop().animate({opacity: 1})
    },
    
    hideLink: function(){
        $(' .read-link a', this).stop().animate({opacity: 0.5})
    }
}

var QuoteFader = {
    quotes: [{
        quote: "Everything was handled professionally, without flaws<br /> and delivered in time. I was extremely pleased with the result and how<br /> everything was arranged by Populate",
        by: "Nigel Darby",
        position: "Chief Technology Officer<br />GE Healthcare"
    },
    {   quote: "Films are an important part of our internal communication.<br /> Populate have a rare ability to interpret what I the customer need.<br /> They are dynamic, flexible and deliver quality in everything they do.",
        by: "Annelie Lundell",
        position: "Internal Communications Manager<br />Q-Med AB"
    },
    {   quote: "We chose Populate to produce our live webinars which are an<br /> important part in our market communication. As a scientist, I view a lot<br /> of webinars and the production quality they delivered was among the<br /> best I've seen.",
        by: "Simon Fredriksson",
        position: "Chief Scientific Officer<br />Olink Bioscience"
    },
    {   quote: "We have high ambitions in using webcasts as a<br /> communication channel at Carnegie. Populate has exceeded our<br /> expectations in every production delivery",
        by: "Rickard Buch",
        position: "Online Manager<br />Carnegie"
    },
    {   quote: "Populate offered professionalism from square one.<br /> With great commitment, initative, high quality and competence Populate<br /> delivered just the product we were looking for &ndash; and on time. Flawless.",
        by: "Jörgen Trouvé Raa",
        position: "CFO<br />Luna AB"
    }],
    
    init: function(){
        this.randomQuote()
        this.timer = setInterval('QuoteFader.switchQuote()', 10000)
        $('body').append('<div id="dummy-quote-holder" style="position: absolute; top: -99999px; left: -99999px;"><span class="quote">&nbsp;</span><span id="dummy-content"></span><span class="cite">&mdash; <strong id="dummy-name"></strong><br /><span id="dummy-position"></span></span></div>')
        $('#dummy-quote-holder').css({
            width: $('#top-quote').css("width")
        })
    },
    
    getRandomQuote: function(){
        index = Math.floor(Math.random()*(this.quotes.length));
        if(index == this.currentQuote){
            this.getRandomQuote()
            return;
        }
        this.currentQuote = index
    },
    
    randomQuote: function(){
        this.getRandomQuote();
        this.setQuote(this.currentQuote);
    },
    
    setQuote: function(index){
        $('#quote-content').html(this.quotes[index].quote)
        $('#quote-name').html(this.quotes[index].by)
        $('#quote-position').html(this.quotes[index].position)
    },
    
    setDummyQuote: function(){
      $('#dummy-content').html(this.quotes[this.currentQuote].quote)
      $('#dummy-name').html(this.quotes[index].by)
      $('#dummy-position').html(this.quotes[index].position)
    },
    
    switchQuote: function(){
        if(this.quoteAboveViewPort())
            this.animateQuote()
    },
    
    quoteAboveViewPort: function(){
        return $(window).scrollTop() < $('#top-quote').offset().top + $('#top-quote').height()/4;
    },
    
    animateQuote: function(){
        $('#top-quote').animate({opacity: 0}, function(){
            QuoteFader.getRandomQuote()
            QuoteFader.setDummyQuote()
            var newHeight = $('#dummy-quote-holder').height()
            $(this).animate({height: newHeight}, function(){
                QuoteFader.setQuote(QuoteFader.currentQuote)  
                $(this).css({height: newHeight}).animate({opacity: 1})
            })
        })
    }
}

var Twitter = {
    url: "http://twitter.com/statuses/user_timeline/20006798.json?callback=?",
    init: function(){
        $.getJSON(this.url, this.updateStatus)
    },
    
    updateStatus: function(json){
        this.latest = json[0].text
        $('#latest-tweet').css({opacity: 0})
        $('#latest-tweet').text(this.latest);
        $('#latest-tweet').animate({opacity: 1})
    }
}

var ReturnToTop = {
    init: function(){
        $('#return-to-top a').click(function(e){
            e.preventDefault()
            $('html,body').animate({scrollTop: 0})
        })
        
        $('.top-link a').click(function(e){
            e.preventDefault()
            $('html,body').animate({scrollTop: 0})
        })
    }
}

var Search = {
    init: function(){
        var showing = false;
        $('#search-link').click(function(e){
            e.preventDefault()
            if (showing){
                $('#header-search').fadeOut()
                $(this).removeClass("active")
                showing = false;
            } else {
                $('#header-search').fadeIn()
                showing = true;
                $(this).addClass("active")
            }
        })
    }
}

var FB = {
    init: function(){
        $('.showcase-img a').fancybox()
        $('.showcase-video a').fancybox({
            'hideOnContentClick': false,
            'frameWidth': 640,
            'frameHeight': 360
        })
        $('.grid a.movie-link').fancybox({
          'hideOnContentClick': false,
        'frameWidth': 640,
        'frameHeight': 360});
        $('.grid a.image-link').fancybox()
    }
}

var Grid = {
    init: function(){
        $('.grid .info').css({opacity: 0})
        $('.grid .info').hover(function(){
            $(this).stop().animate({opacity: 0.95}, 350)
        },
        function(){
            $(this).stop().animate({opacity: 0}, 350)
        })
    }
}

var SubMenu = {
    init: function(){
        $('.sub-navigation a').click(function(e){
            e.preventDefault();
            var offset = $($(this).attr('href')).offset().top;
            $('html,body').animate({scrollTop: offset})
        })
    }
}

var Map = {
    init: function(){
      if($('#map').length > 0)  
        Map.load()
    },

    load: function() {
        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map"));
            var blueIcon = new GIcon(G_DEFAULT_ICON);
            markerOptions = { icon:blueIcon };
            map.setCenter(new GLatLng(59.851039, 17.684855), 15);
            map.addOverlay(new GMarker(new GLatLng(59.851039, 17.684855), markerOptions));
            map.addControl(new GSmallMapControl());
        }
    }
}
$(document).ready(function(){
    $('body').addClass("hasJS")
    ShowCase.init()
    ByTheWay.init('.small-news')
    ByTheWay.init('.news-list')
    //QuoteFader.init()
    Twitter.init()
    ReturnToTop.init()
    Search.init()
    FB.init()
    Grid.init()
    SubMenu.init()
    Map.init()
})

$('body').unload(function(){
    GUnload()
})