$(document).ready(function() {
    /* Cufon font replacement */
    /*Cufon.replace('.comments h3', { fontSize: '28px' });
    Cufon.replace('.comments .CommentUserName', { fontSize: '28px' });
    Cufon.replace('.detail .content h3', { fontSize: '28px' });
    Cufon.replace('.main-info h2', { fontSize: '28px' });*/

    /* Replace submit buttons */
    $('input.button-replace').each(function(e) {
        var replacementLink = $('<a href="#" class="btn01">' + $(this).attr('value') + '</a>');
        replacementLink.css({ 'float': $(this).css('float') });
        replacementLink.css({ 'width': $(this).width() + 'px' });
        $(this).css({ 'display': 'none' });
        $(this).after(replacementLink);

        replacementLink.click(function(e) {
            e.preventDefault()
            $(this).prev().click();
        });
    });

    /* To contentdetail.html */
    $('#carousel').jcarousel({
        animation: 'slow',
        buttonNextHTML: null,
        buttonPrevHTML: null,
        initCallback: general_initCallback
    });

    $("#carousel a img").click(function() {
        var src = $(this).parent().parent().find('#previewImg').attr('src');
        $('#preview').fadeOut("slow", function() {
            $(this).attr('src', src).load(function() { $(this).fadeIn("slow"); }).attr('src', src);
        });
    });

    /* To home.html */
    $('#home_carousel').jcarousel({
        animation: 'slow',
        auto: 5,
        scroll: 1,
        wrap: "both",
        buttonNextHTML: null,
        buttonPrevHTML: null,
        initCallback: home_carousel_initCallback,
        itemVisibleInCallback: home_carousel_next
    });

    /* To contentdetail.html ( tabs ) */
    $(".tabs li").click(function(e) {
        $(".tabs li").removeClass();
        $(this).addClass('active');
        $(".tabs li").each(function(index) {
            if ($(this).hasClass("active")) {
                $(".tab-cnt > div").addClass('hidden');
                if (index == 0) {
                    $(".tab-cnt > div:eq(" + index + ")").removeClass('hidden');
                    $(".gallery").removeClass('hidden');
                } else if (index == 2) {
                    $(".tab-cnt > div:eq(" + index + ")").removeClass('hidden');
                } else {
                    $(".tab-cnt > div:eq(" + index + ")").removeClass('hidden');
                }
                return false;
            }
        });
    });

    // Hide URL row from message form (address detail pages)
    $('.comments .BoardPanel .MessageEdit table table tr:nth-child(2)').hide();

    // Hide subscription panels for blogs and messageboard in my profile - subscriptions
    $('.EditProfile .TabsContent .SubscriptionsPanel').hide();

    // Shop template (temporary)
    $('.shop ul.devices a').click(function(e) {
        e.preventDefault();

        if (!$(this).parent().hasClass('active')) {

            // close open list(s)
            $('.shop ul.devices li.active ul').slideUp();
            $('.shop ul.devices li.active').removeClass('active');

            // open selected list
            $(this).parent().addClass('active');
            $(this).siblings('ul').slideDown();

        }
    });
});

/* To contentdetail.html */
function general_initCallback(carousel) {
    $('.next').bind('click', function() {
        carousel.next();
        return false;
    });

    $('.prev').bind('click', function() {
        carousel.prev();
        return false;
    });
}

/* To home.html */
function home_carousel_initCallback(carousel) {

    $('#home_carousel_control a').bind('click', function() {
        carousel.scroll( $.jcarousel.intval( $(this).text() ) );
        $('#home_carousel_control li').removeClass();
        $(this).parent().addClass('active');
        return false;
    });

};

function home_carousel_next(carousel, link, index) {
    $('#home_carousel_control li').removeClass();
    $('#home_carousel_control li:eq(' + (index - 1)  + ')').addClass('active');
}


