/*
 * Thickbox
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2006 cody lindley
 * 
 * Tim Portner @ netzgestaltung.eu
*/


$(document).ready(function() {
	tb_init('a.thickbox, input.thickbox');
});


function tb_init(domChunk){
	$(domChunk).click(function(){
	  var url = this.href;
	  TB_show(url);
	  this.blur();
	  return false;
	});
}


function TB_show(url) {
  
  try {

    if (document.getElementById("TBhideSelect") == null) {
	    $("body").append("<iframe id='TBhideSelect'></iframe>\n<div id='TBoverlay'>\n</div>\n<div id='TBwindow'>\n</div>");
	    $("#TBoverlay").click(TB_remove);
    }

    $(window).scroll(TB_position);
    TBoverlaySize();
    $("body").append("\n<div id='TBload'></div>");
    TBload_position();
    TB_resize();
    $("#TBwindow").fadeIn("slow").append("\n<a href='#' id='TBcloseWindowButton'>schlie&szlig;en</a><div id='TBajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'>\n</div>");
    $("#TBcloseWindowButton").click(TB_remove);
	TB_position();

	$.ajax({
		type: "GET",
		url: url,
		dataType: "html",
		cache: false,
		success: function(html){
		   $("#TBajaxContent").html(html);
		   $("#TBload").fadeOut();
			    var options = { 
			        target:        '#update',   // target element(s) to be updated with server response 
			        beforeSubmit:  showRequest,  // pre-submit callback 
			        success:       showResponse  // post-submit callback 
			    }; 
 
			    // bind to the form's submit event 
			    $('#order').submit(function() {								
			        $(this).ajaxSubmit(options);
			        return false; 
			    });
		}
	}); 

  } catch(e) {

  }
}

// pre-submit callback 
function showRequest(formData, jqForm, options) { 
	$(".button").css({ color: "#DBDBDB" })
	$("#TBformload").fadeIn("fast");
} 
 
// post-submit callback 
function showResponse(responseText, statusText)  {
	$("#TBformload").fadeOut("fast", function(){
		$(".button").css({ color: "#000" });
	});
} 

function TB_resize(){
  TB_WIDTH = 439;
  TB_HEIGHT = 440;
  ajaxContentW = TB_WIDTH - 30;
  ajaxContentH = TB_HEIGHT - 45;

  var pagesize = TB_getPageSize();
  var x = pagesize[0] - 100;
  var y = pagesize[1] - 100;
  if (ajaxContentW > x) {
    ajaxContentH = ajaxContentH * (x / ajaxContentW);
    ajaxContentW = x; 
    if (ajaxContentH > y) { 
      ajaxContentW = ajaxContentW * (y / ajaxContentH);
      ajaxContentH = y; 
    }
  } else if (ajaxContentH > y) { 
    ajaxContentW = ajaxContentW * (y / ajaxContentH);
    ajaxContentH = y; 
    if (ajaxContentW > x) { 
      ajaxContentH = ajaxContentH * (x / ajaxContentW);
      ajaxContentW = x;
    }
  }
  TB_WIDTH = (ajaxContentW*1) + 30;
  TB_HEIGHT = (ajaxContentH*1) + 40;
}

function TB_remove() {
  $("#TBwindow").fadeOut("slow");
  $("#TBoverlay").fadeOut("fast",function(){
  $("#TBload,#TBhideSelect,#TBoverlay,#TBwindow,#TBajaxContent").remove();
});
  return false;
}

function TB_position() {
  var pagesize = TB_getPageSize();
  var arrayPageScroll = TB_getPageScrollTop();
  $("#TBwindow").css({width:TB_WIDTH+"px",left: (arrayPageScroll[0] + (pagesize[0] - TB_WIDTH)/2)+"px", top: (arrayPageScroll[1] + (pagesize[1]-TB_HEIGHT)/2)+"px" });
}

function TBoverlaySize(){
  if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {  
    yScroll = window.innerHeight + window.scrollMaxY;
    xScroll = window.innerWidth + window.scrollMaxX;
    var deff = document.documentElement;
    var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
    var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
    xScroll -= (window.innerWidth - wff);
    yScroll -= (window.innerHeight - hff);
  } else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
    yScroll = document.body.scrollHeight;
    xScroll = document.body.scrollWidth;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    yScroll = document.body.offsetHeight;
    xScroll = document.body.offsetWidth;
    }
  $("#TBoverlay").css({"height":yScroll +"px"});
  $("#TBhideSelect").css({"height":yScroll +"px","width":xScroll +"px"});
}

function TBload_position() {
  var pagesize = TB_getPageSize();
  var arrayPageScroll = TB_getPageScrollTop();
  $("#TBload")
  .css({left: (arrayPageScroll[0] + (pagesize[0] - 208)/2)+"px", top: (arrayPageScroll[1] + ((pagesize[1]-13)/2))+"px" })
  .css({display:"block"});
}

function TB_getPageScrollTop(){
  var yScrolltop;
  var xScrollleft;
  if (self.pageYOffset || self.pageXOffset) {
    yScrolltop = self.pageYOffset;
    xScrollleft = self.pageXOffset;
  } else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){   // Explorer 6 Strict
    yScrolltop = document.documentElement.scrollTop;
    xScrollleft = document.documentElement.scrollLeft;
  } else if (document.body) {
    yScrolltop = document.body.scrollTop;
    xScrollleft = document.body.scrollLeft;
  }
  arrayPageScroll = new Array(xScrollleft,yScrolltop) 
  return arrayPageScroll;
}

function TB_getPageSize(){
  var de = document.documentElement;
  var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
  arrayPageSize = new Array(w,h) 
  return arrayPageSize;
}

document.onkeydown = function(e){
  if (e == null) {
    keycode = event.keyCode;
  } else {
    keycode = e.which;
  }
  if(keycode == 27){
    TB_remove();
  }
}