﻿/*
File:           Popup.js
Description:    PopupWindow provides nice slide show image effect within a modal window.
Date:           06/03/2009
Last update:    16/03/2009
*/
$(function() {
    //This is the main Popup window object
    var PopupWindow = {

        //Define control buttons IDs
        nextBtn: "#btn_next_image",
        prevBtn: "#btn_prev_image",
        closeBtn: "#btn_close_popup",

        //The popup window is invisible by default
        visible: false,

        //Initializer function
        init: function() {
            var popupWindow = this;

            //Get the gallery images
            popupWindow.gallery = $(".galleryItem img[src]").
            each(
            function(i) {
                //Include the View button for Videos

                $(this).click(function() {
                    popupWindow.showItem(i);
                    return false;
                });
            });
            if (popupWindow.gallery.length == 0) {
                popupWindow.gallery = $(".videoBg img[src]").
            each(function(i) {
                var viewButton = $(this).parent().siblings("ul").find(".view");
                $(this).add(viewButton).click(function() {
                    var index = addZero(i + 1);

                    var lang = "";
                    if (i > 0 && i < 5) {
                        lang = languageCode;
                    }
                    $(".uploadImg").html(ObFlashString("/Common/Swf/player.swf", 304, 192, "file=http://file.gpotato.eu/dragonica/downloads/Videos/Flv/" + index + lang + ".flv&shuffle=true&controlbar=none&autostart=true"))

                    popupWindow.showItem(i);
                });

            });

            }

            //Attach event handlers
            popupWindow.attachHandlers();

            //Returning "this" so that PopupWindow variable gets this object 
            return this;
        },

        //Attaches event handlers to the control buttons and such
        attachHandlers: function() {
            var popupWindow = this;

            //Attach event handler to the the Next Image button
            $(popupWindow.nextBtn).click(function() {
                popupWindow.nextItem();
            });

            //Attach event handler to the the Prev Image button
            $(popupWindow.prevBtn).click(function() {
                popupWindow.prevItem();
            });

            //Hide the popup window when Escape button is pressed
            $(document).keydown(function(e) {
                if (e.keyCode == 27) {
                    popupWindow.hide();
                }
            });

            $(window).resize(function() {
                if (popupWindow.visible) {
                    popupWindow.openPopup(true);
                }
            });

            //Hide it also when the close button is pressed
            $(popupWindow.closeBtn).
        click(function() {
            popupWindow.hide();
        });

            //Attach keyboard shortcuts so that right and left arrow can control the popup window
            $(document).keydown(function(e) {
                if (popupWindow.visible) {
                    switch (e.keyCode) {
                        case 37: popupWindow.prevItem(); break;
                        case 39: popupWindow.nextItem(); break;
                    }
                    return false;
                }
            });
        },

        //Show gallery image for the given itemIndex
        showItem: function(itemIndex) {
            var popupWindow = this;

            //Disable the control buttons according to the current image index (itemIndex)
            $(popupWindow.nextBtn + "," + popupWindow.prevBtn).find("img").removeAttr("style");

            if (itemIndex <= 0) {
                popupWindow.disableButton(popupWindow.prevBtn);
            }

            if (itemIndex >= popupWindow.gallery.length - 1) {
                popupWindow.disableButton(popupWindow.nextBtn);
            }

            //Test whether the requested image exists in the gallery
            if (itemIndex >= 0 && itemIndex < popupWindow.gallery.length) {
                popupWindow.selected = itemIndex;
                popupWindow.show();
            }
            else {
                //Image index out of scrop so there's no action required
                return false;
            }
        },

        //Emulates disabling a button
        disableButton: function(id) {
            $(id).find("img").css(
    {
        "opacity": 0.2,
        "cursor": "default"
    });
        },

        //Show the next image in the gallery
        nextItem: function() {
            var popupWindow = this;
            popupWindow.showItem(popupWindow.selected + 1);
        },

        //Show the previous image in the gallery
        prevItem: function() {
            var popupWindow = this;
            popupWindow.showItem(popupWindow.selected - 1);
        },

        //Show the popup window
        show: function(picture, displayed) {
            var popupWindow = this;

            //If the popup window is not visible yet show it now
            if (!popupWindow.visible) {
                popupWindow.visible = true;
                popupWindow.openPopup();
            }
            else
            //Otherwise just update the image
            {
                popupWindow.updateImage();
            }
        },

        //Hides the popup window
        hide: function() {
            var popupWindow = this;
            if (popupWindow.visible) {
                //Remove the flash object to prevent IE from playing it after the popup is hidden
                $(".uploadImg object").remove();

                popupWindow.visible = false;
                $("#PopupLayout").hide();
            }
        },
        updateImage: function() {
            var popupWindow = this;

            //Update the currently selected image Label
            $("#page_indicator").text("[" + (popupWindow.selected + 1) + "/" + popupWindow.gallery.length + "]");

            //Hide the image so that the loading GIF indicator can be seen
            $("#PopupLayout .uploadImg img").hide();

            //Create a new temporary image which will be used to download the requested image
            var tempImage = new Image();

            //Extract the path and remove the word thumbnails from it so that it points to the bigger image file
            var tempPath = $(popupWindow.gallery[popupWindow.selected]).attr("src").replace("/T/", "/M/");

            //Bind the Onload event handler
            $(tempImage).
            bind("load",
            function() {
                //Update the src of the image and then simply show it
                $("#PopupLayout .uploadImg img").attr("src", tempPath).show();
            });

            //Start downloading the image
            tempImage.src = tempPath;

            //Attach link to the download 1024x768 image button
            $("#btn_download_1024").attr("href", tempPath.replace("/M/", "/1024x768/").replace(".jpg",".zip"));

            //Attach link to the download 1280x1024 image button
            $("#btn_download_1280").attr("href", tempPath.replace("/M/", "/1280x1024/").replace(".jpg",".zip"));
        },

        //Opens Popup window
        openPopup: function(resizeOnly) {
            var popupWindow = this;

            if (!resizeOnly) {
                //Update the image in the popup window
                popupWindow.updateImage();
            }

            //set the image dimensions
            $("#PopupLayout .uploadImg img").
        css({
            width: 500,
            height: 380
        });

            //get handles to document and window objects
            var documentObject = $(document);
            var bodyObject = $("body");
            var windowObject = $(window);

            //Adjust the popup layout and then show it
            $("#PopupLayout").
        css(
            {
                width: documentObject.width(),
                height: documentObject.height()
            }
        ).show();

            //Temporary variable for calculating the position of the popup window
            var position;

            $("#Popup").
        each(function() {
            position = {
                left: popupWindow.calcCenter(windowObject.width(), $(this).width()),
                top: popupWindow.calcCenter(windowObject.height(), $(this).height())
            };
        }).

            //Center the image
        css(position);
        },

        //Basically this calculates the center position for the image using the known equation
        calcCenter: function(content, obj) {
            var result = content / 2 - obj / 2;
            return (result > 0) ? result : 0;
        }
}.
   init(); //Initialize the popup object

        //Remove focus from any anchor after it's clicked
        $("a").click(function() {
            $(this).blur();
        });

        function addZero(number) {
            return number < 10 ? '0' + number : number;
        }

    });