/*
 * Karin.js
 */

var karin = {};

// Follow the link to the actual article page
karin.followClickThrough = function() {
    document.location = $(this).find('a').attr('href');
}

karin.initializeReflection = function() {
    $('img.reflected').reflect({ height: 0.1, opacity: 0.8 });
}

karin.initializeArticleCards = function() {
    $('.clickable').click(karin.followClickThrough);
}

karin.initializeGallery = function() {
    $('ul.gallery a').lightBox(
	{
	    imageLoading: '/images/lightbox/lightbox-ico-loading.gif',
	    imageBtnClose: '/images/lightbox/lightbox-btn-close.gif',
	    imageBtnPrev: '/images/lightbox/lightbox-btn-prev.gif',
	    imageBtnNext: '/images/lightbox/lightbox-btn-next.gif',
	    imageBlank: '/images/lightbox/lightbox-blank.gif'
	});
}

karin.makeRoundedCorners = function() {
    $('.rounded').corner('8px');
    $('.roundedBottom').corner('8px bottom');

    $('li.commentRight').corner('8px br bl tr');
    $('li.commentLeft').corner('8px br bl tl');

    $('.roundedBottomRight').corner('8px br cc:#F5F4FF');
    $('.roundedBottomLeft').corner('8px bl cc:#F5F4FF'); 
}

karin.shareOnFacebook = function() {
    u = location.href;
    t = document.title;
    window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436');
    return false;
}

karin.initializeFacebookLink = function() {
    $('#fbLink').click(karin.shareOnFacebook);
}

karin.showComments = function() {
    // The link href contains the ID of the comments ul
    var comments = $($(this).attr('href'));

    // Make visible
    comments.css({ display: 'block' });

    // Round corners
    karin.makeRoundedCorners();

    // Smooth scroll to
    $.scrollTo(comments, 600, { offset: -50 });

    return false; // Don't follow link
}

karin.initializeCommentsLink = function() {
    $('.readComments').click(karin.showComments);
}

karin.initializeToolTips = function() {
    $('.tooltip').bt({
        contentSelector: "$(this).attr('title')",
        fill: 'white',
        cssStyles: { color: 'black', fontWeight: 'bold' },
        shrinkToFit: true,
        padding: 10,
        cornerRadius: 10,
        spikeLength: 15,
        spikeGirth: 15,
        positions: ['top']
    });
}

karin.createCommentThickbox = function() {
    $('a.submitComment').each(function() {
        var pageID = this.id;
        this.href = '#TB_inline?height=530&width=520&inlineId=commentForm';
    });
}

karin.initializeCommentLinks = function() {
    $('a.submitComment').click(function() {
        var pageID = this.id;
        $('.parentPageID').attr('value', pageID);
    });
}

karin.initialize = function() {
    karin.initializeCommentsLink();
    karin.initializeArticleCards();
    karin.initializeGallery();
    karin.initializeFacebookLink();
    karin.makeRoundedCorners();
    karin.initializeReflection();

    /* Don't use thickbox commenting in IE6 
    if (!($.browser.msie)) { */
        karin.createCommentThickbox();
        karin.initializeCommentLinks();
    /* } */
}

$(document).ready(function() {
    karin.initialize();

    // Initialize KUNDO
    kundo.init();
});

