﻿function mouseOverHead() {
    var newLink = $(this).css("background-image").replace("_i", "_a");
    $(this).css("background-image", newLink);
    
    if ( $(this).attr("id") == "picLink" )
        $("#rightNav").html("myndir");
    else if ( $(this).attr("id") == "homeLink" )
        $("#rightNav").html("heim");        
    else if ( $(this).attr("id") == "videoLink" )
        $("#rightNav").html("myndbönd");        
}

function mouseOutHead() {
    var newLink = $(this).css("background-image").replace("_a", "_i");
    $(this).css("background-image", newLink);
    $("#rightNav").html("");
}

$(function () {
    $("#nav a").mouseover(mouseOverHead);
    $("#nav a").mouseout(mouseOutHead);
});

function getThumbHtml(title, thumbUrl, albumId, numPhotos, updated) {
    template = $("#albumThumbTemplate").html();
    return template.replace("[THUMBURL]", thumbUrl).replace("[ALBUMID]", albumId).replace("[NUMPHOTOS]", numPhotos).replace("[UPDATED]", updated).replace("[TITLE]",title);
}

function getPhotoThumbHtml(thumbUrl,updated,formUrl) {
    template = $("#photoThumbTemplate").html();

    return template.replace("[THUMBURL]", thumbUrl).replace("[FORMURL]", formUrl).replace("[UPDATED]", updated);
}

function getVideoThumbHtml(title, videoUrl, thumbUrl) {
    template = $("#videoThumbTemplate").html();

    return template.replace("[THUMBURL]", thumbUrl).replace("[TITLE]", title).replace("[VIDEOURL]", videoUrl);
}

function loadAlbums(albums) {
    $("#content").html("");
    today = new Date();

    $.each(albums.data.items, function (i, item) {
        title = item.title;
        thumbUrl = item.media.thumbnails[0];
        albumId = item.id;
        numPhotos = item.numPhotos;
        updatedDate = new Date(item.updated);
        
        $("#content").append(getThumbHtml(title, thumbUrl, albumId, numPhotos, "Uppfært " + updatedDate.toString("d/M")));
    });
}

function loadPhoto(picture) {
    $("#photoTemplate").html("<h1>" + picture.data.updated.substring(0,10) + "</h1><img src='" + picture.data.media.image.url + "'>");
}

function loadPictures(pictures) {
    pictures.data.items.sort(sortBy('timestamp', true, parseInt));
    
    $("#page h1").html(pictures.data.title);
    $("#content").html("");
    
    $.each(pictures.data.items, function(i, item) {
        var title = item.title;
        var thumbUrl = item.media.thumbnails[0];
        var imageUrl = item.media.image.url;
        
        var dateCreated = new Date(parseInt(item.timestamp));

        //formUrl = '?p=photo&photoid=' + item.id + '&albumid=' + item.album;
        //updated = item.updated.substring(0, 10);
        
        $("#content").append(getPhotoThumbHtml(thumbUrl, dateCreated.toString("yyyy-MM-dd"), imageUrl));
    });

    Shadowbox.clearCache(); // <= clear Shadowbox's cache 
    Shadowbox.setup(); // <= set up all Shadowbox links 
}

function loadVideos(videos) {
    $("#videoContent").html("");
    $.each(videos.data.items, function (i, item) {
        var title = item.title;
        var thumbUrl = item.thumbnail.sqDefault;
        var videoUrl = item.content[5] + '&autoplay=1&rel=0';
        
        updated = item.updated.substring(0, 10);
        $("#videoContent").append(getVideoThumbHtml(title,videoUrl,thumbUrl));
    });

    Shadowbox.clearCache(); // <= clear Shadowbox's cache 
    Shadowbox.setup(); // <= set up all Shadowbox links 
}

videoUrl =  'http://gdata.youtube.com/feeds/api/videos?author=oivarsson&v=2&format=5&alt=jsonc&autoplay=1&rel=0&callback=?';
googleUrl = 'http://picasaweb.google.com/data/feed/api/user/oli.ivarsson';
params =    '?alt=jsonc&thumbsize=160&v=2&callback=?';
