/*!
 * jsBox, based on:
 * 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*=jsBox]').jsBox()
 *  })
 *
 *  <a href="#terms" rel="jsBox">Terms</a>
 *    Loads the #terms div in the box
 *
 *  <a href="terms.html" rel="jsBox">Terms</a>
 *    Loads the terms.html page in the box
 *
 *  <a href="terms.png" rel="jsBox">Terms</a>
 *    Loads the terms.png image in the box
 *
 *
 *  You can also use it programmatically:
 *
 *    jQuery.jsBox('some html')
 *
 *  This will open a jsBox with "some html" as the content.
 *
 *    jQuery.jsBox(function() { ajaxes })
 *
 *  This will show a loading screen before the passed function is called,
 *  allowing for a better ajax experience.
 *
 */

(function(jQuery) {

  //var staticurl = "http:/" + "/static" + document.location.hostname.replace(/^[^\.]*((\.[^\.]+){2,})/i, "$1") + "/";

  staticSuffix = "";
  if (document.location.protocol == "https:") {
      // auf https ist der Domain-Name von static anders
      staticSuffix = "-secure";
  }
  var staticurl = document.location.protocol + "/" + "/static" + staticSuffix + document.location.hostname.replace(/^[^\.]*((\.[^\.]+){2,})/i, "$1") + "/";
  
  
  jQuery('head').append('<link rel="stylesheet" type="text/css" href="' + staticurl + 'css/jquery.jsbox.css" media="screen" />');

  jQuery.jsBox = function(data, klass, type) {
    if (jQuery("#jsBox").css("display") == "block") {
    	jQuery("#jsBox .content > *").css("visibility", "hidden");
    }
    else {
	    jQuery.jsBox.init(null);
	    jQuery.jsBox.loading();
	    }
//    jQuery('#jsBox').find('a.ok, .ok_image').hide();
//    jQuery('#jsBox').find('a.cancel, .cancel_image').hide();
//    jQuery('#jsBox').find('a.close, .close_image').show();
    jQuery('#jsbox_btn_ok').hide();
    jQuery('#jsbox_btn_cancel').hide();
    jQuery('#jsBox').find('a.close, .close_image').show();
    if (type == "ajax") {
    	jQuery.get(data, {}, function(data) { jQuery.jsBox.reveal(data, klass, 'jsBox', type); try{activateEventHandlersForAjaxableItems();}catch(e){}; }, 'html');
    }
    else {
	    jQuery.isFunction(data) ? data.call(jQuery) : jQuery.jsBox.reveal(data, klass, 'jsBox', type);
    }
  }

  jQuery.modalBox = function(data, klass, callback, type) {
    jQuery.jsBox.init();
    jQuery.jsBox.loading();
//    jQuery('#jsBox').find('a.ok, img.ok_image').show();
//    jQuery('#jsBox').find('a.cancel, img.cancel_image').show();
    jQuery('#jsbox_btn_ok').show();
    jQuery('#jsbox_btn_cancel').show();
    jQuery('#jsBox').find('a.close, img.close_image').show();
    if (type == "ajax") {
    	jQuery.get(data, {}, function(data) { jQuery.jsBox.reveal(data, klass, 'modal', type);try{activateEventHandlersForAjaxableItems();}catch(e){};  }, 'html');
    }
    else {
	    jQuery.isFunction(data) ? data.call(jQuery) : jQuery.jsBox.reveal(data, klass, 'modal', type);
    }

    jQuery(document).bind('ok.jsBox', function() {
    	jQuery(document).unbind('ok.jsBox');
    	if (callback) {
	    	jQuery.isFunction(callback) ? callback() : window.location.href = callback;
	    }
    });
  }


  jQuery.alertBox = function(data, klass, callback, type) {
    jQuery.jsBox.init();
    jQuery.jsBox.loading();
//    jQuery('#jsBox').find('a.ok, img.ok_image').show();
//    jQuery('#jsBox').find('a.cancel, img.cancel_image').hide();
    jQuery('#jsbox_btn_ok').show();
    jQuery('#jsbox_btn_cancel').hide();
    jQuery('#jsBox').find('a.close, img.close_image').show();
    if (type == "ajax") {
    	jQuery.get(data, {}, function(data) { jQuery.jsBox.reveal(data, klass, 'modal', type);try{activateEventHandlersForAjaxableItems();}catch(e){};  }, 'html');
    }
    else {
	    jQuery.isFunction(data) ? data.call(jQuery) : jQuery.jsBox.reveal(data, klass, 'modal', type);
    }


    jQuery(document).bind('ok.jsBox', function() {
    	jQuery(document).unbind('ok.jsBox');
    	if (callback) {
	    	jQuery.isFunction(callback) ? callback() : window.location.href = callback;
	    }
    });
  }

  jQuery.jsBox.settings = {
    loading_image 	: staticurl + 'images/ajax_loading.gif',
    close_image   	: staticurl + 'images/buttons/close.gif',
    ok_image      	: staticurl + 'images/jsbox/ok.gif',
    cancel_image  	: staticurl + 'images/jsbox/cancel.gif',
    btn_ok			: 'OK',
    btn_cancel		: 'Abbrechen',
    image_types   	: [ 'png', 'jpg', 'jpeg', 'gif' ],
    //jsBox_html  	: '<div id="jsBox_overlay" style="display:none;"><div id="jsBox" 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 id="jsb_header" style="width:100%; background-repeat: repeat-x; background-position: 0pt bottom; background-image: url(http://static.dhd24.com//images/boxes/blue_run_bg_50.jpg);"><span class="b_grey1"><span class="b_grey2"><span class="b_grey3"><div style="height: 20px;" class="contentb_grey"><div style="padding: 1px 0px; position: relative;"><div style="float:left;margin-left:5px;" class="norm_b"><##TITLE##></div><div style="float:right;margin-right:5px;"><a href="#" class="close"><img src="' + staticurl + 'images/buttons/close.gif" title="close" class="close_image"></a></div></div></div><span class="b_grey3"><span class="b_grey2"><span class="b_grey1"></div><div class="footer"><div id="jsbox_btn_cancel" style="float:left;margin-right:10px;"><a href="#"><##BTN_CANCEL##></a></div><div id="jsbox_btn_ok" style="float:left;"><div class="bt_blue_l"></div><div class="bt_blue_c big_b white"><##BTN_OK##></div><div class="bt_blue_r"></div></div></div></td><td class="b"></tr><tr><td class="bl"><td class="b"><td class="br"></tr></tbody></table></div></div>',
    //jsBox_html  	: '<div id="jsBox_overlay"style="display:none;"/><div id="jsBox"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 id="jsb_header"style="width:100%; background-repeat: repeat-x; background-position: 0pt bottom; background-image: url(http://static.dhd24.com//images/boxes/blue_run_bg_50.jpg);"><span class="b_grey1"/><span class="b_grey2"/><span class="b_grey3"/><div style="height: 20px;"class="contentb_grey"><div style="padding: 1px 0px; position: relative;"><div style="float:left;margin-left:5px;" class="norm_b"><##TITLE##></div><div style="float:right;margin-right:5px;"><a href="#"class="close"><img src="' + staticurl + 'images/buttons/close.gif"title="close"class="close_image"/></a></div></div></div><span class="b_grey3"/><span class="b_grey2"/><span class="b_grey1"/></div><div class="content"></div><div class="footer"><div id="jsbox_btn_ok"style="cursor:pointer; float:left;"><div class="bt_blue_l"/><div class="bt_blue_c big_b white"><##BTN_OK##></div><div class="bt_blue_r"/></div><div id="jsbox_btn_cancel"style="cursor:pointer;float:left;"><div class="bt_blue_l"/><div class="bt_blue_c big_b white"><##BTN_CANCEL##></div><div class="bt_blue_r"/></div></div></td><td class="b"/></tr><tr><td class="bl"/><td class="b"/><td class="br"/></tr></tbody></table></div></div>',
    jsBox_html  	: '<div id="jsBox_overlay" style="display:none;"/>' +
						'<div id="jsBox"style="display:none;">' +
							'<div class="popup">' +
								'<table>' +
									'<tr>' +
										'<td class="body">' +
											'<div class="jsb_top">' +
												'<div id="jsb_header">' +
													'<div style="float:left;margin-left:5px;">' +
														'<##TITLE##>' +
													'</div>' +
													'<div style="float:right;margin-right:5px;margin-top: 5px;">' +
														'<a href="#"class="close">' +
															'<img src="' + staticurl + 'images/buttons/close.gif" title="close" class="close_image"/>' +
														'</a>' +
													'</div>' +
													'<div style="clear:both"></div>' +
												'</div>' +
												'<div class="content">' +
												'</div>' +
											'</div>' +
											'<div class="footer">' +
												'<div id="jsbox_btn_cancel" style="float:left;margin-right:5px;">' +
													'<a href="#">' +
														'<##BTN_CANCEL##>' +
													'</a>' +
												'</div>' +
												'<div id="jsbox_btn_ok" style="float:left;margin-right:5px;">' +
													'<a href="#">' +
														'<##BTN_OK##>' +
													'</a>' +
												'</div>' +
											'</div>' +
										'</td>' +
									'</tr>' +
								'</table>' +
							'</div>' +
						'</div>',
		title    		: "&nbsp;"
  };

  jQuery.jsBox.loading = function(nocleanup) {
    if (jQuery('#jsBox .loading').length == 1) { return true; }

	if (!nocleanup) {
	    jQuery('#jsBox .content').empty();
	    jQuery('#jsBox .body').children().hide();
	    }
    jQuery('#jsBox .body').prepend('<div class="loading"><img src="'+jQuery.jsBox.settings.loading_image+'"/></div>');

    var pageScroll = jQuery.jsBox.getPageScroll();
    jQuery('#jsBox').css({
      top:	pageScroll[1] + (jQuery.jsBox.getPageHeight() / 10),
      left:	pageScroll[0]
    }).show()

    jQuery(document).bind('keydown.jsBox', function(e) {
      if (e.keyCode == 27) jQuery.jsBox.close();
    })
  }


  jQuery.jsBox.reveal = function(data, klass, mode, datatype) {
    if (mode) jQuery('#jsBox_overlay').addClass(mode);
    jQuery('#jsBox_overlay').css("height", jQuery.jsBox.getRealPageHeight()).show();
    jQuery('embed, object, select').css('visibility', 'hidden');
//    jQuery('#jsBox_iframe').css("height", jQuery.jsBox.getRealPageHeight()).show();
    if (klass) jQuery('#jsBox .content').addClass(klass);

    if (datatype == 'dom') {
    	jQuery('#jsBox .content').empty().append(jQuery(data).clone(true).show());
    }
    else {
	    jQuery('#jsBox .content').empty().html(data);
    }
    jQuery('#jsBox .loading').remove();
    jQuery('#jsBox .body').children().fadeIn('normal', function() {
    	var height = jQuery.jsBox.getRealPageHeight();
//	    jQuery('#jsBox_iframe').css("height", height);
        jQuery('#jsBox_overlay').css("height", height + 5);
    	});
  }

  jQuery.jsBox.close = function() {
    jQuery(document).trigger('close.jsBox');
    return false;
  }

  jQuery.jsBox.ok = function() {
    jQuery(document).trigger('close.jsBox');
    jQuery(document).trigger('ok.jsBox');
    return false;
  }

  jQuery.jsBox.cancel = function() {
    jQuery(document).trigger('close.jsBox');
    return false;
  }

  jQuery(document).bind('close.jsBox', function() {
    jQuery(document).unbind('keydown.jsBox')
    jQuery('#jsBox').fadeOut(function() {
      jQuery(document).trigger('forceClose.jsBox');
    });
  })
  	.bind('forceClose.jsBox', function() {
	    jQuery.jsBox.settings.inited = false;
	    jQuery(document).unbind('keydown.jsBox')
	    jQuery('#jsBox').remove();
	    //jQuery('#jsBox .content').removeClass().addClass('content');
        jQuery('#jsBox_overlay').remove();
        jQuery('embed, object, select').css('visibility', 'visible');
	    //jQuery('#jsBox_iframe').hide();
  	});

  jQuery.fn.jsBox = function(settings) {
    jQuery.jsBox.init(settings);

    var image_types = jQuery.jsBox.settings.image_types.join('|');
    image_types = new RegExp('\.' + image_types + '$', 'i');

    function click_handler() {

      if (jQuery("#jsBox").css("display") == "block") {
			jQuery("#jsBox .content > *").css("visibility", "hidden");
         jQuery.jsBox.loading(true);
		}
      else {
         jQuery.jsBox.loading();
		 }

//      jQuery(document).trigger('forceClose.jsBox');

      // support for rel="jsBox[.inline_popup]" syntax, to add a class
      var klass = this.rel.match(/jsBox\[\.(\w+)\]/);
      if (klass) { klass = klass[1]; }

      // div
      if (this.href.match(/#/)) {
        var url    = window.location.href.split('#')[0];
        var target = this.href.replace(url,'');
        jQuery.jsBox.reveal(jQuery(target).clone().show(), klass, 'jsBox');

      // image
      } else if (this.href.match(image_types)) {
        var image = new Image();
        image.onload = function() {
          jQuery.jsBox.reveal('<div class="image"><img src="' + image.src + '" /></div>', klass, 'jsBox');
        }
        image.src = this.href;

      // ajax
      } else {
        jQuery.get(this.href, function(data) { jQuery.jsBox.reveal(data, klass, 'jsBox');try{activateEventHandlersForAjaxableItems();}catch(e){};  });
      }

      return false;
    }

    this.unbind("click").click(click_handler);
    return this;
  }

  jQuery.jsBox.init = function(settings) {
    if (jQuery.jsBox.settings.inited) {
      return true;
    } else {
      jQuery.jsBox.settings.inited = true;
    }

    if (settings) { jQuery.extend(jQuery.jsBox.settings, settings); }
    var str_jsBox_html = jQuery.jsBox.settings.jsBox_html;
    str_jsBox_html = str_jsBox_html.replace(/<##TITLE##>/, jQuery.jsBox.settings.title);
    str_jsBox_html = str_jsBox_html.replace(/<##BTN_OK##>/, jQuery.jsBox.settings.btn_ok);
    str_jsBox_html = str_jsBox_html.replace(/<##BTN_CANCEL##>/, jQuery.jsBox.settings.btn_cancel);
    jQuery('body').append(str_jsBox_html);

    var preload = [ new Image(), new Image() ];
    preload[0].src = jQuery.jsBox.settings.close_image;
    preload[1].src = jQuery.jsBox.settings.loading_image;

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

    jQuery('#jsBox .close_image').click(jQuery.jsBox.close);
    jQuery('#jsBox .close_image').attr('src', jQuery.jsBox.settings.close_image);

    //jQuery('#jsBox .ok_image').click(jQuery.jsBox.ok);
    //jQuery('#jsBox .ok_image').attr('src', jQuery.jsBox.settings.ok_image);
    jQuery('#jsbox_btn_ok').click(jQuery.jsBox.ok);

    //jQuery('#jsBox .cancel_image').click(jQuery.jsBox.cancel);
    //jQuery('#jsBox .cancel_image').attr('src', jQuery.jsBox.settings.cancel_image);
    jQuery('#jsbox_btn_cancel').click(jQuery.jsBox.cancel);
  }

  // getPageScroll() by quirksmode.com
  jQuery.jsBox.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
  jQuery.jsBox.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.jsBox.getRealPageHeight = function() {
	var yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
	}

    var windowHeight = jQuery.jsBox.getPageHeight();

	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
    return pageHeight;
  }
})(jQuery);

/**
 * jsConfirm, jsAlert und jsBox erlauben jeweils Aufrufe mit:
 *
 * - msg vom Typ String/HTML
 * - msg vom Typ String/URL, dann mit type = 'ajax' [lädt die URL in die Box]
 * - msg vom Typ String/Selector, dann mit type = 'dom' [zieht alle auf "Selector" passenden DOM-Objekte des aktuellen DOM-tree in die Box]
 *
 *
 * jsConfirm und jsAlert erlauben außerdem Aufruf mit callback:
 *
 * - callback vom Typ String/URL [Weiterleitung auf die URL bei Klick auf "ok"]
 * - callback vom Typ Function/Callback [ruft die Funktion bei Klick auf "ok" auf]
 *
 * die Notation für Function/Callback muss eine anonyme Funktion sein, die intern die gewünschten Funktionen erst aufruft.
 * Andernfalls wird deine Funktion schon zum "Anzeigezeitpunkt" aufgerufen - deshalb die Kapselung:
 *
 */

function jsConfirm(msg, callback, type) {
	jQuery.modalBox(msg, null, callback, type);
}

function jsAlert(msg, callback, type) {
	jQuery.alertBox(msg, null, callback, type);
}


function jsBox(data, type) {
	jQuery.jsBox(data, null, type);
}