﻿(function($) {
    $.Pop = { version: '2.5' };
    $.fn.Pop = function(settings) {


        settings = jQuery.extend({
            animationSpeed: 'fast', /* fast/slow/normal */
            padding: 40, /* padding for each side of the picture */
            opacity: 0.80, /* Value between 0 and 1 */
            showTitle: true, /* true/false */
            allowresize: true, /* true/false */
            counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
            theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
            ContentText: '',
            callback: function() { },
            closeCallBack: function() { }
        }, settings);

        //    alert(settings.ContentText);
        // Global variables accessible only by prettyPhoto
        var doresize = true, percentBased = false, correctSizes,

        // Cached selectors
        $pp_pic_holder, $ppt, settings,

        // prettyPhoto container specific
        pp_contentHeight, pp_contentWidth, pp_containerHeight, pp_containerWidth,

        //Gallery specific
        setPosition = 0,

        // Global elements
        $scrollPos = _getScroll();
        //   alert("click");
        // Fallback to a supported theme for IE6
        if ($.browser.msie && $.browser.version == 6) {
            settings.theme = "light_square";
        }
        // Set my global selectors
        $pp_pic_holder = $('.pp_pic_holder');
        $ppt = $('.ppt');
        //   alert("opacity");
        $('div.pp_overlay').css('opacity', 0);
        $('div.pp_overlay').css('height', $(document).height()).hide().bind('click', function() {
            //  $.Pop.close();
        });

        $('div.pp_close').bind('click', function() {
            $.Pop.close();
        });




        $(this).each(function() {

            $(this).bind('click', function() {
                // alert("click");
                //  $.Pop.open();

            });
        });

        $.Pop.open = function() {

            $('.container').hide();
            // To fix the bug with IE select boxes
            if ($.browser.msie && $.browser.version == 6) {
                $('select').css('visibility', 'hidden');
            };

            // Hide the flash
            $('object,embed').css('visibility', 'hidden');

            // Set my global selectors
            $pp_pic_holder = $('.pp_pic_holder');
            $ppt = $('.ppt');

            $pp_pic_holder.attr('class', 'pp_pic_holder ' + settings.theme); // Set the proper theme
            _centerOverlay(); // Center it
            _checkPosition(1);
            $('.pp_loaderIcon').show();
            $('div.pp_overlay').css('height', $(document).height() + 150).show().fadeTo(settings.animationSpeed, settings.opacity);
            $pp_pic_holder.fadeIn(settings.animationSpeed);
            $('div.pp_close').hide();
            $('.pp_content').css("background-color", "");

        };
        $.Pop.expand = function(ContentText, width, height, CallBack, variable) {
            $('div.pp_close').hide();
            $pp_pic_holder.fadeIn(settings.animationSpeed, function() {
                $pp_pic_holder.find('.pp_content').css('overflow', 'hidden');
                // $pp_pic_holder.find('#fullResImage').attr('src', images[setPosition]);
                // Fit item to viewport
                correctSizes = _fitToViewport(width, height);
                _showContent(ContentText, CallBack, variable);
                $('div.pp_close').show();

            });
        }
        _showContent = function(ContentText, CallBack, variable) {
            $scrollPos = _getScroll();
            $('.pp_loaderIcon').hide();

            if ($.browser.opera) {
                windowHeight = window.innerHeight;
                windowWidth = window.innerWidth;
            } else {
                windowHeight = $(window).height();
                windowWidth = $(window).width();
            };

            // Calculate the opened top position of the pic holder
            projectedTop = $scrollPos['scrollTop'] + ((windowHeight / 2) - (correctSizes['containerHeight'] / 2));
            if (projectedTop < 0) projectedTop = 0 + $pp_pic_holder.find('.ppt').height();

            // Resize the content holder
            $pp_pic_holder.find('.pp_content').animate({ 'height': correctSizes['contentHeight'] }, settings.animationSpeed);

            // Resize picture the holder
            $pp_pic_holder.animate({
                'top': projectedTop,
                'left': ((windowWidth / 2) - (correctSizes['containerWidth'] / 2)),
                'width': correctSizes['containerWidth']
            }, settings.animationSpeed, function() {
                $pp_pic_holder.width(correctSizes['containerWidth']);
                $pp_pic_holder.find('.pp_hoverContainer,#fullResImage').height(correctSizes['height']).width(correctSizes['width']);

                // Fade the new image
                $pp_pic_holder.find('#pp_full_res').fadeIn(settings.animationSpeed);

                if (correctSizes['resized']) $('a.pp_expand,a.pp_contract').fadeIn(settings.animationSpeed);

                pp_typeMarkup = ContentText;

                if (pp_typeMarkup.length > 0 && pp_typeMarkup != "no change") {
                    $pp_pic_holder.find('#pp_full_res')[0].innerHTML = pp_typeMarkup;
                }

                CallBack(variable);
            });

        };
        function _checkPosition(setCount) {

            if (setPosition == setCount - 1) {
                $pp_pic_holder.find('a.pp_next').css('visibility', 'hidden');
                $pp_pic_holder.find('a.pp_arrow_next').addClass('disabled').unbind('click');
            } else {
                $pp_pic_holder.find('a.pp_next').css('visibility', 'visible');
                $pp_pic_holder.find('a.pp_arrow_next.disabled').removeClass('disabled').bind('click', function() {
                    $.prettyPhoto.changePage('next');
                    return false;
                });
            };

            // If at the beginning, hide the previous link
            if (setPosition == 0) {
                $pp_pic_holder.find('a.pp_previous').css('visibility', 'hidden');
                $pp_pic_holder.find('a.pp_arrow_previous').addClass('disabled').unbind('click');
            } else {
                $pp_pic_holder.find('a.pp_previous').css('visibility', 'visible');
                $pp_pic_holder.find('a.pp_arrow_previous.disabled').removeClass('disabled').bind('click', function() {
                    $.prettyPhoto.changePage('previous');
                    return false;
                });
            };

            // Hide the bottom nav if it's not a set.
            if (setCount > 1) {
                $('.pp_nav').show();
            } else {
                $('.pp_nav').hide();
            }
        };

        function _centerOverlay() {
            if ($.browser.opera) {
                windowHeight = window.innerHeight;
                windowWidth = window.innerWidth;
            } else {
                windowHeight = $(window).height();
                windowWidth = $(window).width();
            };

            if (doresize) {

                $pHeight = $pp_pic_holder.height();
                $pWidth = $pp_pic_holder.width();
                $tHeight = $ppt.height();

                projectedTop = (windowHeight / 2) + $scrollPos['scrollTop'] - ($pHeight / 2);
                if (projectedTop < 0) projectedTop = 0 + $tHeight;
                //   alert(projectedTop);
                //   alert((windowWidth / 2) + $scrollPos['scrollLeft'] - ($pWidth / 2));
                $pp_pic_holder.css({
                    'top': projectedTop,
                    'left': (windowWidth / 2) + $scrollPos['scrollLeft'] - ($pWidth / 2)
                });

                $ppt.css({
                    'top': projectedTop - $tHeight,
                    'left': (windowWidth / 2) + $scrollPos['scrollLeft'] - ($pWidth / 2) + (settings.padding / 2)
                });
            };
        };
        function _getScroll() {

            if (self.pageYOffset) {
                scrollTop = self.pageYOffset;
                scrollLeft = self.pageXOffset;
            } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
                scrollTop = document.documentElement.scrollTop;
                scrollLeft = document.documentElement.scrollLeft;
            } else if (document.body) {// all other Explorers
                scrollTop = document.body.scrollTop;
                scrollLeft = document.body.scrollLeft;
            }

            return { scrollTop: scrollTop, scrollLeft: scrollLeft };

        };
        $.Pop.resize = function(width, height) {
            correctSizes = _fitToViewport(width, height);
            projectedTop = $scrollPos['scrollTop'] + ((windowHeight / 2) - (correctSizes['containerHeight'] / 2));
            if (projectedTop < 0) projectedTop = 0 + $pp_pic_holder.find('.ppt').height();
            $pp_pic_holder.find('.pp_content').animate({ 'height': correctSizes['contentHeight'] }, settings.animationSpeed);
            $pp_pic_holder.animate({
                'top': projectedTop,
                'left': ((windowWidth / 2) - (correctSizes['containerWidth'] / 2)),
                'width': correctSizes['containerWidth']
            }, settings.animationSpeed);

        };
        $.Pop.expand = function(ContentText, width, height, CallBack, variable) {

            $pp_pic_holder.fadeIn(settings.animationSpeed, function() {
                $pp_pic_holder.find('.pp_content').css('overflow', 'hidden');
                // $pp_pic_holder.find('#fullResImage').attr('src', images[setPosition]);
                // Fit item to viewport
                correctSizes = _fitToViewport(width, height);
                _showContent(ContentText, CallBack, variable);
                $('div.pp_close').show();

            });
        }
        $.Pop.close2 = function(CallBack) {

            $pp_pic_holder.find('object,embed').css('visibility', 'hidden');

            $('div.pp_pic_holder,div.ppt').fadeOut(settings.animationSpeed);

            $('div.pp_overlay').fadeOut(settings.animationSpeed, function() {
                // $('div.pp_overlay,div.pp_pic_holder,div.ppt').remove();

                // To fix the bug with IE select boxes
                if ($.browser.msie && $.browser.version == 6) {
                    $('select').css('visibility', 'visible');
                };

                // Show the flash
                $('object,embed').css('visibility', 'visible');

                setPosition = 0;

                CallBack();
                // if (pp_typeMarkup.length > 0 && pp_typeMarkup != "no change") {
                //   alert(pp_typeMarkup);
                $pp_pic_holder.find('#pp_full_res')[0].innerHTML = "";
                // }

            });
            $('.container').show();
            doresize = true;
            $('.pp_content').css("background-color", "");
        };
        $.Pop.close = function() {

            $pp_pic_holder.find('object,embed').css('visibility', 'hidden');

            $('div.pp_pic_holder,div.ppt').fadeOut(settings.animationSpeed);

            $('div.pp_overlay').fadeOut(settings.animationSpeed, function() {
                // $('div.pp_overlay,div.pp_pic_holder,div.ppt').remove();

                // To fix the bug with IE select boxes
                if ($.browser.msie && $.browser.version == 6) {
                    $('select').css('visibility', 'visible');
                };

                // Show the flash
                $('object,embed').css('visibility', 'visible');

                setPosition = 0;

                settings.closeCallBack();
                // if (pp_typeMarkup.length > 0 && pp_typeMarkup != "no change") {
                //   alert(pp_typeMarkup);
                $pp_pic_holder.find('#pp_full_res')[0].innerHTML = "";
                // }

            });
            $('.container').show();
            $('.pp_content').css("background-color", "");
            doresize = true;
        };



        /**
        * Resize the item dimensions if it's bigger than the viewport
        * @param width {integer} Width of the item to be opened
        * @param height {integer} Height of the item to be opened
        * @return An array containin the "fitted" dimensions
        */
        function _fitToViewport(width, height) {
            hasBeenResized = false;

            _getDimensions(width, height);

            // Define them in case there's no resize needed
            imageWidth = width;
            imageHeight = height;

            windowHeight = $(window).height();
            windowWidth = $(window).width();

            return {
                width: imageWidth,
                height: imageHeight,
                containerHeight: pp_containerHeight,
                containerWidth: pp_containerWidth,
                contentHeight: pp_contentHeight,
                contentWidth: pp_contentWidth,
                resized: hasBeenResized
            };
        };
        /**
        * Get the containers dimensions according to the item size
        * @param width {integer} Width of the item to be opened
        * @param height {integer} Height of the item to be opened
        */
        function _getDimensions(width, height) {
            //  $pp_pic_holder.find('.pp_details').width(width).find('.pp_description').width(width - parseFloat($pp_pic_holder.find('a.pp_close').css('width'))); /* To have the correct height */

            // Get the container size, to resize the holder to the right dimensions
            pp_contentHeight = height;
            pp_contentWidth = width;
            pp_containerHeight = pp_contentHeight + $pp_pic_holder.find('.ppt').height() + $pp_pic_holder.find('.pp_top').height() + $pp_pic_holder.find('.pp_bottom').height();
            pp_containerWidth = width + settings.padding;
        }

        //* Hide the content...DUH!

        function _hideContent() {
            // Fade out the current picture
            //  $pp_pic_holder.find('.pp_hoverContainer,.pp_details').fadeOut(settings.animationSpeed);
            $pp_pic_holder.find('#pp_full_res object,#pp_full_res embed').css('visibility', 'hidden');
            $pp_pic_holder.find('#pp_full_res').fadeOut(settings.animationSpeed, function() {
                $('.pp_loaderIcon').show();
            });

            // Hide the title
            $ppt.fadeOut(settings.animationSpeed);
        }


    };

})(jQuery);

