(function($){
    $(document).ready(function(){
        if($("#side-content").is(".sidebar-blog")) {
            fetchFlickr("flicks", 16, "55350976@N05");
        } else {
            fetchFlickr("flicks", 15, "55350976@N05");
        }
        
        
        $("#home-highlight-content").shadowfy();
        $("#home-highlight-menu").shadowfy();
        $("#labs-featured").shadowfy();
        $("#labapp-gallery").shadowfy();
        
        if($("#labapp-gallery").length > 0) {
            initAppGallery(5700);
        }
        
        if($("#home-highlight").length > 0) {
            initHighlightGallery(5700);
        }
    });
    
    getTwitters('tweets', { 
        id: 'mihswat', 
        count: 5, 
        enableLinks: true, 
        ignoreReplies: true, 
        clearContents: true,
        template: '%text% - <a href="http://twitter.com/%user_screen_name%/statuses/%id%" title="View tweet on Twitter" class="timeago">%time%</a>'
    });
    
    function fetchFlickr(element, max, id) {
        var html = $("<ul />");

        $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?ids=" + id + "&lang=en-us&format=json&jsoncallback=?", function(data) {
            total = (data.items.length < max) ? data.items.length : max;

            for(i = 0; i < total; i++) {
                var src = data.items[i].media.m.replace("_m.jpg","_s.jpg");
                
                var item = $("<li />");
                var photo = $("<a />");
                
                if((i+1)%2 == 0) { item.addClass("c2"); }
                if((i+1)%3 == 0) { item.addClass("c3"); }
                if((i+1)%4 == 0) { item.addClass("c4"); }
                if((i+1)%5 == 0) { item.addClass("c5"); }
                
                item.html(photo.attr("href", data.items[i].link).attr("title", data.items[i].title + " - View this photo on Flickr").html($("<img src='"+src+"' />").attr("alt", data.items[i].title)));
                html.append(item);
            }

            $("#"+element).html(html);
        });
    }
    
    $.fn.extend({
        shadowfy: function() {
            return this.each(function() {
                $(this).append('<span class="sdt"> </span><span class="sdr"> </span><span class="sdb"> </span><span class="sdl"> </span>');
            });
        }
    });
    
    function initAppGallery(speed) {
        var qty = $("#labapp-gallery").find("li").length;
        var current = 0;
        
        setInterval(function(){
            var previous = $("#labapp-img"+current).removeClass("active");
            current = (current == qty-1) ? 0 : current+1;
            $("#labapp-img"+current).addClass("active").fadeIn("slow", function(){
                previous.hide();
            });
        }, speed);
    }
    
    function initHighlightGallery(speed) {
        var qty = $("#home-highlight-content").find(".home-highlight-item").length;
        var current = 0;
        var timer;
        
        var startTimer = function(){
            timer = setInterval(function(){
                var item = (current == qty-1) ? 0 : current+1;
                selectItem(item);
            }, speed);
        }
        
        var selectItem = function(item) {
            $("#home-highlight-menu-"+current).removeClass("active");
            var previous = $("#home-highlight-"+current).removeClass("active");
            current = item;
            $("#home-highlight-"+current).addClass("active").fadeIn("slow", function(){
                previous.hide();
            });
            $("#home-highlight-menu-"+current).addClass("active");
        }
        
        $("#home-highlight-menu").find("a").click(function(){
            clearInterval(timer);
            var patt = /[0-9]+/;
            var index = parseInt($(this).parents("li:first").attr("id").match(patt));
            if(index != current) {
                selectItem(index);
            }
            startTimer();
            return false;
        });
        
        startTimer();
    }
})(jQuery);
