﻿/// <reference path="jquery-1.4.1.min.js" />
var image = new Array();
var c = 0, grp = 0, grpc = 0, grpw = 125, _e;
var play = true, ppbutton = true, fullscr = false;
var duration = 6000, t;
var top, left;
var scrollTop, scrollLeft;
var $J = jQuery.noConflict();
var _pntrimg = new Image();
_pntrimg.src = 'http://www.miamiarch.org/images/slideshow/_imgpointer.png';

/* ********************** images to operate the slideshow ********************** */

var toolBK = 'images/slideshow/toolBK.gif';
var thumbBK = 'images/slideshow/thumBK.png';
var backward = 'images/slideshow/img_Backward.png';
var back = 'images/slideshow/img_Minus.png';
var next = 'images/slideshow/img_Plus.png';
var forward = 'images/slideshow/img_Forward.png';
var playpause = 'images/slideshow/img_PlayPause.png';
var fullscreen = 'images/slideshow/img_FullScreen.png';

/* ********************** dimensions ********************** */

//var _cntHeight = 360, _cntWidth = 450; /* container: height; width - editable, all - dependant */
var _cntHeight = 390, _cntWidth = 450; /* container: height; width - editable, all - dependant */
var _div00_height = 290; /* height - editable, imageshow - dependant */
var _isHeight = _div00_height; /* imageshow: height - editable,  window; tools - dependant */
var _tlHeight = 29; /* tools: height - editable, window - dependant (dont change this value) */

var pbStyle = 'font-family: Courier New; font-size: smaller; font-style: italic;'; // style for 'Photo By'

$J.fn.slideShow = function () {

    /* ********************** do not edit the skeleton ********************** */

    var skeleton = '<div class="container" style="position: relative; top: 0px; left: 0px; z-index: 0; height: ' + _cntHeight + 'px; width: ' + _cntWidth + 'px; padding: 5px; background: #fffde8; border: 1px solid #846d04;">' +
                '<div style="position: relative; top: 0px; left: 0px; z-index: 1; height: ' + _div00_height + 'px; width: 100%;">' +
                '<div class="imageshow" style="position: relative; top: 0px; left: 0px; z-index: 1; height: ' + _isHeight + 'px; width: 100%;">' +
                '<div class="window" align="center" style="position: relative; top: 0px; left: 0px; z-index: 1; height: ' + (_isHeight - _tlHeight) + 'px; width: 100%; background: #303030;"></div>' +
                '<div class="tools" style="position: relative; top: auto; bottom: 0px; left: 0px; z-index: 1; height: ' + _tlHeight + 'px; width: 100%; background: url(' + toolBK + ') repeat-x;">' +
                '<table border="0" cellpadding="0" cellspacing="0" style="width: 280px; height: 100%; background-color: transparent;" align="center">' +
                '<tr><td style="text-align: center;"><div align="center"><div class="prvImgs" style="height: 15px; width: 15px; background: transparent url(' + backward + ') no-repeat 0 0; vertical-align: middle;"></div></div></td>' +
                '<td style="text-align: center;"><div align="center"><div class="prvImg" style="height: 15px; width: 15px; background: transparent url(' + back + ') no-repeat 0 0; vertical-align: middle;"></div></div></td>' +
                '<td style="width: 114px; height: 25px;"><div style="position: relative; height: 25px; width: 114px; background: transparent url(' + thumbBK + ') center no-repeat; vertical-align: middle;"><table border="0" cellpadding="0" cellspacing="0" style="height: 100%; width: 100%;">' +
                '<tr valign="middle" style="height: 100%; width: 100%;"><td style="height: 100%; width: 100%; padding: 0px 0px 0px 10px;"><div class="imgThmbCntnr" style="position: relative; z-index: 0; top: 0px; left: 0px; vertical-align: middle; height: 17px; width: 100%; overflow: hidden;">' +
                '<div style="position: absolute; z-index: 0; top: 0px; left: 0px; height: 100%; width: 100%;" ></div></div></td></tr></table></div></td>' +
                '<td style="text-align: center;"><div align="center"><div class="nxtImg" style="height: 15px; width: 15px; background: transparent url(' + next + ') no-repeat 0 0; vertical-align: middle;"></div></div></td>' +
                '<td style="text-align: center;"><div align="center"><div class="nxtImgs" style="height: 15px; width: 15px; background: transparent url(' + forward + ') no-repeat 0 0; vertical-align: middle;"></div></div></td>' +
                '<td style="text-align: center;"><div align="center"><div class="playpause" style="height: 15px; width: 15px; background: transparent url(' + playpause + ') no-repeat 0 0; vertical-align: middle;"></div></div></td>' +
                '<td style="text-align: center;"><div align="center"><div class="fullscr" style="height: 15px; width: 16px; background: transparent url(' + fullscreen + ') no-repeat 0 0; vertical-align: middle;"></div></div></td></tr></table></div></div></div>' +
                '<div class="textshow" style="position: relative; top: 0px; left: 0px; z-index: 1; height: 95px; width: 100%; background: transparent; vertical-align: bottom; text-align: left; color: #846d04"></div></div>';
    $J(this).append(skeleton);

    $J.ajax({
        type: "GET",
        url: xmlfile,
        dataType: "xml",
        success: function (xml) {
            $J(xml).find('Image').each(function (e) {
                image[e] = new Array();
                image[e][0] = new Image();
                image[e][0].src = $J(this).find('Url').text();
                image[e][1] = $J(this).find('Data').text();
                image[e][2] = (jQuery.trim(image[e][1]) != '' ? '<br />' : '') + '<span style="' + pbStyle + '">Photo By: ' + $J(this).find('PhotoBy').text() + '</span>';
                if ((_e = e % 4) == 0) {
                    grp++;
                }
                if (_e == 0)
                    left = 17 * _e + (grp - 1) * grpw;
                else
                    left = 17 * _e + 5 * _e + (grp - 1) * grpw;
                $J('.imgThmbCntnr div').append('<img id="imgThumb' + (e + 1) + '" alt="" src="' + image[e][0].src +
                        '" style="height: 15px; width: 15px; border: 1px solid Gray; margin: 0px 5px 0px 0px; vertical-align: middle;' +
                        ' position: absolute; top: 0px; left: ' + left + 'px" />');
            });
            rotate();
            $J(window).resize(function () {
                scrollTop = $J(window).scrollTop();
                scrollLeft = $J(window).scrollLeft();
                if (fullscr) {
                    $J('.fullwin').css({
                        'top': scrollTop,
                        'left': scrollLeft,
                        'height': $J(window).height(),
                        'width': $J(window).width()
                    });                    
                    $J('.window').css({
                        'height': $J('.fullwin').height() - 29,
                        'width': $J('.fullwin').width()
                    });
                    $J('.window div img').css({
                        'height': $J('.window').height()
                    });
                }
            });
            $J('.imgThmbCntnr img').hover(function () {
                var center = $J(this).position().left + Math.round($J(this).width() / 2);
                var middle = $J(this).position().top;
                var _pointerimg = '<img alt="" src="' + _pntrimg.src + '" style="border: 0px;" />';
                var pointer = '<div id="pointer" align="center" style="position: absolute; left:' + (center + 10 - (grpc * grpw)) + 'px; top: auto; bottom: 22px;">' +
                        '<div class="pointer" style="position: absolute; top: auto; bottom: 0px; left: -6px">' + _pointerimg + '</div></div>';
                $J('.imgThmbCntnr').parent().append($J(pointer));
                var imgthumbpre = '<div id="imgThmbPre" class="imgThmbPre" style="position: absolute; top: auto; bottom: 4px; width: 0px; height: 0px; ' +
                        'border: 0px solid #fcfcfc; background: #fcfcfc"><img alt="" class="test" src="' + $J(this).attr('src') + '" style="display: none;" />' +
                        '</div>';
                $J('#pointer').append($J(imgthumbpre));
                $J('.test').css({ 'height': 70 });
                var _width = $J('.test').width();
                var _height = $J('.test').height();
                $J('#imgThmbPre').animate({ 'height': _height, 'width': _width, 'left': -(_width / 2 + 4), 'top': -(_height + $J('.pointer').height() + 4), 'border-width': 2 }, 50, function () {
                    $J('.test').show();
                });
            }, function () { $J('#pointer').remove(); });
            $J('.imgThmbCntnr img').click(function () {
                c = $J(this).attr('id').substr(8) - 1;
                play = false;
                rotate();
            });
            $J('.prvImgs').click(function () {
                if (grpc > 0) {
                    grpc = 0;
                    c = 0;
                    play = false;
                    $J('.imgThmbCntnr div').animate({ 'left': 0 }, 500, function () { });
                    rotate();
                }
            });
            $J('.prvImg').click(function () {
                clearTimeout(t);
                play = false;
                if (c != 0) { c--; }
                if (c % 4 == 3 && grpc > 0) { grpc--; $J('.imgThmbCntnr div').animate({ 'left': '+=' + grpw }, 500, function () { }); }
                rotate();
            });
            $J('.nxtImg').click(function () {
                clearTimeout(t);
                play = false;
                if (c != image.length - 1) { c++; }
                if (c % 4 == 0 && grpc < grp - 1) { grpc++; $J('.imgThmbCntnr div').animate({ 'left': '-=' + grpw }, 500, function () { }); }
                rotate();
            });
            $J('.nxtImgs').click(function () {
                if (grpc < grp - 1) {
                    grpc = grp - 1;
                    c = grpc * 4;
                    play = false;
                    $J('.imgThmbCntnr div').animate({ 'left': -(grp - 1) * grpw }, 500, function () { });
                    rotate();
                }
            });
            $J('.playpause').click(function () {
                ppbutton = true;
                play = !play;
                rotate();
            });
            $J('.fullscr').click(function () {
                scrollTop = $J(window).scrollTop();
                scrollLeft = $J(window).scrollLeft();
                if (!fullscr) {
                    $J('body').css({ 'overflow': 'hidden' });
                    var fullwin = '<div class="fullwin" style="position: absolute; z-index: 9999; height: ' + $J('.imageshow').height() + 'px; width: ' + $J('.imageshow').width() + 'px; top: ' + $J('.imageshow').offset().top + 'px; left: ' + $J('.imageshow').offset().left + 'px; background: #303030;"></div>';
                    $J('body').append(fullwin);
                    $J('.fullwin').animate({
                        'top': scrollTop,
                        'left': scrollLeft,
                        'height': $J(window).height(),
                        'width': $J(window).width()
                    }, 1000, function () {
                        $J('.fullwin').append($J('div.imageshow').hide());
                        $J('.window').css({ 'height': $J('.fullwin').height() - 29, 'width': $J('.fullwin').width() });
                        $J('.window div img').css({ 'height': $J('.window').height() });
                        $J('div.imageshow').fadeIn();
                        reRotate();
                    });
                    $J(this).css({
                        'background-position': ' 0 -20px'
                    }).attr({
                        'title': 'Restore'
                    }); ;
                    fullscr = !fullscr;
                }
                else if (fullscr) {
                    $J('body').css({ 'overflow': 'scroll' });
                    $J('div.imageshow').hide();
                    $J('div.imageshow').appendTo('.container div:eq(0)');                    
                   if (fullscr)
	            	  	 {	
	            	  	 	v_madita_largura=$J('.container').width();
	            	 		 }
            	 		 else
            	  	 {
	            	  	 	v_madita_largura=_cntWidth;
	    	 		 				}                                
                    
                    $J('.fullwin').animate({
                        'top': $J('.container').offset().top + 5,
                        'left': $J('.container').offset().left + 5,
                        'height': _isHeight,
                        'width': v_madita_largura
                    }, 1000, function () {                    	
				                   if (fullscr)
				            	  	 {
				            	  	 	v_madita_altura=$J('.fullwin').height() - 29;
				            	  	 	v_madita_largura=$J('.fullwin').width();
				            	 		 }
				            	 		 else
				            	  	 {
				            	  	 	v_madita_altura=(_isHeight - _tlHeight);
				            	  	 	v_madita_largura=_cntWidth;
            	 		 				}                    	
                        $J('.window').css({ 'height':v_madita_altura,'width':v_madita_largura });
                        $J('.window div img').css({ 'height': $J('.window').height() });
                        $J('div.imageshow').fadeIn();
                        reRotate();
                    });
                    $J('.fullwin').fadeOut('slow', function () { $J('.fullwin').remove(); });
                    $J(this).css({ 'background-position': ' 0 0' }).attr({ 'title': 'Full Screen' });
                    fullscr = !fullscr;
                }
            });
            function reRotate() {
            	     if (fullscr)
            	  	 {
            	  	 	v_madita_altura=$J('.window').height();
            	 		 }
            	 		 else
            	  	 {
            	  	 	v_madita_altura=(_isHeight - _tlHeight);
            	 		 }
            	
                if ($J('.window div img').attr('src') == $J('.imgThmbCntnr img:eq(' + c + ')').attr('src')) {
                    var _thisImg = $J('<img src="' + $J('.imgThmbCntnr img:eq(' + c + ')').attr('src') + '" height="' + v_madita_altura + '" border="0px" />');
                    $J('.window div').addClass('remove').removeClass('add');
                    $J('.window div.remove').fadeOut(function () { $J('div.remove').remove(); });
                    $J(_thisImg).css({ 'height': v_madita_altura, 'border': '0px none' });
                    $J('.window').append('<div class="add" align="center" style="position: absolute; top: 0px; left: 0px; height: 100%; width: 100%; display: none;"></div>');
                    $J('.window div.add').append($J(_thisImg));
                    $J('.window div.add').fadeIn();
                    $J('.textshow').html(image[c][1] + ' ' + image[c][2]);
                }
            }
            function rotate() {
            	  if (fullscr)
            	  	 {
            	  	 	v_madita_altura=$J('.window').height();
            	 		 }
            	 		 else
            	  	 {
            	  	 	v_madita_altura=(_isHeight - _tlHeight);
            	 		 }            	  
            	  
                clearTimeout(t);
                if (c == image.length)
                    c = 0;
                $J('.imgThmbCntnr img').removeClass('selected').css({ 'cursor': 'pointer', 'border-color': 'Gray' });
                $J('.imgThmbCntnr img:eq(' + c + ')').addClass('selected').css({ 'cursor': 'default', 'border-color': 'Red' });
                if ($J('.window div img').attr('src') != $J('.imgThmbCntnr img:eq(' + c + ')').attr('src')) {
                    var _thisImg = $J('<img src="' + $J('.imgThmbCntnr img:eq(' + c + ')').attr('src') + '" height="' +v_madita_altura+ '" border="0px" />');
                    $J('.window div').addClass('remove').removeClass('add');
                    $J('.window div.remove').fadeOut(function () { $J('div.remove').remove(); });
                    $J(_thisImg).css({ 'height': v_madita_altura, 'border': '0px none' });
                    $J('.window').append('<div class="add" align="center" style="position: absolute; top: 0px; left: 0px; height: 100%; width: 100%; display: none;"></div>');
                    $J('.window div.add').append($J(_thisImg));
                    $J('.window div.add').fadeIn();
                    $J('.textshow').html(image[c][1] + ' ' + image[c][2]);
                }
                fn_buttons();
                if (play) {
                    if (c % 4 == 0 && grpc < grp - 1 && !ppbutton) {
                        grpc++;
                        $J('.imgThmbCntnr div').animate({ 'left': '-=' + grpw }, 500, function () { });
                    } else if (c == 0) {
                        grpc = 0;
                        $J('.imgThmbCntnr div').animate({ 'left': grpc }, 500, function () { });
                    }
                    t = setTimeout(function () {
                        ppbutton = (c % 4 == 3) ? false : true;
                        c++;
                        rotate();
                    }, duration);
                }
            }
            function fn_buttons() {
                if (play) $J('.playpause').css({ 'background-position': '0 0' }).attr({ 'title': 'Pause' });
                else if (!play) $J('.playpause').css({ 'background-position': '0 -20px' }).attr({ 'title': 'Play' });
                if (fullscr) $J('.fullscr').css({ 'background-position': '0 -20px' }).attr({ 'title': 'Restore' });
                else $J('.fullscr').css({ 'background-position': '0 0' }).attr({ 'title': 'Full Screen' });
                if (grpc == grp - 1) $J('.nxtImgs').css({ 'background-position': '0 0' }).removeAttr('title');
                else $J('.nxtImgs').css({ 'background-position': '0 -20px' }).attr({ 'title': 'Last' });
                if (grpc == 0) $J('.prvImgs').css({ 'background-position': '0 0' }).removeAttr('title');
                else $J('.prvImgs').css({ 'background-position': '0 -20px' }).attr({ 'title': 'First' });
                if (c == image.length - 1) $J('.nxtImg').css({ 'background-position': '0 0' }).removeAttr('title');
                else $J('.nxtImg').css({ 'background-position': '0 -20px' }).attr({ 'title': 'Next' });
                if (c == 0) $J('.prvImg').css({ 'background-position': '0 0' }).removeAttr('title');
                else $J('.prvImg').css({ 'background-position': '0 -20px' }).attr({ 'title': 'Prev' });
            }
        }
    });
};
