var cd_lightbox = {
    popup_template: "<div id='CD_window'></div><div id='CD_navigation'><span id='CD_page'></span><input id='CD_left_button' type='button' value='' onclick='cd_lightbox.previous_image();' /><input id='CD_right_button' type='button' value='' onclick='cd_lightbox.next_image();' /><input id='CD_exit_button' type='button' value='' onclick='cd_lightbox.remove();' /></div>",
    popup_image_template: "<div id='CD_popup_image'><img id='preview_image' src='[SOURCE]' alt='preview' /><div class='CD_caption'><h2>[HEADER]</h2><h3>[SUBHEADER]</h3><h4>RETAILER</h4><p>[RETAILER]</p><h4>QUANTITY PRODUCED</h4><p>[QUANTITY]</p><h4>CLIENT'S OBJECTIVES</h4><p>[OBJECTIVES]<br/>[VIEW MORE]</p></div></div>",
    popup_flash_template: "<div id='CD_popup_flash'></div>",
    popup_view_more_template: "<a href='gallery.htm'>view more</a>",
    popup: "",
    popup_image: "",
    popup_flash: "",
    loading: "<div id='CD_load'><img src='images/loadingAnimation.gif' /></div>",
    images: "",
    number_of_images: 0,
    showing_image: 0,
    flash: "",

    previous_image: function() {
        if (1 <= this.showing_image) {
            var src = this.images[this.showing_image - 1];
            $('#preview_image').attr("src", src);
            this.showing_image = this.showing_image - 1;
            this.set_page_number(this.showing_image + 1, this.number_of_images);
        }
    },

    next_image: function() {
        if (this.number_of_images - 1 > this.showing_image) {
            var src = this.images[this.showing_image + 1];
            $('#preview_image').attr("src", src);
            this.showing_image = this.showing_image + 1;
            this.set_page_number(this.showing_image + 1, this.number_of_images);
        }
    },

    show_popup_blank: function(html) {
        this.load_overlay();
        $("#CD_load").remove();
        $(document.body).append(this.popup_template);
        $("#CD_window").append(html);
        $("#CD_right_button").css("background", "url(images/lightboxNavNextOut.jpg)");
        $("#CD_left_button").css("background", "url(images/lightboxNavPreviousOut.jpg)")
    },

    show_popup: function(arg, viewMore) {

        //Check to see how this has been called, 1 parameter = use json object, more than 1 use parameters
        if (arg[1] != null) {
            var header = arg[0];
            var subHeader = arg[1];
            var retailer = arg[2];
            var quantity = arg[3];
            var objectives = arg[4];
            var images = arg[5];
            if (arg[6] != null) {
                var flash = arg[6];
            }
        }
        //use the json object
        else {
            var x = arg[0];
            var header = lightboxOptions[x]["header"];
            var subHeader = lightboxOptions[x]["subHeader"];
            var retailer = lightboxOptions[x]["retailer"];
            var quantity = lightboxOptions[x]["quantity"];
            var objectives = lightboxOptions[x]["objective"];
            var images = lightboxOptions[x]["images"];
            if (lightboxOptions[x]["flash"] != null) {
                var flash = lightboxOptions[x]["flash"];
            }
        }

        this.initialise();
        this.load_overlay();
        this.images = images
        this.load_images(this.images);

        if (viewMore == true) {
            this.popup_image = this.popup_image.replace("[VIEW MORE]", this.popup_view_more_template);
        }
        else {
            this.popup_image = this.popup_image.replace("[VIEW MORE]", "");
        }

        this.popup_image = this.popup_image.replace("[HEADER]", header);
        this.popup_image = this.popup_image.replace("[SUBHEADER]", subHeader);
        this.popup_image = this.popup_image.replace("[RETAILER]", retailer);
        this.popup_image = this.popup_image.replace("[QUANTITY]", quantity);
        this.popup_image = this.popup_image.replace("[OBJECTIVES]", objectives);
        $("#CD_load").remove();
        $(document.body).append(this.popup);
        $("#CD_window").append(this.popup_image);
        $("div.CD_caption").css("margin-top", (((397 - $("div.CD_caption").height()) / 2) - 13));

        $("#CD_window").show();

        if (flash != null) {
            this.flash = flash[1];
            $("div.CD_caption").append("<a id='CD_linktovideo' onclick='cd_lightbox.link_to_video();' href='#'>" + flash[0] + "</a>");
            this.number_of_images++;
        }

        this.set_page_number(1, this.number_of_images);
    },

    load_images: function(images) {
        this.number_of_images = 0;
        $.each(images,
            function(x, obj) {
                pic = new Image();
                pic.src = obj;
                cd_lightbox.number_of_images = cd_lightbox.number_of_images + 1;
            });
        this.popup_image = this.popup_image.replace("[SOURCE]", images[0]);
    },

    load_overlay: function() {
        if (typeof document.body.style.maxHeight == "undefined") {
            $("body", "html").css({
                height: "100%",
                width: "100%"
            });
            $("html").css("overflow", "hidden");
            if (document.getElementById("CD_HideSelect") == null) {
                $("body").append("<iframe id='CD_HideSelect'></iframe><div id='CD_overlay'></div>>");
            }
        } else {//all others
            if (document.getElementById("CD_overlay") == null) {
                $("body").append("<div id='CD_overlay'></div>");
            }
        }

        if (this.detect_max_xff()) {
            $("#CD_overlay").addClass("CD_overlayMacFFBGHack");
        } else {
            $("#CD_overlay").addClass("CD_overlayBG");
        }
        $("body").append(this.loading);
        $('#CD_load').show();
    },

    remove: function() {
        $("#CD_imageOff").unbind("click");
        $("#CD_closeWindowButton").unbind("click");
        $("#CD_window").fadeOut("fast", function() {
            $('#CD_window,#CD_overlay,#CD_HideSelect,#CD_navigation').trigger("unload").unbind().remove();
        });
        $("#CD_load").remove();
        if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
            $("body", "html").css({
                height: "auto",
                width: "auto"
            });
            $("html").css("overflow", "");
        }
        document.onkeydown = "";
        document.onkeyup = "";
        return false;
    },

    detect_max_xff: function() {
        var userAgent = navigator.userAgent.toLowerCase();
        if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox') != -1) {
            return true;
        }
        else {
            return false
        }
    },

    set_page_number: function(page, of) {
        if (of == 1) {
            $("#CD_right_button").css("background", "url(images/lightboxNavNextOut.jpg)");
            $("#CD_left_button").css("background", "url(images/lightboxNavPreviousOut.jpg)");
        }

        if (page == 1) {
            $("#CD_left_button").css("background", "url(images/lightboxNavPreviousOut.jpg)");
        }
        else if (page == of) {
            $("#CD_right_button").css("background", "url(images/lightboxNavNextOut.jpg)");
            $("#CD_left_button").css("background", "url(images/lightboxNavPreviousUp.jpg)");
        }
        else {
            $("#CD_right_button").css("background", "url(images/lightboxNavNextUp.jpg)");
            $("#CD_left_button").css("background", "url(images/lightboxNavPreviousUp.jpg)");
        }

        $("#CD_page").text(page + " of " + of);

        // Bit of a dirty hack, when setting the page number check to see if its the last page and
        // there is some flash included
        if (page == of && this.flash != null) {
            this.load_flash();
        }
        if (page == of - 1 && this.flash != null) {
            this.unload_flash();
        }

    },

    load_flash: function() {
        $("#CD_popup_image").hide()
        $("#CD_window").append(this.popup_flash);
        var s1 = new SWFObject('js/player.swf', 'player', '630', '372', '9');
        s1.addParam('allowfullscreen', 'true');
        s1.addParam('allowscriptaccess', 'always');
        s1.addVariable('type', 'video');
        s1.addParam('flashvars', 'file=../' + this.flash);
        s1.write('CD_popup_flash');
    },

    unload_flash: function() {
        $("#CD_popup_flash").remove();
        $("#CD_popup_image").show();

    },

    link_to_video: function() {
        this.showing_image = this.number_of_images - 1;
        this.set_page_number(this.showing_image + 1, this.number_of_images);
    },

    initialise: function() {
        this.images = "";
        this.number_of_images = 0;
        this.showing_image = 0;
        this.flash = null;
        this.popup = this.popup_template;
        this.popup_image = this.popup_image_template;
        this.popup_flash = this.popup_flash_template;
    }


}
