/*
 * Facebox (for jQuery)
 * version: 1.1 (01/20/2007)
 * @requires jQuery v1.2 or later
 *
 * Examples at http://famspam.com/facebox/
 *
 * Licensed under the MIT:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright 2007 Chris Wanstrath { chris@ozmm.org }
 *
 * Usage:
 *  
 *  $j(document).ready(function(){
 *    $j('a[rel*=facebox]').facebox() 
 *  })
 *
 *  <a href="#terms" rel="facebox">Terms</a>
 *    Loads the #terms div in the box
 *
 *  <a href="terms.html" rel="facebox">Terms</a>
 *    Loads the terms.html page in the box
 *
 *  <a href="terms.png" rel="facebox">Terms</a>
 *    Loads the terms.png image in the box
 *
 *
 *  You can also use it programmatically:
 * 
 *    jQuery.facebox('some html')
 *
 *  This will open a facebox with "some html" as the content.
 *    
 *    jQuery.facebox(function($) { $j.ajaxes() })
 *
 *  This will show a loading screen before the passed function is called,
 *  allowing for a better ajax experience.
 *
 */
(function($j) {
 /**
   *  The static facebox() function, can be passed a string or
   *  function.
   *
   *  You can also use it programmatically:
   * 
   *    jQuery.facebox('some html')
   *
   *  This will open a facebox with "some html" as the content.
   *    
   *    jQuery.facebox(function($) { $j.ajaxes() })
   *
   *  This will show a loading screen before the passed function is called,
   *  allowing for a better ajax experience.
   */
  $j.facebox = function(data, e) {
    facebox_init()
    facebox_loading(e)
    $j.isFunction(data) ? data.call(this, $j) : facebox_reveal(data)
    return $j;
  }

 /**
   * Facebox settings, which can be modified through the facebox() method 
   * or directly.
   *
   *    jQuery('a[rel*=facebox]').facebox({ loading_image: '//cscommerce/images/spinner.gif' })
   *
   *    jQuery.facebox.settings.loading_image = '//cscommerce/images/spinner.gif'
   */
  $j.facebox.settings = {
	title: null, // where to look for the gallery title, ie: jQuery('a[rel*=facebox]').facebox({ title:'div.pictures ul' })
    loading_image : '/apache/images/facebox/facebox_loading.gif',
    close_image   : '/apache/images/facebox/facebox_close.png',
    image_types   : [ 'png', 'jpg', 'jpeg', 'gif' ],
    play_image    : '/apache/images/facebox/play.gif',
    pause_image   : '/apache/images/facebox/pause.gif',
    slide_duration: 6,
    is_note       : false,
    scroll        : true,
    x_offset      : 10,
    y_offset      : 10,
    facebox_html  : '\
  <div id="facebox" 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="top"> \
                <a href="#" class="close"> \
                  <img src="/apache/images/facebox/facebox_close.png" title="close" class="close_image" /> \
                </a> \
              </div> \
              <div class="content"> \
              </div> \
              <div class="footer"> \
                <div class="navigation"></div> \
				<div class="info"></div> \
              </div> \
            </td> \
            <td class="b"/> \
          </tr> \
          <tr> \
            <td class="bl"/><td class="b"/><td class="br"/> \
          </tr> \
        </tbody> \
      </table> \
    </div> \
  </div>'
  }
  var $s = $j.facebox.settings

  $j.fn.facebox = function(settings) {
    facebox_init(settings)
	// save the gallery title
	var list_title = null;
	// if there is a title to look for, save it
	if(settings) if(settings.title != null) list_title = $j(settings.title).attr('title');

    var image_types = $s.image_types.join('|')
    image_types = new RegExp('\.' + image_types + '$', 'i')

    // suck out the images and titles of them
    var images = []
    var titles = []
    $j(this).each(function() {
      if (this.href.match(image_types) && $j.inArray(this.href, images) == -1) 
        images.push(this.href)
		titles.push(this.title)
    })
    if (images.length == 1) {
		images = null 
		titles = null
	}

    function click_handler() {
      if ($j('#facebox .loading').length == 1) return false
      facebox_loading()

      // support for rel="facebox[.inline_popup]" syntax, to add a class
      var klass = this.rel.match(/facebox\[\.(\w+)\]/)
      if (klass) klass = klass[1]
	  
	  if($s.scroll) {
	  	$j('div#facebox').addClass("scroll");
	  } else {
	  	$j('div#facebox').addClass("noscroll");
	  }
	  
	  // remove all facebox classes from the body
	  $j('body').removeClass('facebox_noinfo').removeClass('facebox_image').removeClass('facebox_network');

      // div
     if (this.href.match(/#/)) {
        var url    = window.location.href.split('#')[0];
        var target = this.href.replace(url,'');
		// add facebox_noinfo class to the body, this class removes the caption div from facebox
		$j('body').addClass('facebox_noinfo');
        facebox_reveal($j(target).clone(true).show(), klass);

      // image
      } else if (this.href.match(image_types)) {
		  
		// add facebox_image class to the body, this class sets a gray background to the facebox 
		$j('body').addClass('facebox_image');
		  
		if(!images) {
			// if it is not a gallery, removes the caption div from the facebox, and add a title to show above the image
			$j('body').addClass('facebox_noinfo');
			list_title = this.title;
		}
        facebox_reveal_image(this.href, images, klass, list_title, titles, this.title)

      // ajax
      } else {
		// if it is the calling area facebox, add class facebox_network to the body
		if($j(this).hasClass("network_area")){
			$j('body').addClass('facebox_network');
		}
        //$j.get(this.href + "?random=" + (new Date().getTime()), function(data) { $j('body').addClass('facebox_noinfo'); facebox_reveal(data, klass); } )
        $j.get(this.href, function(data) { $j('body').addClass('facebox_noinfo'); facebox_reveal(data, klass); } )
      }
		// if it is genie facebox
		if($j(this).hasClass("genie")){
			// removes previous facebox overlay
			$j('#facebox_overlay').remove();
			// add facebox overlay
		    $j('#wrapper').after('<div id="facebox_overlay"></div>');
		    $j('body').addClass('genie');
		    var pageSize = getPageSize();
		    $j('#facebox_overlay').width(pageSize[0]).height(pageSize[1]).css("opacity",0.6);
		}
		// if outsideClickClose, close facebox if user clicks outside of the facebox
		if ($j(this).hasClass('outsideClickClose')) {
			$j('#facebox_overlay, .popup').click(facebox_close);
		    $j('.popup table').bind("click", function(event){
			  event.stopPropagation();
			});
		}

		// if hideCloseButton, hide the default close
		if ($j(this).hasClass('hideCloseButton')) {
			$j('div.popup div.top a.close').hide();
			// if any elements in the popup have the close class, make it work like the default close
			$j('#facebox .close').click(facebox_close);
		}
		
        return false;
    }

    return this.click(click_handler);
  }

/**
  * The init function is a one-time setup which preloads vital images
  * and other niceities.
  */
  function facebox_init(settings) {
    if ($s.inited && typeof settings == 'undefined')
      return true
    else 
      $s.inited = true

    if (settings) $j.extend($s, settings)

    $j('body').append($s.facebox_html)

    var preload = [ new Image(), new Image() ]
    preload[0].src = $s.close_image
    preload[1].src = $s.loading_image

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

    $j('#facebox .close').click(facebox_close)
    $j('#facebox .close_image').attr('src', $s.close_image)
  }

/**
  * The loading function prepares the facebox by displaying it
  * in the proper spot, cleaning its contents, attaching keybindings 
  * and showing the loading image.
  */
  function facebox_loading(event) {
    if ($j('#facebox .loading').length == 1) return true
    $j(document).unbind('.facebox')
    $j('#facebox .content, #facebox .info, #facebox .navigation').empty()
    $j('#facebox .body').children().hide().end().
      append('<div class="loading"><img src="'+$s.loading_image+'"/></div>')
	if($s.is_note && event != null){
		 var pageScroll = getPageScroll()
	    $j('#facebox').css({
	      top:	event.pageY - 20,
	      left:	event.pageX - 450,
	      width: "auto"
	    }).show()
	} else {
	    var pageScroll = getPageScroll()
	    $j('#facebox').css({
	      top:	pageScroll[1] + (getPageHeight() / 10) - 30,
	      left:	pageScroll[0]
	    }).show()
    }

    $j(document).bind('keydown.facebox', function(e) {
      if (e.keyCode == 27) facebox_close()
    })
  }

/**
  * The facebox_reveal function sets the user-defined class (if any)
  * on the .content div, removes the loading image, and displays
  * the data.  If an extra_setup functino is provided, it will be run
  * right before the data is displayed but after it is added.
  */
  function facebox_reveal(data, klass, extra_setup) {
    $j('#facebox .content').addClass(klass).append(data)
    $j('#facebox .loading').remove()
    if ($j.isFunction(extra_setup)) extra_setup.call(this)
    $j('#facebox .body > *').fadeIn('fast')
  }

/**
  * Used to load and show an image in the facebox.  Involved in the slideshow business.
  */
  function facebox_reveal_image(href, images, klass, list_title, titles, image_title) {
    if (images) var extra_setup = facebox_setup_gallery(href, images, klass, list_title, titles)
    var image    = new Image()
    image.onload = function() {
		var t = '';
		// if there is a title, show it above the image
		if(list_title) {
			if(list_title.indexOf('/') > -1) {
				// splits the title in 2 lines (Title / Subtitle)
				t = '<p class="title"><strong>' + list_title.split(' / ')[0] + ' </strong>' + list_title.split(' / ')[1] + '</p>';
			} else {
				t = '<p class="title"><strong>' + list_title + ' </strong>';
			}	
		}
      facebox_reveal(t+'<div class="image"><img src="' + image.src + '" /></div>', klass, extra_setup)
      // load the next image in the background
      if (images) {
        var position = $j.inArray(href, images)
        var next = new Image()
        next.src = images[position+1] ? images[position+1] : images[0]
      }
    }
    image.src = href
  }

/**
  * Unbinds all listeners and closes the facebox.
  */
	function facebox_close(){
		$('.facebox_video_player').remove();
		facebox_stop_slideshow()
		$j(document).unbind('.facebox')
		$j('#facebox').fadeOut('fast',function(){
			$j('#facebox .content').removeClass().addClass('content')
			// If it was a genie, remove facebox overlay and genie class on body.
			$j('#facebox_overlay').remove();
			$j('body').removeClass('genie');
		})
		return false
	}

  function facebox_setup_gallery(href, images, klass, title, titles) {
    var position = $j.inArray(href, images);
	var info_title = titles[position]
	//$j('body').removeClass('genie');
    var jump = function(where) {
      facebox_loading()
      if (where >= images.length) where = 0
      if (where < 0) where = images.length - 1
      facebox_reveal_image(images[where], images, klass, title, titles, titles[where])
    }

    return function() {
      $j('#facebox .image').click(function() { jump(position + 1) }).css('cursor', 'pointer')
      $j('#facebox .info').append( info_title )
      $j('#facebox .navigation').
        append('<div class="prev"></div>' +
          '<div class="next"></div>').
        find('img').css('cursor', 'pointer').end().
        find('.prev').click(function() { jump(position - 1); return false }).end().
        find('.next').click(function() { jump(position + 1); return false }).end()

      $j('#facebox .play').bind('click.facebox', function() {
        $s.playing ? facebox_stop_slideshow() : facebox_start_slideshow()
        return false
      })

      $j(document).bind('keydown.facebox', function(e) {
        if (e.keyCode == 39) jump(position + 1) // right
        if (e.keyCode == 37) jump(position - 1) // left
      })
    }
  }

  function facebox_start_slideshow() {
    $j('#facebox .play').attr('src', $s.pause_image)
    $s.playing = setInterval(function() { $j('#facebox .next').click() }, $s.slide_duration * 1000)
  }

  function facebox_stop_slideshow() {
    $j('#facebox .play').attr('src', $s.play_image)
    clearInterval($s.playing)
    $s.playing = false
  }

  // 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) 
  }

  // adapter 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
  }

	//
	//  getPageSize()
	//
	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;
		}	
		var pageHeight;
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		var pageWidth;
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}

})(jQuery);

$j(document).ready(function(){
	function contentGrabber(event)
	{
		var xhr = new XHR();
		var clickedElem = event.target || event.srcElement;
		url = clickedElem.href;
		xhr.open("GET", url, true);
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4){
				jQuery.facebox(xhr.responseText, event);
			}
		}
		xhr.send(null);
	}
	$j('a[rel*=facebox]').facebox();
	
	$j('a[rel*=big-image]').facebox();
	
	$j('a[rel*=rating]').facebox();
	
	$j('a[rel*=vid-facebox]').facebox( { scroll : false } );
	
	$j('a[rel*=facebox-note-h-l]').facebox( { scroll : false, is_note : true } );
	$j('a[rel*=facebox-note-h-l]').unbind("click");
	$j('a[rel*=facebox-note-h-l]').click(function(event){stopEvent(event); contentGrabber(event);});
});
/*	Transferred to Global.js
var ie = document.all?true:false;

function focusMe()
{
	//IE6 standards compliant mode
	//DOM standards compliant mode (document.body.scrollLeft)
	var docLeft= ie ? document.documentElement.scrollLeft : window.pageXOffset;
	var docTop= ie ? document.documentElement.scrollTop : window.pageYOffset;
	var mydiv = document.getElementById('facebox');
	if (mydiv != null) {
		mydiv.style.top = (docTop + 25) + 'px';
	}
}

window.onscroll = focusMe;
**/