/*
 * Miscellaneous functions used in the lightwindows (jQuery related)
 * Fier Concept & Design
 *
 * Project: Staatsbosbeheer digitaal jaarverslag
 * Author: Gest
 * Version: february 2010
 */
 

$(document).ready(function() {
	initListHovers();
	initImageHovers();	
});

function closeFancyBox() {
    parent.closeFancyBox();
}

 /* Hover images */
function initImageHovers() {		
    $('img.hover, input[type=image].hover').hover(		
        function() {			
			// Mouse in
           	var newSrc = $(this).attr("src").replace(/\.png$/,"_hover.png");
			$(this).attr("src",newSrc);
        },
        function () {
			// Mouse out
			var oldSrc = $(this).attr("src").replace(/_hover\.png$/,".png");
			$(this).attr("src",oldSrc)
        }
    ); // end hover
}

/* Hover article list rows */
function initListHovers() {		
	elems = $(".article-list li");
	elems.hover(
        function() {
            // Mouse in
            var button = $(this).find("img.button-image");
            if (button) {
                var newSrc = button.attr("src").replace(/\.png$/, "_hover.png");
                button.attr("src", newSrc);
            }
        },
        function() {
            // Mouse out
            var button = $(this).find("img.button-image");
            if (button) {
                var oldSrc = $(this).find("img.button-image").attr("src").replace(/_hover\.png$/, ".png");
                $(this).find("img.button-image").attr("src", oldSrc)
            }
        }
    );     // end hover
}

/* Gallery (Custom plugin) */

jQuery.fn.gallery = function(items, options) {
    var defaults = {
        duration: 7000 // Duration in milliseconds
    };
    // Extend our default options with those provided.
    var opts = jQuery.extend(defaults, options);

    var duration = opts['duration'];
    var i = 0;

    if (items.length == 0) return;
    var elem = $(this);
    var timerId = setInterval(function() { slide(); }, duration);

    // Start with fade-in
    elem.fadeIn(3000);

    // Change image
    slide = function() {
        // Determine new index        
        (i < items.length - 1) ? i++ : i = 0;
        // Fade out old image
        elem.fadeOut("slow", function() {
            // Set new image & text
        elem.find(".gallery-image").attr("src", items[i].src);
        $('.gallery-image')
         .load(
              function() {
                //Do stuff once the image specified above is loaded
                elem.find(".gallery-text .title").html(items[i].title);
                elem.find(".gallery-text .caption").html(items[i].caption);
                // Fade in
                elem.fadeIn("slow");                  
              }
         );            
            
        });
    }
    // Continue the chain without modifying this element
    return this;
}
