/*
 * Facebox (for jQuery)
 * version: 1.1 (03/01/2008)
 * @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, 2008 Chris Wanstrath [ chris@ozmm.org ]
 */
var destination = '';
var scrollto = '';
(function($) {
  $.facebox = function(data, klass) {
    $.facebox.loading()
    $.isFunction(data) ? data.call($) : $.facebox.reveal(data, klass)
  }

  $.facebox.settings = {
    loading_image : '//image1.fmgstatic.com/jscript/facebox/loading.gif',
    close_image   : '//image1.fmgstatic.com/jscript/facebox/closelabel.gif',
    print_image   : '//image1.fmgstatic.com/jscript/facebox/printlabel.png',
    image_types   : [ 'png', 'jpg', 'jpeg', 'gif' ],
	form_input_types : ['text','hidden','select'],
    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="faceboxheader"> \
					<a href="#" class="print"> \
					  <img src="//image1.fmgstatic.com/jscript/facebox/temp_img.gif" title="Print" class="print_image" /> \
					</a> \
					<a href="#" class="close"> \
					  <img src="//image1.fmgstatic.com/jscript/facebox/temp_img.gif" title="Close" class="close_image" /> \
					</a> \
				</div> \
              <div class="faceboxcontent"> \
              </div> \
              <div class="footer"> \
				<a href="#" class="print"> \
				  <img src="//image1.fmgstatic.com/jscript/facebox/temp_img.gif" title="Print" class="print_image" /> \
				</a> \
                <a href="#" class="close"> \
                  <img src="//image1.fmgstatic.com/jscript/facebox/temp_img.gif" title="Close" class="close_image" /> \
                </a> \
              </div> \
            </td> \
            <td class="b"/> \
          </tr> \
          <tr> \
            <td class="bl"/><td class="b"/><td class="br"/> \
          </tr> \
        </tbody> \
      </table> \
    </div> \
  </div>'
  }

  $.facebox.loading = function() {
    if ($('#facebox .loading').length == 1) return true

    $('#facebox .faceboxcontent').empty()
    $('#facebox .body').children().hide().end().
      append('<div class="loading"><img src="'+$.facebox.settings.loading_image+'"/></div>')
    var pageScroll = $.facebox.getPageScroll()
    $('#facebox').css({
      top:	pageScroll[1] + ($.facebox.getPageHeight()/10),
      left:	pageScroll[0]
    }).show()
		//$('#facebox .faceboxcontent').css({ height: $.facebox.getPageHeight()/1.8 })
		

	$('.faceboxcontent').css({height: $.facebox.getPageHeight()/1.5 - $.facebox.getPageHeight()/20});

    $(document).bind('keydown.facebox', function(e) {
      if (e.keyCode == 27) $.facebox.close()
    })
  }

  $.facebox.reveal = function(data, klass) {
	printElements("#content,.faceboxheader,.footer,#topwrap,.bottomwrap,.copyrightinfo,#btmlinks");
    if (klass) $('#facebox .faceboxcontent').addClass(klass)
    $('#facebox .faceboxcontent').append(data)
    $('#facebox .loading').remove()
	var scrolling = 1;
	if(scrollto != true) {
		var mainBody = $('#facebox .faceboxcontent').get(0);
		$('#facebox .body').children().fadeIn('normal');
		mainBody.scrollTop = 0;
		
	} else {
		$('#facebox .body').children().fadeIn('normal', function() { 
			if(scrolling == 1) {
				$('.faceboxcontent').scrollTo('#'+destination,1000);
				scrolling = 0;
			}
		})
	}
  }

  $.facebox.close = function() {
    $(document).trigger('close.facebox')
	printElements("reset");
    return false;
  }

  $(document).bind('close.facebox', function() {
    $(document).unbind('keydown.facebox')
    $('#facebox').animate({ height: 'hide', opacity: 'hide' },500)
  })

  $.fn.facebox = function(settings) {
    $.facebox.init(settings)
	

    var image_types = $.facebox.settings.image_types.join('|')
    image_types = new RegExp('\/.' + image_types + '$', 'i')

    function click_handler() {
      $.facebox.loading(true)
	
      // support for rel="facebox[.inline_popup]" syntax, to add a class
      var klass = $(this).attr('rel').match(/facebox\[\.(\w+)\]/);
      if (klass) klass = klass[1];
	  var relFunctions = $(this).attr('rel').match(/facebox\[\:((\w+\((\'?[\w\+]*\'?\,?\s?)+\);)+)]/);
	  if(relFunctions) relFunctions = relFunctions[1];
	  
	  
	  $('#facebox .close').click($.facebox.close)
	  $('#facebox .print').unbind('click.facebox').bind('click.facebox', function(){window.print()});
	  $('#facebox .close_image').attr('src', $.facebox.settings.close_image)
	  $('#facebox .print_image').attr('src', $.facebox.settings.print_image)
     
	 // div
	if(this.tagName == 'INPUT'){
		var form_input_types = $.facebox.settings.form_input_types.join('|');
		var form = this.form;
		var formUrl = $(form).attr('action');
		var ajaxUrl = null;
		$('input', $(form)).each(function(){
			if(form_input_types.match($(this).attr('type'))){
				if(!formUrl.match(new RegExp('\\?'+$(this).attr('name') + '|\\&' + $(this).attr('name'), 'i'))){
				formUrl += '&' + $(this).attr('name') + '=' + $(this).val();
				};
			};
		});
	   $.ajax({
		type: "GET",
		url: formUrl + '&noheader=true',
		error: function(xmlHttp, status, error){$.facebox.close();},
		success: function(data) {$.facebox.reveal(data.replace(/<link.+global.+css.+ \/>/,"").replace(/<script.+src\=.+<\/script>/gm,""), klass);runRelFunctions(relFunctions); $('#facebox a[rel^=facebox], #facebox input[rel^=facebox]').facebox(); }
		});
			
	}else if (this.href.match(/#/)) {
        var targetUrl = this.href.split('#')[0]
		destination = this.href.split('#')[1]
		if(this.href.indexOf('?') != -1) {
		var data = this.href.split('?')[1].split('#')[0];
		} else {
		var data = '';	
		}
		var plainUrl = this.href.split('?')[0]
		var currUrl = window.location.href.split('#')[0]
		if(targetUrl == currUrl) {
			scrollto = false;
			var target = this.href.replace(currUrl,'')
			$.facebox.reveal($(target).clone().show(), klass)
		} else {
			scrollto = true;
			$.ajax({
				type: "GET",
				url: plainUrl,
				data: data+'&noheader=true',
				success: function(data) {$.facebox.reveal(data.replace(/<link.+global.+css.+ \/>/,"").replace(/<script.+src\=.+<\/script>/gm,""), klass); runRelFunctions(relFunctions); $('#facebox a[rel^=facebox], #facebox input[rel^=facebox]').facebox();}
			});
			/*$.get(this.href, function(data) {$.facebox.reveal(data.replace(/<link.+global.css.+ \/>/,""), klass)});*/
		}
      // image
      } else if (this.href.match(image_types)) {
		scrollto = false;
        var image = new Image()
        image.onload = function() {
          $.facebox.reveal('<div class="image"><img src="' + image.src + '" /></div>', klass)
        }
        image.src = this.href

      // ajax
	  }else{
	  scrollto = false;
	          $.ajax({
		     type: "POST",
             url: this.href,
             data: 'noheader=true',
			error: function(xmlHttp, status, error){$.facebox.close();},
			success: function(data) {$.facebox.reveal(data.replace(/<link.+global.+css.+ \/>/,"").replace(/<script.+src\=.+<\/script>/gm,""), klass);runRelFunctions(relFunctions); $('#facebox a[rel^=facebox], #facebox input[rel^=facebox]').facebox(); }
      		})
	//  $.get(this.href, function(data) {$.facebox.reveal(data.replace(/<link.+global.css.+ \/>/,"").replace(/<scri.+ipt>/gm,""), klass) })
      }

      return false
    }

    this.click(click_handler)
    return this
	
	function runRelFunctions(functions){
		if(functions){
			var functionsArray = functions.substring(0,functions.length - 1).split(';');
			var i=0;
			
			for(i=0; i<functionsArray.length;i++){
				eval(functionsArray[i]);
			};
		};
	}
	
  }

  $.facebox.init = function(settings) {
    if ($.facebox.settings.inited) {
      return true
    } else {
      $.facebox.settings.inited = true
    }

    if (settings) $.extend($.facebox.settings, settings)
    $('body').append($.facebox.settings.facebox_html)
  }

  // getPageScroll() by quirksmode.com
  $.facebox.getPageScroll = function() {
    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
  $.facebox.getPageHeight = function() {
    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
  }
})(jQuery);

function printElements(elements) {
	if(elements == "reset") {
		$('.notprinting').removeClass('noprint notprinting');
	} else {
		var elementArray = elements.split(',');
		var elementArrayLength = elementArray.length;
		for(var i=0;i<elementArrayLength;i++) {
			$(elementArray[i]).addClass('noprint notprinting');
		}
	}
}
