﻿var Fullhouse = {};
Fullhouse.Flash = function() {

    var showFlashDetectionFailure = function(id) {
        var element = findFlashDetectionFailure(document.getElementById(id));
        if (element) {
            element.style.display = 'block';
        }
    };


    var hideFlashDetectionFailure = function(id) {
        var element = findFlashDetectionFailure(document.getElementById(id));
        if (element) {
            element.style.display = 'none';
        }
    };

    var findFlashDetectionFailure = function(element) {
        var needle;
        if (element) {
            if (element.nodeType == 1 && element.className.indexOf('FlashDetectionFailure') != -1) {
                needle = element;
            } else if (element.childNodes.length > 0) {
                var needle;
                for (var i = 0; i < element.childNodes.length; i++) {
                    needle = findFlashDetectionFailure(element.childNodes[i]);
                    if (needle) {
                        break;
                    }
                }
            }
        }
        return needle;
    };

    var isExpressInstallerSupported = function() {
        var vista = navigator.userAgent.match(/Windows NT (\d)\.\d/);
        if (vista && parseInt(vista[1]) >= 6) {
            return swfobject.hasFlashPlayerVersion('9.0.28');
        } else {
            return swfobject.hasFlashPlayerVersion('6.0.65');
        }
    };

    return {

        embedSWF: function(src, id, width, height, flashvars, params) {
            // Ensure params have been defined and set defaults
            if (params == null) {
                params = {};
            }
            params['bgcolor'] = params['bgcolor'] || '#ffffff';
            params['wmode']   = params['wmode'] || 'window';
            params['base']    = params['base'] || './';

            // The ID passed to this script is the container element ID. All Flash embedding on this
            // site will be done in a child element of the container named "{ID}-FlashContent". We
            // should test for the existence of the elementand if it exists use it rather than the
            // container we are given. Please see /UserControls/FlashContainerUserControl.ascx for
            // more information
            id += '-FlashContent';

            // Test success for having the main Flash version or support for express intall
            if (isExpressInstallerSupported()) {
                swfobject.embedSWF(src, id, width, height, '8.0.0', '/Scripts/expressInstall.swf', flashvars, params);
            } else {
                swfobject.addDomLoadEvent(function() {
                    showFlashDetectionFailure(id);
                });
            }
        }
    };
} ();

Fullhouse.Slideshow = function() 
{
    var update = function(id, images, index, options) {
        var img = createImage(images[index].src, function() {            
            $(this).appendTo('#' + id).wrap('<a href="' + images[index].url + '" target="' + images[index].target + '" />');
            displayImage(this, options, function() {
                var slides = $('#' + id + ' a');
                if (slides.length > 1) {
                    slides.first().remove();
                }
            });
            
            var seconds;
            if (options != null) {
                seconds = options.imagePauseInSeconds || 8;
            }
            
            setTimeout(function() {
                 update(id, images, (index + 1) % images.length, options);
            }, seconds * 1000);           
        });    
    };
    
    var createImage = function(src, callback) {
        var img = new Image();
        $(img).load(callback)
            .css('position', 'absolute')
            .css('left', '0')
            .css('top',  '0')
            .attr('src', src)
            .hide();
        return img;
    };
    
    var displayImage = function(img, options, callback) {
         $(img).fadeIn(1000, callback);
    }
    
    return {
        start: function(url, id, width, height, options) {
            $('#' + id).empty();
            $('#' + id).css('position', 'relative');
            $.get(url, function(data) {
                var images = [];
                $(data).find('image').each(function (index, element) {
                    images[index] = {
                        src: $(element).attr('photoURL'),
                        url: $(element).attr('targetLink'),
                        target: $(element).attr('openNewWin') != null && $(element).attr('openNewWin') == 'true' ? '_blank' : '_self'
                    };
                });
                update(id, images, 0, options);
            }, 'xml');       
            $(id).empty();
        }
    }
} ();
