/*
 * GalleryBox (for jQuery)
 * version: 1.2 (05/05/2008)
 * @requires jQuery v1.2 or later
 *
 * Examples at http://famspam.com/GalleryBox/
 *
 * Licensed under the MIT:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright 2007, 2008 Chris Wanstrath [ chris@ozmm.org ]
 *
 * Usage:
 *  
 *  jQuery(document).ready(function() {
 *    jQuery('a[rel*=GalleryBox]').GalleryBox() 
 *  })
 *
 *  <a href="#terms" rel="GalleryBox">Terms</a>
 *    Loads the #terms div in the box
 *
 *  <a href="terms.html" rel="GalleryBox">Terms</a>
 *    Loads the terms.html page in the box
 *
 *  <a href="terms.png" rel="GalleryBox">Terms</a>
 *    Loads the terms.png image in the box
 *
 *
 *  You can also use it programmatically:
 * 
 *    jQuery.GalleryBox('some html')
 *
 *  The above will open a GalleryBox with "some html" as the content.
 *    
 *    jQuery.GalleryBox(function($) { 
 *      $.get('blah.html', function(data) { $.GalleryBox(data) })
 *    })
 *
 *  The above will show a loading screen before the passed function is called,
 *  allowing for a better ajaxy experience.
 *
 *  The GalleryBox function can also display an ajax page or image:
 *  
 *    jQuery.GalleryBox({ ajax: 'remote.html' })
 *    jQuery.GalleryBox({ image: 'dude.jpg' })
 *
 *  Want to close the GalleryBox?  Trigger the 'close.GalleryBox' document event:
 *
 *    jQuery(document).trigger('close.GalleryBox')
 *
 *  GalleryBox also has a bunch of other hooks:
 *
 *    loading.GalleryBox
 *    beforeReveal.GalleryBox
 *    reveal.GalleryBox (aliased as 'afterReveal.GalleryBox')
 *    init.GalleryBox
 *
 *  Simply bind a function to any of these hooks:
 *
 *   $(document).bind('reveal.GalleryBox', function() { ...stuff to do after the GalleryBox and contents are revealed... })
 *
 */
var imageListArr;

(function($) {
  $.GalleryBox = function(data, klass) {
	//$.extend($.GalleryBox.settings, {imageListArr: data.imageList});
	
	imageListArr = data.imageList;

	$.GalleryBox.loading()
    //if (data.ajax) fillGalleryBoxFromAjax(data.ajax)
     if (data.imageList) fillGalleryBoxFromImage(data.imageList[0])
   // else if (data.div) fillGalleryBoxFromHref(data.div)
   // else if ($.isFunction(data)) data.call($)
   // else $.GalleryBox.reveal(data, klass)
  }

  /*
   * Public, $.GalleryBox methods
   */
   
  $.extend($.GalleryBox, {
    settings: {
      opacity      : 0,
      overlay      : true,
      loadingImage : '/JQ/GalleryBox/loading.gif',
      closeImage   : '/JQ/GalleryBox/closelabel.gif',
      imageTypes   : [ 'png', 'jpg', 'jpeg', 'gif' ],
      GalleryBoxHtmlwNav  : '\
    <div id="GalleryBox" style="display:none;"> \
      <div class="popup"> \
        <table> \
			<tbody> \
		    <tr> \
              <td class="tl"/><td class="b"/><td class="tr"/> \
            </tr> \
            <tr> \
              <td class="b"/> \
              <td class="body"> \
                <div class="content"> \
                </div> \
                <div class="footer"> \
				  <div class="close"></div>' + "<div class='previous'></div><div class='next'></div>" + '\
                </div> \
              </td> \
              <td class="b"/> \
            </tr> \
            <tr> \
              <td class="bl"/><td class="b"/><td class="br"/> \
            </tr> \
          </tbody> \
        </table> \
      </div> \
    </div>'
    },

    loading: function() {
		
      init()
      if ($('#GalleryBox .loading').length == 1) return true
      showOverlay()

      $('#GalleryBox .content').empty()
      $('#GalleryBox .body').children().hide().end().
        append('<div class="loading"><img src="'+$.GalleryBox.settings.loadingImage+'"/></div>')

      $('#GalleryBox').css({
        top:	getPageScroll()[1] + (getPageHeight() / 10),
        left:	385.5
      }).show()

      $(document).bind('keydown.GalleryBox', function(e) {
        if (e.keyCode == 27) $.GalleryBox.close()
        return true
      })
      $(document).trigger('loading.GalleryBox')
    },

    reveal: function(data, klass) {
      $(document).trigger('beforeReveal.GalleryBox')
      if (klass) $('#GalleryBox .content').addClass(klass)
      $('#GalleryBox .content').append(data)
      $('#GalleryBox .loading').remove()
      $('#GalleryBox .body').children().fadeIn('normal')
      $('#GalleryBox').css('left', $(window).width() / 2 - ($('#GalleryBox table').width() / 2))
      $(document).trigger('reveal.GalleryBox').trigger('afterReveal.GalleryBox')
    },

    close: function() {
      $(document).trigger('close.GalleryBox')
      return false
    }
  })



  /*
   * Private methods
   */

  // called one time to setup GalleryBox on this page
  function init(settings) {

   if ($.GalleryBox.settings.inited) return true
    else $.GalleryBox.settings.inited = true

    $(document).trigger('init.GalleryBox')
    makeCompatible()

    var imageTypes = $.GalleryBox.settings.imageTypes.join('|')
    $.GalleryBox.settings.imageTypesRegexp = new RegExp('\.' + imageTypes + '$', 'i')

    if (settings) $.extend($.GalleryBox.settings, settings)

	$('body').append($.GalleryBox.settings.GalleryBoxHtmlwNav);
	
    var preload = [ new Image(), new Image() ]
    preload[0].src = $.GalleryBox.settings.closeImage
    preload[1].src = $.GalleryBox.settings.loadingImage

    $('#GalleryBox').find('.b:first, .bl, .br, .tl, .tr').each(function() {
      preload.push(new Image())
      preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1')
    })

    $('#GalleryBox .close').click($.GalleryBox.close)
    //$('#GalleryBox .close_image').attr('src', $.GalleryBox.settings.closeImage)
	
	$('#GalleryBox .previous').click(function(){
		//console.log(imageListArr);
		RotateArr(imageListArr,1);
		$('#GalleryBox .content').empty();
		fillGalleryBoxFromImage(imageListArr[0]);
		//$.GalleryBox({ imageList: RotateArr(imageListArr,1)});
	})
	$('#GalleryBox .next').click(function(){
		//console.log(imageListArr);
		RotateArr(imageListArr,-1);
		$('#GalleryBox .content').empty();
		fillGalleryBoxFromImage(imageListArr[0]);		
		//$.GalleryBox({ imageList: RotateArr(imageListArr,-1)});
	})
  }
  
  // getPageScroll() by quirksmode.com
  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;	
    }
    return new Array(xScroll,yScroll) 
  }

  // Adapted from getPageSize() by quirksmode.com
  function getPageHeight() {
    var windowHeight
    if (self.innerHeight) {	// all except Explorer
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowHeight = document.body.clientHeight;
    }	
    return windowHeight
  }

  // Backwards compatibility
  function makeCompatible() {
    var $s = $.GalleryBox.settings

    $s.loadingImage = $s.loading_image || $s.loadingImage
    $s.closeImage = $s.close_image || $s.closeImage
    $s.imageTypes = $s.image_types || $s.imageTypes
    $s.GalleryBoxHtmlwNav = $s.GalleryBox_html || $s.GalleryBoxHtmlwNav
  }

  function fillGalleryBoxFromImage(href, klass) {
    var image = new Image()
    image.onload = function() {
      $.GalleryBox.reveal('<div class="image"><img src="' + image.src + '" /></div>', klass)
    }
    image.src = href
  }

  function fillGalleryBoxFromAjax(href, klass) {
    $.get(href, function(data) { $.GalleryBox.reveal(data, klass) })
  }

  function skipOverlay() {
    return $.GalleryBox.settings.overlay == false || $.GalleryBox.settings.opacity === null 
  }

  function showOverlay() {
    if (skipOverlay()) return

    if ($('GalleryBox_overlay').length == 0) 
      $("body").append('<div id="GalleryBox_overlay" class="GalleryBox_hide"></div>')

    $('#GalleryBox_overlay').hide().addClass("GalleryBox_overlayBG")
      .css('opacity', $.GalleryBox.settings.opacity)
      .click(function() { $(document).trigger('close.GalleryBox') })
      .fadeIn(200)
    return false
  }

  function hideOverlay() {
    if (skipOverlay()) return

    $('#GalleryBox_overlay').fadeOut(200, function(){
      $("#GalleryBox_overlay").removeClass("GalleryBox_overlayBG")
      $("#GalleryBox_overlay").addClass("GalleryBox_hide") 
      $("#GalleryBox_overlay").remove()
    })
    
    return false
  }

  /*
   * Bindings
   */

  $(document).bind('close.GalleryBox', function() {
    $(document).unbind('keydown.GalleryBox')
    $('#GalleryBox').fadeOut(function() {
      $('#GalleryBox .content').removeClass().addClass('content')
      hideOverlay()
      $('#GalleryBox .loading').remove()
    })
  })

})(jQuery);


function RotateArr(arr, dir)  {
	if (parseInt(dir) > 0) {
		var last = arr.pop();
		arr.reverse();
		arr.push(last);
		arr.reverse();
	} else {
		arr.reverse();
		var first = arr.pop();
		arr.reverse();
		arr.push(first);
		}
	return arr;
}


