var DEBUG = false;
var state = 0;
$(document).ready(
  function()
  {
    $('body').append('<div id="gevErrorWindow">'+
                     '<div id="gevErrorWindowTop">'+
                     '<div id="gevErrorWindowTopContent">&nbsp;</div>'+
                     '<img src="window_demo/images/window_close.jpg" alt="Schlie&szlig;en" id="gevErrorWindowClose" />'+
                     '</div>'+
                     '<div id="gevErrorWindowBottom"><div id="gevErrorWindowBottomContent">&nbsp;</div></div>'+
                     '<div id="gevErrorWindowContent">&nbsp;</div>'+
                     '</div>');
    //$('#gevErrorWindowOpen').bind(
    $("*[@id =~ '^gevErrorWindowOpen[0-9]*$']").bind(
      'click',
      function() {
        url = this.href;
        var checkURL = url.toLowerCase();
        if( /\.(xhtml|php|html)/.test(checkURL) ) {
            if($('#gevErrorWindow').css('display') == 'none') {
                $(this).TransferTo({
                    to:'gevErrorWindow',
                    className:'transferer2',
                    duration: 400,
                    complete: function(){
                        gevErrorWindowUpdate(true);
                        $('#gevErrorWindow').show();
                    }
                });
            }
        }
        this.blur();
        return false;
      }
    );
    $('#gevErrorWindowClose').bind(
      'click',
      gevErrorWindowClose
    );
    $('#gevErrorWindowMin').bind(
      'click',
      function()
      {
        $('#gevErrorWindowContent').SlideToggleUp(300);
        $('#gevErrorWindowBottom, #gevErrorWindowBottomContent').animate({height: 10}, 300);
        $('#gevErrorWindow').animate({height:40},300).get(0).isMinimized = true;
        $(this).hide();
        $('#gevErrorWindowResize').hide();
        $('#gevErrorWindowMax').show();
      }
    );
    $('#gevErrorWindowMax').bind(
      'click',
      function()
      {
        var gevErrorWindow = $.iUtil.getSize(document.getElementById('gevErrorWindowContent'));
        $('#gevErrorWindowContent').SlideToggleUp(300);
        $('#gevErrorWindowBottom, #gevErrorWindowBottomContent').animate({height: gevErrorWindow.hb + 13}, 300);
        $('#gevErrorWindow').animate({height:gevErrorWindow.hb+43}, 300).get(0).isMinimized = false;
        $(this).hide();
        $('#gevErrorWindowMin, #gevErrorWindowResize').show();
      }
    );
    $('#gevErrorWindow').Resizable(
      {
        minWidth: 325,
        minHeight: 348,
        maxWidth: 700,
        maxHeight: 400,
        dragHandle: '#gevErrorWindowTop',
        handlers: {
          se: '#gevErrorWindowResize'
        },
        onResize : function(size, position) {
          $('#gevErrorWindowBottom, #gevErrorWindowBottomContent').css('height', size.height-33 + 'px');
          var gevErrorWindowContentEl = $('#gevErrorWindowContent').css('width', size.width - 25 + 'px');
          if (!document.getElementById('gevErrorWindow').isMinimized) {
            gevErrorWindowContentEl.css('height', size.height - 48 + 'px');
          }
        }
      }
    );
  }
);
function initGEVAjaxForm() {
        $('#gevErrorPopup').ajaxForm({
            target: '#gevErrorWindowContent',
            success: function(data) {
                gevErrorWindowClose();
                return true;
            }
        });
    return true;
}

function gevErrorWindowUpdate(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){
                $("#gevErrorWindowContent").html(html);
                //checkState();
                initGEVAjaxForm();
                //initAjaxSlider();
            }
        });
        if (force != true) setTimeout('gevErrorWindowUpdate()', 1000);
    }
}

function gevErrorWindowClose() {
    $('#gevErrorWindow').TransferTo(
        {
            to:'errorTrigger',
            className:'transferer2',
            duration: 400
        }
    ).hide();
}