var slideshow,
    disabled = false;
var slideshowPause = document.getElementById("hdnRotationInterval").value;
var ie7 = (jQuery.browser.msie && jQuery.browser.version == 7.0);


function initCallbackFunction(carousel) {


    $("#img").bind('click', function() {
        if ($("#frontpagegallery li.active").attr("nodelink").substring(0, 4) == "http") {
            window.open($("#frontpagegallery li.active").attr("nodelink"));
        } else {
            window.location = $("#frontpagegallery li.active").attr("nodelink");
        }
    });

    $("#frontpagegallery li").bind("click", function() {
        disabled = true; // stop slideshow
    });

    $('#img').bind('image-loaded', function() {
        var idx = $('#frontpagegallery li.active').data('index');
        carousel.scroll(idx);
        return false;
    });
    $("#frontpagegallery li").each(function() {
        //	    $(this).append("<p class='frontpage-text'>" + $(this).find("img").attr("alt") + "</p>");
    });
};

function gallery_changed() {

    $("#frontpageselector2-learnmore").attr("href", $("#frontpagegallery li.active").attr("nodelink"));
    if ($("#frontpagegallery li.active").attr("nodelink").substring(0, 4) == "http") {
        $("#frontpageselector2-learnmore").attr("target", "_blank");
    }
};

$(document).ready(function() {

    //slideshowPause = document.getElementById('<%=hdnRotationInterval.ClientID%>').value;
    $("#img .caption").hide();
    $('#frontpagegallery li').each(function(idx) { $(this).data('index', (++idx)); });



    // load and fade-in thumbnails
    $('#frontpagegallery li img').each(function() {
        if (this.complete || this.readyState == 'complete') { $(this).animate({}, 5); }
        else { $(this).load(function() { $(this).animate({}, 5) }); }
    });
    $('#frontpagegallery').find('li:first').addClass('active'); // display first image when Galleria is loaded

    // need to load all of the images before using the following for IE7
    $('#frontpagegallery').jcarousel({ scroll: 1, wrap: 'both', initCallback: initCallbackFunction });
    $('#frontpagegallery').galleria(
			    {
			        // #img is the empty div which holds full size images
			        insert: "#img",

			        // enable history plugin (uses anchor hash tags)
			        history: false,
			        clickNext: false,

			        // function fired when the image is displayed
			        onImage: function(image, caption, thumb) {

			            // fade in the image

			            //			            image.hide().fadeIn(5000);
			            //			            image.show().fadeOut(5000);

			            //			        image.hide()
			            //			            .fadeIn(3000)
			            //			            .show()
			            //			            .fadeOut(6000);
			            //			            gallery_changed();

			            //			            $("#img").trigger('image-loaded');
			            image.hide().fadeIn(3000);
			            gallery_changed();
			            $("#img").trigger('image-loaded');

			        }
			    });
    // clear interval when timeout is changed
    window.clearInterval(slideshow);

    // and set new interval with new timeout value
    disabled = false;
    slideshow = window.setInterval(function() {
        if (!disabled)

            $.galleria.next();
    },
				   slideshowPause); // must be set in milisecond
});





