/*global jQuery SWFUpload*/
(function($) {
    $.fn.photoGallery = function(params) {

        params = $.extend({
        }, params);

        return this.each(function() {
            var gallery = $(this);
            var iframe_container = $('.gallery_container', gallery);
            var myframe = $("iframe", gallery);
            var panel = $(".cpanel", gallery);
            var playing = false;
            var speed = "medium"; // /slow|medium|fast/
            var fetchUrl = null;
            var full_slideshow = null;

            if (myframe.length == 0) {
                var iframe_css = {
                    width: gallery.innerWidth() * 2 + 'px',
                    height: gallery.innerHeight() * 2 + 'px',
                    position: "relative",
                    top: '-' + Math.floor(gallery.innerHeight() / 2) + "px",
                    left: '-' + Math.floor(gallery.innerWidth() / 2) + "px",
                    overflow: "hidden"
                };
                var container_css = {
                     width: gallery.innerWidth() + 'px',
                     height: gallery.innerHeight() - (($.browser.msie) ? 35 : 25) + 'px',
                     overflow: "hidden",
                     marginTop: "5px",
                     marginBottom: "5px",
                     position: 'relative'
                };

                iframe_container = $('<div></div>').css(container_css).appendTo(gallery);
                myframe = $('<iframe frameborder="0" class="slideshow_iframe" src="'
                                + '/slideshow/slideshow.html"></iframe>')
                            .css(iframe_css)
                            .appendTo(iframe_container);

                panel = $('<div class="cpanel"><a href="#" class="bck"></a> <a href="#" class="play"></a>'
                            + '<a href="#" class="fwd"></a>'
                            + '<a href="#" class="fullscreen noshow"></a>'
                            + '</div>').appendTo(gallery);

                panel.find(".play,.stop").bind("click", function () {

                    $(this).blur();
                    gallery[0].playToggle();
                    return false;

                }).end().find(".bck").bind("click", function () {

                    $(this).blur();
                    gallery[0].updateCpanel(false);
                    gallery[0].previousImage();
                    return false;

                }).end().find(".fwd").bind("click", function () {

                    $(this).blur();
                    gallery[0].updateCpanel(false);
                    gallery[0].nextImage();
                    return false;

                }).end().find(".fullscreen").bind("click", function () {

                    $(this).blur();
                    if (full_slideshow) {
                        full_slideshow.show(0);
                        gallery[0].stopSlideshow();
                    }
                    return false;

                });

                gallery.data("pg_params", params);
            }

           this.updateCpanel = function (p) {
                playing = p;

                if (playing) {
                    $(".play", panel).removeClass("play").addClass("stop");
                } else {
                    $(".stop", panel).removeClass("stop").addClass("play");
                }

                panel.find(".slow,.medium,.fast").removeClass("current").end()
                     .find("." + speed).addClass("current");
            };

            this.populateSlideshow = function (photos) {
                if (!photos || photos.length == 0) {
                    return;
                }

                $.getScript("http://slideshow.triptracker.net/slide.js", function () {
                    full_slideshow = new PhotoViewer();
                    full_slideshow.enableAutoPlay();
                    full_slideshow.enableLoop();

                    var data = gallery.data("pg_params");
                    for (var i = 0; i < photos.length; i++) {
                        full_slideshow.add(photos[i].url, photos[i].filename);
                    }

                    panel.find(".fullscreen").removeClass("noshow");
               });
            };

            // =================================================================
            //	Public API
            // =================================================================
            this.fetchResources = function (url, params) {
                fetchUrl = url;
                myframe[0].src = "/slideshow/slideshow.html?url="
                                    + encodeURIComponent(url)
                                    + "#reload"
                                    + "__" + (new Date()).getTime();
                gallery[0].updateCpanel(true);
            };

            this.stopSlideshow = function () {
                myframe[0].src = "/slideshow/slideshow.html?url="
                                    + encodeURIComponent(fetchUrl)
                                    + "#stop"
                                    + "__" + (new Date()).getTime();
                gallery[0].updateCpanel(false);
            };

            this.startSlideshow = function () {
                myframe[0].src = "/slideshow/slideshow.html?url="
                                    + encodeURIComponent(fetchUrl)
                                    + "#start"
                                    + "__" + (new Date()).getTime();
                gallery[0].updateCpanel(true);
            };

            this.setSpeedSlow = function () {
                myframe[0].src = "/slideshow/slideshow.html?url="
                                    + encodeURIComponent(fetchUrl)
                                    + "#speed8000"
                                    + "__" + (new Date()).getTime();
                speed = "slow";
                gallery[0].updateCpanel(playing);
            };

            this.setSpeedNormal = function () {
                myframe[0].src = "/slideshow/slideshow.html?url="
                                    + encodeURIComponent(fetchUrl)
                                    + "#speed4000"
                                    + "__" + (new Date()).getTime();
                speed = "medium";
                gallery[0].updateCpanel(playing);
            };

            this.setSpeedFast = function () {
                myframe[0].src = "/slideshow/slideshow.html?url="
                                    + encodeURIComponent(fetchUrl)
                                    + "#speed1200"
                                    + "__" + (new Date()).getTime();
                speed = "fast";
                gallery[0].updateCpanel(playing);
            };

            this.playToggle = function () {
                if (playing) {
                    gallery[0].stopSlideshow();
                } else {
                    gallery[0].startSlideshow();
                }
            };

            this.previousImage = function () {
               myframe[0].src = "/slideshow/slideshow.html?url="
                                    + encodeURIComponent(fetchUrl)
                                    + "#prev"
                                    + "__" + (new Date()).getTime();
            };

            this.nextImage = function () {
                myframe[0].src = "/slideshow/slideshow.html?url="
                                    + encodeURIComponent(fetchUrl)
                                    + "#next"
                                    + "__" + (new Date()).getTime();
            };
        });
    };
})(jQuery);