(function ($) {
    $.fn.extend({

        homepage_banner: function (options) {
            var defaults = {
                interval: 5000,
                timer_label: 'banner_timer',
                swf_name: 'homepage_banner',
                detector_label: 'heartbeat',
                detector_interval: 500,
                movie_load_detector_label: 'movieLoaded',
                movie_load_detector_interval: 500,
                swf_path: 'homepage.swf'
            };

            var options = $.extend(defaults, options);

            return this.each(function () {
                var obj = $(this);
                var current_item = -1;
                var total_item = obj.find('.images-container img').length;
                obj.find('.images-container img').each(function (i) {
                    $(this).hide();
                }).end();

                obj.find('.play-button').click(function () {
                    obj.find('.images-container').hide().end();
                    obj.find('.flash-container').show().end();
                    obj.load_movie();
                    return false;
                }).end();

                obj.load_movie = function () {
                    if (obj.find('.flash-container').html().length == 0) {
                        obj.find('.flash-container').html("<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' WIDTH='990' HEIGHT='340' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0' ID='" + options['swf_name'] + "'><PARAM NAME='MOVIE' VALUE='" + options['swf_path'] + "'><PARAM NAME='PLAY' VALUE='true'><PARAM NAME='LOOP' VALUE='false'><PARAM NAME='QUALITY' VALUE='high'><PARAM NAME='SCALE' VALUE='SHOWALL'><EMBED NAME='" + options['swf_name'] + "' SRC='" + options['swf_path'] + "' WIDTH='990' HEIGHT='340' PLAY='true'  LOOP='false' QUALITY='high' SCALE='SHOWALL' swLiveConnect='true' PLUGINSPAGE='http://www.macromedia.com/go/flashplayer/'></EMBED></OBJECT>").end();
                    }
                    obj.find('.flash-container object').ready(function () {
                        obj.playmovie(options['swf_name']);
                    });
                }

                obj.auto_play = function () {
                    obj.find('.images-container img').fadeOut().end();
                    if (current_item < total_item - 1) {
                        current_item += 1;
                    } else {
                        current_item = 0;
                    }
                    obj.find('#.images-container img[rel^=' + current_item + ',]').fadeIn().end();
                    var duration = obj.find('#.images-container img[rel^=' + current_item + ',]').attr('rel').split(',')[2] * 1000;
                    var showPlaybtbn = obj.find('#.images-container img[rel^=' + current_item + ',]').attr('rel').split(',')[1];
                    if (showPlaybtbn == 0) {
                        $(".play-button").hide();
                    } else{
                        $(".play-button").show();
                    }
                    setTimeout(obj.auto_play, duration);
                }

                obj.thisMovie = function (movieName) {
                    // IE and Netscape refer to the movie object differently.
                    // This function returns the appropriate syntax depending on the browser.
                    //                    if (navigator.appName.indexOf ("Microsoft") !=-1) {
                    //                        return window[movieName]
                    //                    } else {
                    return document[movieName]
                    //                    }
                }

                // Checks if movie is completely loaded.
                // Returns true if yes, false if no.
                obj.movieIsLoaded = function (theMovie) {
                    if (typeof (theMovie) != "undefined") {
                        return theMovie.PercentLoaded() == 100;
                    } else {
                        return false;
                    }
                }

                obj.playmovie = function (movieName) {
                    obj.everyTime(options['movie_load_detector_interval'], options['movie_load_detector_label'], function () {
                        if (obj.movieIsLoaded(obj.thisMovie(movieName))) {
                            obj.stopTime(options['movie_load_detector_label']);
                            obj.thisMovie(movieName).Play();

                            obj.everyTime(options['detector_interval'], options['detector_label'], function () {
                                if (!obj.thisMovie(movieName).IsPlaying()) {
                                    obj.find('.images-container').show().end();
                                    obj.find('.flash-container').hide().end();
                                    obj.stopTime(options['detector_label']);
                                }
                            });
                        }
                    });
                }

                obj.everyTime(options['interval'], options['timer_label'], function () {
                    // obj.auto_play();
                });
                obj.auto_play();
                obj.find('.images-container').show().end();
                obj.find('.flash-container').hide().end();
            });
        }
    });

})(jQuery);

