﻿function ___getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else {
        // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        if (document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
    return arrayPageSize;
}
function ___getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;
    }
    arrayPageScroll = new Array(xScroll, yScroll);
    return arrayPageScroll;
}

function popit(url) {
    // Hime some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
    $('embed, object, select').css({ 'visibility': 'hidden' });

    // Call the function to create the markup structure; style some elements; assign events in some elements.
    // Apply the HTML markup into body tag
    //$('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="lightbox-container-image-box"><div id="lightbox-container-image"><div id="lightbox-image"><div style="height:400px" id="lightbox-nav"><a href="#" id="lightbox-nav-btnPrev"></a><a href="#" id="lightbox-nav-btnNext"></a></div><div id="lightbox-loading"><a href="#" id="lightbox-loading-link"><img src="' + '/images/popup/loader.gif' + '"></a></div></div></div><div id="lightbox-secNav-btnClose"><input id="lightbox-secNav-btnClose-image" name="lightbox-secNav-btnClose-image" type="image" src="' + '/images/popup/btnClose.gif' + '"></div></div></div></div></div>');
    //$('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="lightbox-container-image-box"><div id="lightbox-container-image"><div id="lightbox-maincontent"></div><div id="lightbox-container-header"><div id="lightbox-secNav-btnClose"><input id="lightbox-secNav-btnClose-image" name="lightbox-secNav-btnClose-image" type="image" src="' + '/images/popup/btnClose.gif' + '"></div></div><div id="lightbox-loading">Loading...</div></div></div></div>');
    $('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="lightbox-container-image-box"><div id="lightbox-container-image"><div id="lightbox-maincontent"></div><div id="lightbox-container-header-flash"><div id="lightbox-secNav-btnClose"><input id="lightbox-secNav-btnClose-image" name="lightbox-secNav-btnClose-image" type="image" src="' + '/images/popup/btnClose.gif' + '"></div></div><div id="lightbox-loading">Loading...</div></div></div></div>');

    // Enable keyboard navigation
    _enable_keyboard_navigation();


    // Get page sizes
    var arrPageSizes = ___getPageSize();

    // Style overlay and show it
    $('#jquery-overlay').css({
        backgroundColor: '#000',
        opacity: 0.8,
        width: arrPageSizes[0],
        height: arrPageSizes[1]
    }).fadeIn();

    // Get page scroll
    var arrPageScroll = ___getPageScroll();
    // Calculate top and left offset for the jquery-lightbox div object and show it
    $('#jquery-lightbox').css({
        top: arrPageScroll[1] + (arrPageSizes[3] / 10),
        left: arrPageScroll[0]
    }).show();

    // Assign the _finish function to lightbox-loading-link and lightbox-secNav-btnClose objects
    $('#lightbox-loading-link,#lightbox-secNav-btnClose-image').click(function() {
        _finish();
        return false;
    });
    // If window was resized, calculate the new overlay dimensions
    $(window).resize(function() {
        // Get page sizes
        var arrPageSizes = ___getPageSize();
        // Style overlay and show it
        $('#jquery-overlay').css({
            width: arrPageSizes[0],
            height: arrPageSizes[1]
        });
        // Get page scroll
        var arrPageScroll = ___getPageScroll();
        // Calculate top and left offset for the jquery-lightbox div object and show it
        $('#jquery-lightbox').css({
            top: arrPageScroll[1] + (arrPageSizes[3] / 10),
            left: arrPageScroll[0]
        });
    });
    var href = url;


    // Call the function that prepares image exibition
    // Show the loading
    $('#lightbox-loading').show();
    // Hide some elements
    $('#lightbox-maincontent,#lightbox-nav,#lightbox-nav-btnPrev,#lightbox-nav-btnNext,#lightbox-container-image-data-box,#lightbox-image-details-currentNumber').hide();

    //Get the page
    $.get(href, {},
              function(data) {
                  _show_image(data);

              });

              _resize_container_image_box(798, 528);

          }
          function _finish() {
              $('#jquery-lightbox').remove();
              $('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
              // Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
              $('embed, object, select').css({ 'visibility': 'visible' });
              //SetFeatureAccordian();
          }
          function _show_image(data) {
              $('#lightbox-loading').hide();
              $('#lightbox-maincontent').fadeIn("fast", function() {
                  $('#lightbox-maincontent').hide();
                  $('#lightbox-maincontent').append(data);
                  $('#lightbox-maincontent').fadeIn("fast", function() { $("#lightbox-container-header-flash").fadeIn("fast"); });
              });
          }
function _resize_container_image_box(intImageWidth, intImageHeight) {
    // Get current width and height
    var intCurrentWidth = $('#lightbox-container-image-box').width();
    var intCurrentHeight = $('#lightbox-container-image-box').height();
    // Get the width and height of the selected image plus the padding
    var intWidth = (intImageWidth + (0* 2)); // Plus the image´s width and the left and right padding value
    var intHeight = (intImageHeight + (0 * 2)); // Plus the image´s height and the left and right padding value
    // Diferences
    var intDiffW = intCurrentWidth - intWidth;
    var intDiffH = intCurrentHeight - intHeight;
    // Perfomance the effect
    $('#lightbox-container-image-box').animate({ width: intWidth, height: intHeight }, 400, function() { });
    if ((intDiffW == 0) && (intDiffH == 0)) {
        if ($.browser.msie) {
            ___pause(250);
        } else {
            ___pause(100);
        }
    }
    $('#lightbox-nav-btnPrev,#lightbox-nav-btnNext').css({ height: intImageHeight + (0 * 2) });
    $('#lightbox-container-image-data-box').css({ width: intImageWidth });
}
function _enable_keyboard_navigation() {

    $(document).keydown(function(objEvent) {
        _keyboard_action(objEvent);
    });
}
/**
* Disable the support to keyboard navigation
*
*/
function _disable_keyboard_navigation() {
    $(document).unbind();
}
/**
* Perform the keyboard actions
*
*/
function _keyboard_action(objEvent) {
    // To ie
    if (objEvent == null) {
        keycode = event.keyCode;
        escapeKey = 27;
        // To Mozilla
    } else {
        keycode = objEvent.keyCode;
        escapeKey = objEvent.DOM_VK_ESCAPE;
    }
    // Get the key in lower case form
    key = String.fromCharCode(keycode).toLowerCase();
    // Verify the keys to close the ligthBox
    if ((key == 'x') || (keycode == escapeKey)) {
        _finish();
    }

}