/*
    Functions for opening, closing product description
*/

var collapseAll = true;
var openItem = false;

// On document ready
$(document).ready(function() {
    // On click
    $('.more').click(function(e) {
                        
        // If there is a description
        if ($(this).parent().children().size() > 4) {
        
            // If the description is hidden
            if ($(this).parent().children().eq(3).is(":hidden")) {
                // Open description box
                openDescription($(this).parent());
            }
            else {
                // Close a description box
                closeDescription($(this).parent());
            }
        }
        
        // Prevent default action from occuring
        e.preventDefault();
        return false;
    });
    
    
    // Enable clicks on the description box to close it to, makes it a little easier   
    $('.description').click(function(e) {
        // Close a description box
        closeDescription($(this).parent());
        
        // Prevent default action from occuring
        e.preventDefault();
        return false;
    });
    
    
    // Add hover effect
    $('.more a').hover(function() {
        if ($(this).parent().parent().children().size() > 4) {
            if ($(this).parent().parent().children().eq(3).is(':hidden')) {
                $(this).css("background-image", "url(/media/css/images/open.png)");
            }
            else {
                $(this).css("background-image", "url(/media/css/images/close.png)");
            }
        }
        else {
            $(this).css("background-image", "none");
        }
    }, function() {
        if ($(this).parent().parent().children().eq(3).is(':hidden')) {
            $(this).css("background-image", "none");
        }
    });
    
}); // Close "on document ready"




// Function for closing a description box
function closeDescription(element) {
    $(element).children().eq(3).hide();
    $(element).css("background", "none");
    $(element).children().eq(0).css("font-weight", "normal");
    $(element).children().eq(1).show();
    $(element).children().eq(2).show();
    $(element).children().eq(4).children().eq(0).css("background-image", "none");
    
    openItem = false;
}

// Function for opening a description box
function openDescription(element) {
    $(element).css("background", "#e6f3f6");
    $(element).children().eq(0).css("font-weight", "bold");
    $(element).children().eq(1).hide();
    $(element).children().eq(2).hide();
    $(element).children().eq(3).fadeIn("fast");
    $(element).children().eq(4).children().eq(0).css("background-image", "url(/media/css/images/close.png)");

    // If collapseAll is true, close the last open item
    if ( (collapseAll == true) && (openItem !== false) ) {
        closeDescription(openItem);
    }
    
    // Save the current open item
    openItem = element;
}



/* JS FOR LATEST FEEDS */
$(document).ready(function() {
    $("#latest_feeds [title]").mbTooltip({
        opacity : 1, //opacity
        wait:200, //before show
        ancor:"mouse", //"parent"
        cssClass:"default", // default = default
        timePerWord:70, //time to show in milliseconds per word
        hasArrow:false,
        color:"white",
        shadowColor:"black",
        fade:350
    });
});

