/*
 * 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 ]
 *
 * Usage:
 *  
 *  jQuery(document).ready(function() {
 *    jQuery('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() { ajaxes })
 *
 *  This will show a loading screen before the passed function is called,
 *  allowing for a better ajax experience.
 *
 */
(function($) {
  $.facebox = function(data, klass, center) {
    $.facebox.init()
    $.facebox.loading()
    $.isFunction(data) ? data.call($) : $.facebox.reveal(data, klass, center)
  }
  
  //START: Modification by Søren Christensen
  var htmlCopy = '';
  var targetDiv = '';
  //END: Modification by Søren Christensen

  $.facebox.settings = {  	
    image_types   : [ 'png', 'jpg', 'jpeg', 'gif' ],
    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="header"><h1> \
              	<table> <tr> <td class="info" style="width:99%;"></td><td> \
              	<a href="#" class="close" style="width: 20px;"> \
                </a> \
                </td></tr></table></h1> \
              </div> \
              <div class="content"> \
              </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 .content').empty()
    $('#facebox .body').children().hide().end().
      append('<div class="loading"><img src="'+$.facebox.settings.loading_image+'"/><br /><br /><h2>' + pleaseWaitMsg + '</h2></div>')

    var pageScroll = $.facebox.getPageScroll();
       
    $('#facebox').css({
      top:	pageScroll[1] + ($.facebox.getPageHeight() / 4),
      left:	pageScroll[0]
    }).show()

    $(document).bind('keydown.facebox', function(e) {
      if (e.keyCode == 27) $.facebox.close()
    })
  }

  $.facebox.reveal = function(data, klass, center) {
    //if (klass) $('#facebox .content').addClass(klass)
    if( 'true' == center ) {
    	$('#facebox .content').append('<center>'+data+'</center>');    
    } else {
	    $('#facebox .content').append(data);    
    }
	$('#facebox .close').empty();    
    $('#facebox .close').append('<img src="'+$.facebox.settings.close_image+'" title="close" class="close_image" />')
	$('#facebox .info').empty();
	
	if( document.getElementById( klass ) != null ) {
	    $('#facebox .info').append( document.getElementById( klass ).innerHTML );	
	} else if( klass != null && klass.length > 0 ) {
	    $('#facebox .info').append( klass );	
	}
    $('#facebox .loading').remove()
    $('#facebox .body').children().fadeIn('normal')
    
    //Move facebox dialog to fit window.
    var height = $.facebox.getPageHeight();
    height = height - Element.getHeight(document.getElementById('facebox'));
    
    var pageScroll = $.facebox.getPageScroll();
    
    if( height > 0 ) {
    	height = height / 2;
    } else {
    	height = 0;
    }
       
    $('#facebox').css({
      top:	pageScroll[1] + height,
      left:	pageScroll[0]
    });
  }

  $.facebox.close = function() {
  	//START: Modification by Søren Christensen
  	if( targetDiv != null &&  document.getElementById( targetDiv )) {
	  	document.getElementById( targetDiv ).innerHTML = htmlCopy;
	  	targetDiv = '';
	  	htmlCopy = '';  	
  	}
  	//END: Modification by Søren Christensen
    $(document).trigger('close.facebox')
    //START: Modification by Søren Christensen
  	if (eval("typeof postFacebox == 'function'")) {
  		postFacebox();
	}    
  	//END: Modification by Søren Christensen	
    return false
  }

  $(document).bind('close.facebox', function() {
    $(document).unbind('keydown.facebox')
    $('#facebox').fadeOut(function() {
      $('#facebox .content').removeClass().addClass('content')
    })
  })
  
  $(document).bind('loading.facebox', function() {
  	$.facebox.loading();
  })  
  
  

  $.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() {
		//START: Modification by Søren Christensen  
		var show = true;
		if (eval("typeof preFacebox == 'function'")) {
			show = preFacebox();
		}
			
		if( !show ) {
			return;
		}
		//END: Modification by Søren Christensen     
      $.facebox.loading(true)

      // support for rel="facebox[.inline_popup]" syntax, to add a class
      var klass = this.rel.match(/facebox\[(\w+)\]/)
      if (klass) klass = klass[1]
      
      // div
      if (this.href.match(/#/)) {
        var url    = window.location.href.split('#')[0]
        var target = this.href.replace(url,'')  
        //START: Modification by Søren Christensen
        targetDiv = target.substring(1);
        htmlCopy = document.getElementById(targetDiv).innerHTML;        
        //END: Modification by Søren Christensen       
        $.facebox.reveal($(target).clone().show(), klass)
        //START: Modification by Søren Christensen
        document.getElementById(targetDiv).innerHTML = '';
        //END: Modification by Søren Christensen

      // image
      } else if (this.href.match(image_types)) {
        var image = new Image()
        image.onload = function() {
          $.facebox.reveal('<div class="image"><img src="' + image.src + '" /></div>', klass)
        }
        image.src = this.href

      // ajax
      } else {      
        $.get(this.href, function(data) { $.facebox.reveal(data, klass) })
      }

      return false
    }

    this.click(click_handler)
    return this
  }

  $.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)

    var preload = [ new Image(), new Image() ]
    preload[0].src = $.facebox.settings.close_image
    preload[1].src = $.facebox.settings.loading_image

    $('#facebox').find('.b:first, .bl, .br, .tl, .tr').each(function() {
      preload.push(new Image())
      /*
	  $('#facebox').getStyle('background-image').replace(/url\((.+)\)/, '$1')
	  */
      preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1')
    })

    $('#facebox .close').click($.facebox.close)
    $('#facebox .close_image').attr('src', $.facebox.settings.close_image)
  }

  // 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
  }
  
  $(document).bind('showAddAdress.facebox', function() {
	$('#facebox .content').empty();
	$('#facebox .info').empty();			
	$('#facebox .info').append( document.getElementById('checkout_addaddress_div_headline').innerHTML );			
	$('#facebox .content').append( document.getElementById('checkout_addaddress_div').innerHTML );
  })  
  
  $(document).bind('showCreateFavoriteList.facebox', function() {
	$('#facebox .content').empty();
	$('#facebox .info').empty();			
	$('#facebox .info').append( document.getElementById('createFavoriteList_headline').innerHTML );			
	$('#facebox .content').append( document.getElementById('createFavoriteList').innerHTML );
  })    
})(jQuery);
