/*!
 * Slice Template - Main JavaScript file
 * http://inventis.github.com/
 *
 * Copyright 2011, Inventis bvba
 *
 */ 
$(document).ready(function(){

    // FancyBox
    $("a.lightbox").fancybox({
        'transitionIn'	:	'elastic',
        'transitionOut'	:	'elastic'
    });
    
    // Prevent #-links going to top
    $('a[href=#]').click(function(e){ e.preventDefault(); });
    
    // Close flash message boxes
    $('.alert-message .close').click(function(){
        $(this).parent('.alert-message').fadeOut('slow');
        return false;
    });
    
    // Toggle login box
    if (!($.browser.msie && ($.browser.version == "7.0" || $.browser.version == "6.0"))) {
        $('#loginButton').click(function(){
            $('#loginPopup').toggleClass('active');
            return false;
        });
    }
    
    // Toggle password listeners
    $('input[name="pass_readable"]').change(function(){
        var target = $('#'+$(this).attr('rel'));
        var type = "password";
        
        if ($(this).attr("checked")) {
            type = "text";
        }
        
        var newField = target.clone();
        newField.attr('type', type);
        target.replaceWith(newField);
    });
    
    /* Make submenu accessible for keyboard and add some delay */    
    $('#navigation > ul').superfish({
        dropShadows:    false,
        autoArrows:     false,
        speed:          0.2
    });
    
    if(jQuery().carouFredSel){
        if ($("#headerCarousel > div").length > 0) {
            $("#headerCarousel > div").carouFredSel({
                pagination: '#headerCarousel_pag',
                auto: {
                    pauseDuration: 5000
                },
                scroll: {
                    pauseOnHover: "resume"
                }
            });
        }

        if ($("#booksCarousel > div").length > 0) {
            $("#booksCarousel > div").carouFredSel({
                prev: "#booksCarousel_prev",
                next: "#booksCarousel_next",
                auto: false
            });
            if ($.browser.msie && $.browser.version == "8.0") {
                window.scrollTo(0, 1);
            }
        }

        if ($("#testimonialsCarousel > div.markup").length > 0) {
            $("#testimonialsCarousel > div.markup").carouFredSel({
                prev: "#testimonialsCarousel_prev",
                next: "#testimonialsCarousel_next",
                auto: false
            });
        }
    }
    
    
    /**
     * Up down controls for quantity
     */
    $('.entry.number .controls .arrow').click(function(e){

        var valueField = $(this).parent('.controls').prev('.inputfield').find('input');
        if (valueField.length < 1) { return false; }
        valueField = $(valueField[0]);
        var v, value = parseInt(valueField.val());
        
        if ($(this).hasClass('up')) {
            v = value+1;
            v = v > 999 ? 999 : v;
            valueField.val( v );
        } else {
            v = value-1;
            v = v <= 0 ? 1 : v;
            valueField.val( v );
        }
    });
    

    /**
     * Autocomplete for publishers and authors
     * http://www.pengoworks.com/workshop/jquery/autocomplete.htm
     */
    var publisherUrl    = '/ajax/publishers';
    var authorUrl       = '/ajax/authors';

    $('#searchbox_publisher').autocomplete( publisherUrl,   {
         minChars: 1,
         autoFill: true,
         maxItemsToShow: 10
    });
    $('#searchbox_author').autocomplete(    authorUrl,      {
         minChars: 1,
         autoFill: true,
         maxItemsToShow: 10
    });
    
});

/**
 * IE browser fixes
 */
if ($.browser.msie && $.browser.version == "7.0") {
   $('#main section.markup .button.left.marginBottom').parent('.clearfix').css('marginBottom', '20px'); 
}

