﻿
       /* ================================================================ 
       This copyright notice must be untouched at all times.
       Copyright (c) 2008 Stu Nicholls - stunicholls.com - all rights reserved.
       =================================================================== */

       $(document).ready(function() {
            $("#mainContent img").addClass("enlarge");
       });


       enlarge = function() {

           $('body').append('<div id="imageBacking"></div><div id="imageHolder" class="large"></div>');
           $('#imageBacking').css({ position: 'absolute', left: 0, top: 0, display: 'none', textAlign: 'center', background: '#000', zIndex: '600' });
           $('#imageHolder').css({ position: 'absolute', left: 0, top: 0, display: 'none', textAlign: 'center', zIndex: '700' });
           var imageArray = $('.enlarge');
           var total = $('.enlarge').length;
           /* preload images */
           for (q = 0; q < total; q++) {
               $(imageArray[q]).css({ cursor: 'pointer' });
               $('body').append('<img src="' + $(imageArray[q]).attr("alt") + '" style="position:absolute; left:-9999px; top:-9999px;" />');
           }

           $('.enlarge').click(function() {

               browserWindow()
               getScrollXY()

               if (height < totalY) { height = totalY; }

               $('#imageBacking').css({ width: totalX + 'px', height: '1000%', top: '0px', left: scrOfX + 'px' });
               $('#imageHolder').css({width: width + 'px', height: '0px', top: scrOfY + 25 + 'px', left: scrOfX + 'px' });
               source = $(this).attr("src");
               title = $(this).attr("title");

               $('#imageHolder').html('<img id="largeImage" src="' + source + '" style="padding:20px; margin-top:20px; background:#ffffff; display:block;" /><p>' + title + '<br /> <span class="close" >Close <b>X</b></span></p>');
               $('#imageHolder p').css({ color: '#fff', margin: '0 auto', padding: '10px 0' });

               $('#imageBacking').css({ opacity: 0.9, display: 'block' });
               $('#imageHolder').css({ display: 'block', visibility: 'hidden' });
               imgWidth = $('#largeImage').innerWidth() - 80;
               imgHeight = $('#largeImage').innerHeight() - 80;
               
//               topMargin = height - imgHeight;
//               topMargin = topMargin / 2;
//                
//               $('#imageHolder').css({width: width + 'px', height: '0px', top: topMargin + 'px', left: scrOfX + 'px' });
//               
               $('#largeImage').css({ display: 'none', height: imgHeight, width: 0, margin: '0 auto' });
               $('#imageHolder').css({ visibility: 'visible' });

               $('#largeImage').animate({ "width": imgWidth }, 500);

               $('#imageHolder p').css({ width: imgWidth + 'px', display: 'none' });
               $('#imageHolder p span').css({ float: 'right', cursor: 'pointer' });

               $('#imageHolder p').slideDown(500);


               $('.close').click(function() {
                   $('#imageBacking').hide();
                   $('#imageHolder').hide();
                   //$("#imageHolder").fadeOut("fast");
               });


           });

           /* find browser window size */
           function browserWindow() {
               width = 0
               height = 0;
               if (document.documentElement) {
                   width = document.documentElement.offsetWidth;
                   height = document.documentElement.offsetHeight;
               } else if (window.innerWidth && window.innerHeight) {
                   width = window.innerWidth;
                   height = window.innerHeight;
               }
               return [width, height];
           }
           /* find total page height */
           function getScrollXY() {
               scrOfX = 0;
               scrOfY = 0;
               if (typeof (window.pageYOffset) == 'number') {
                   scrOfY = window.pageYOffset;
                   scrOfX = window.pageXOffset;
               } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
                   scrOfY = document.body.scrollTop;
                   scrOfX = document.body.scrollLeft;
               } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
                   scrOfY = document.documentElement.scrollTop;
                   scrOfX = document.documentElement.scrollLeft;
               }
               totalY = (window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null);

               totalX = (window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null);
               return [scrOfX, scrOfY, totalY, totalX];
           }

           return false;
       }
