/*
base.js

The basic setup and effects within the theme.
*/

$(document).ready(function () {
    // comment form
    $('textarea#comment').attr('title', 'Write your comment here...');

    // Hint Setup
    $('input[title!=""]').hint();
    $('textarea[title!=""]').hint();

    // Nav tooltip
    $('.nav li').hover(function () {
        $(this).find('.nav-tip').fadeIn(200);
    }, function () {
        $(this).find('.nav-tip').fadeOut(200);
    });
    $('#logo').hover(function () {
        $(this).find('.nav-tip').fadeIn(200);
    }, function () {
        $(this).find('.nav-tip').fadeOut(200);
    });

    // Facebox setup
    $('a[rel*=facebox]').facebox({
        loadingImage: 'http://www.doublejdesign.co.uk/wp-content/themes/djwp_v4/js/facebox/loading.gif',
        closeImage: 'http://www.doublejdesign.co.uk/wp-content/themes/djwp_v4/js/facebox/closelabel.png'
    });

    // Homepage Tabs
    $('#tab-featured').click(function () {
        $('#featured').fadeIn(200);
        $('#paid').hide();
        $('#free').hide();
        $('.nav-active').fadeOut(200);
        $(this).next().fadeIn(200);
    })
    $('#tab-free').click(function () {
        $('#featured').hide();
        $('#paid').hide();
        $('#free').fadeIn(200);
        $('.nav-active').fadeOut(200);
        $(this).next().fadeIn(200);
    })
    $('#tab-paid').click(function () {
        $('#featured').hide();
        $('#free').hide();
        $('#paid').fadeIn(200);
        $('.nav-active').fadeOut(200);
        $(this).next().fadeIn(200);
    })

    // Animated Scroll
    $('a[href*=#]').click(function () {
        if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
        && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target
          || $('[name=' + this.hash.slice(1) + ']');
            if ($target.length) {
                var targetOffset = $target.offset().top - 30;
                $('html,body')
            .animate({ scrollTop: targetOffset }, 750);
                return false;
            }
        }
    });

    // Previous/Next Post Navigation
    if ($('#nav_prev_post').html() == '') {
        $('#nav_prev_post').addClass('btn-small-disabled');
        $('#nav_prev_post').html('<a title="No more older posts.">Older Post</a>');
    }
    if ($('#nav_next_post').html() == '') {
        $('#nav_next_post').addClass('btn-small-disabled');
        $('#nav_next_post').html('<a title="No more newer posts.">Newer Post</a>');
    }
    $('#product-clip-next').click(function () {
        var clip = $(this).parent().parent().parent().prev();
        clip.scrollTop(clip.scrollTop() + 540);
    });


    // btn-small
    $('span.btn-small a').addClass('btn-small').contents().wrap('<span></span>');

    $('ul.page-numbers li a.prev').parent().addClass('prev');
    $('ul.page-numbers li a.next').parent().addClass('next');
    $('ul.page-numbers li .current').parent().addClass('current');
    $('ul.page-numbers li .dots').parent().addClass('dots');
    if ($('#pagination').attr('current') == '1') {
        $('ul.page-numbers li .current').parent().before('<li class="prev disabled"><span class="page-numbers">Previous Page</span></li>');
    }
    if ($('#pagination').attr('current') == $('#pagination').attr('total')) {
        $('ul.page-numbers li .current').parent().after('<li class="next disabled"><span class="page-numbers">Next Page</span></li>');
    }

    $(".wpsc_select_variation").change(function () { $("input.wpsc_buy_button").attr('disabled', ''); });




    // Gallery
    var step = $(".gallery-list li").width() + 8;
    $("ul.gallery-list").width($("ul.gallery-list").children().length * step);
    $(".gallery-list li img").addClass("gallery-image");
    var animating = false;
    var animating_speed = 500;
    update_gallery_show($(".gallery-list"), $(".gallery-list").find("li").eq(0).html())
    $(".gallery-slide img").hover(function () {
        var hover = $(this).attr("rel");
        $(this).attr("rel", $(this).attr("src"));
        $(this).attr("src", hover);
    });
    $(".gallery-slide img.next").click(function () {
        if (animating == false) {
            animating = true;
            var list = $(this).parent().find(".gallery-wrap").find(".gallery-list");
            if (list.position().left <= -list.width() + step) {
                list.animate({ left: '0' }, animating_speed, function () { animating = false; });
            } else {
                list.animate({ left: '-=' + step }, animating_speed, function () { animating = false; });
            }
            index = list.position().left / (-step) + 1;
            if (index >= list.children().length) { index = 0; }
            var html_content = list.find("li").eq(index).html();
            update_gallery_show(list, html_content);
        } else {
            animating = false;
        }
    });
    $(".gallery-slide img.prev").click(function () {
        if (animating == false) {
            animating = true;
            var list = $(this).parent().find(".gallery-wrap").find(".gallery-list");
            if (list.position().left >= 0) {
                list.animate({ left: -list.width() + step }, animating_speed, function () { animating = false; });
            } else {
                list.animate({ left: '+=' + step }, animating_speed, function () { animating = false; });
            }
            index = list.position().left / (-step) - 1;
            if (index < 0) { index = list.children().length - 1; }
            var html_content = list.find("li").eq(index).html();
            update_gallery_show(list, html_content);
        } else {
            animating = false;
        }
    });
    $(".gallery-list li").click(function () {
        var list = $(this).parent();
        var index = $(".gallery-list li").index(this);
        list.animate({ left: index * (-step) }, animating_speed, function () { });
        var html_content = $(this).html();
        update_gallery_show(list, html_content);
    });

    function update_gallery_show(list, content) {
        var show = list.parent().parent().prev();
        show.html(content);
    }
});
