var DEBUG = false;
$.log( 'GEV InfoWin::debug start at:' + new Date().toLocaleString() );
$.log( 'GEV InfoWin::jquery initialisation start!' );
var state = 0;
$(document).ready(
  function()
  {
    $('body').append('<div id="gevInfoWindow">'+
                     '<div id="gevInfoWindowTop">'+
                     '<div id="gevInfoWindowTopContent">&nbsp;</div>'+
                     '<img src="window_demo/images/window_close.jpg" alt="Schlie&szlig;en" id="gevInfoWindowClose" />'+
                     '</div>'+
                     '<div id="gevInfoWindowBottom"><div id="gevInfoWindowBottomContent">&nbsp;</div></div>'+
                     '<div id="gevInfoWindowContent">&nbsp;</div>'+
                     '</div>');
    //$('#gevInfoWindowOpen').bind(
    $("*[@id =~ '^gevInfoWindowOpen[0-9]*$']").bind(
      'click',
      function() {
        url = this.href;
        $.log( 'GEV InfoWin::gevInfoWindow found url:'+url )
        opener_id = this.id;
        var checkURL = url.toLowerCase();
        if( /\.(xhtml|php|html)/.test(checkURL) ) {
            if($('#gevInfoWindow').css('display') == 'none') {
            	$.log( 'GEV InfoWin::gevInfoWindow initialized!' );

            	var myTop = tempY - 200;
            	var myLeft = tempX + 200;

            	$('#gevInfoWindow').css('top', myTop + 'px');
            	$('#gevInfoWindow').css('left', myLeft + 'px');

                $(this).TransferTo({
                    to:'gevInfoWindow',
                    className:'transferer2',
                    duration: 400,
                    complete: function(){
                    	$.log( 'GEV InfoWin::gevInfoWindow show!' );
                        gevInfoWindowUpdate(true);
                        $('#gevInfoWindow').show();

                    }
                });
            }
            else $.log( 'GEV InfoWin::gevInfoWindow allways initialized!' );
        }
        else $.log( 'GEV InfoWin::gevInfoWindow url dropped!' );
        this.blur();
        return false;
      }
    );
    $('#gevInfoWindowClose').bind(
      'click',
      gevInfoWindowClose
    );
    $('#gevInfoWindowMin').bind(
      'click',
      function()
      {
        $('#gevInfoWindowContent').SlideToggleUp(300);
        $('#gevInfoWindowBottom, #gevInfoWindowBottomContent').animate({height: 10}, 300);
        $('#gevInfoWindow').animate({height:40},300).get(0).isMinimized = true;
        $(this).hide();
        $('#gevInfoWindowResize').hide();
        $('#gevInfoWindowMax').show();
      }
    );
    $('#gevInfoWindowMax').bind(
      'click',
      function()
      {
        var gevInfoWindow = $.iUtil.getSize(document.getElementById('gevInfoWindowContent'));
        $('#gevInfoWindowContent').SlideToggleUp(300);
        $('#gevInfoWindowBottom, #gevInfoWindowBottomContent').animate({height: gevInfoWindow.hb + 13}, 300);
        $('#gevInfoWindow').animate({height:gevInfoWindow.hb+43}, 300).get(0).isMinimized = false;
        $(this).hide();
        $('#gevInfoWindowMin, #gevInfoWindowResize').show();
      }
    );
    $('#gevInfoWindow').Resizable(
      {
        minWidth: 325,
        minHeight: 348,
        maxWidth: 700,
        maxHeight: 400,
        dragHandle: '#gevInfoWindowTop',
        handlers: {
          se: '#gevInfoWindowResize'
        },
        onResize : function(size, position) {
          $('#gevInfoWindowBottom, #gevInfoWindowBottomContent').css('height', size.height-33 + 'px');
          var gevInfoWindowContentEl = $('#gevInfoWindowContent').css('width', size.width - 25 + 'px');
          if (!document.getElementById('gevInfoWindow').isMinimized) {
            gevInfoWindowContentEl.css('height', size.height - 48 + 'px');
          }
        }
      }
    );
  }
);
function initGEVAjaxForm() {
	$.log( 'GEV InfoWin::ajaxForm initialized!' );
    $('#gevInfoPopup').ajaxForm({
        target: '#gevInfoWindowContent',
        success: function(data) {
            gevInfoWindowClose();
            return true;
        }
    });
    return true;
}

function gevInfoWindowUpdate(force) {
	$.log( 'GEV InfoWin::gevInfoWindow update force:'+force );
    if ((force == true || state == 0 || state == 2 || state == 3) && /\.(xhtml|php|html)/.test(url) ) {
        num = Math.random();
        var sep = '?';
        if( /\?/.test(url) ) sep = '&';
        $.ajax({
            type: "GET",
            url: url+sep+num,
            dataType: "html",
            success: function(html){
            	$.log( 'GEV InfoWin::gevInfoWindow show!' );
                $("#gevInfoWindowContent").html(html);
                //checkState();
                initGEVAjaxForm();
                //initAjaxSlider();
            }
        });
        if (force != true) setTimeout('gevInfoWindowUpdate()', 1000);
    }
    else $.log( 'GEV InfoWin::gevInfoWindow update skiped!' );
}

function gevInfoWindowClose() {
   	$.log( 'GEV InfoWin::gevInfoWindow close!' );
    $('#gevInfoWindow').TransferTo(
        {
            to:opener_id,
            className:'transferer2',
            duration: 400
        }
    ).hide();
}

// ständiges mitschreiben der Mouseposition
var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft - 185
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX - 185
    tempY = e.pageY
  }
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}
  return true
}

function positionDiv(){
	alert(tempY + " - " + tempX)
	document.getElementById("gevInfoWindow").style.top = (tempY - 200) + "px";
	document.getElementById("gevInfoWindow").style.left = (tempX + 200) + "px";
}
$.log( 'GEV InfoWin::jquery initialization ready!' );