/**
 * this js requires jquery and cbClientArea to be loaded before.
 *
 * base.js contains some general functions for cb-applications and is   
 * automatically included with class.cb_ui_page_layout_cb_app.php
 *
 *
 */

/**
 * showRoundedRedWindow(url)
 *
 * This method shows a white window with a red rounded border. Containing the
 * HTML fragment which is provided by URL. For Usage see Language Window
 * below.
 */

function showRoundedRedWindow(url) {
   var langLayer  = jQuery('<div class="layerForAppSwitcher" style="width:100%;height:100%;position:fixed;top:0px;left:0px;z-index:1337;"></div>');
   var langWindow = jQuery('<div class="layerForAppSwitcher" style="border:2px solid #b40000;border-radius:10px;-webkit-border-radius:10px;-moz-border-radius:10px;padding:35px 20px 35px 20px;width:360px;height:310px;position:fixed;z-index:1337;background-color:#ffffff;"></div>');

   jQuery('body').append(jQuery(langLayer));

    if(!jQuery.browser.msie) {
        // correct top positioning failed with ie
        if (window.innerWidth)
            jQuery(langWindow).css('left',window.innerWidth/2-202+'px').css('top',window.innerHeight/2-191+'px');
        else
            jQuery(langWindow).css('left',window.document.body.offsetWidth/2-202+'px').css('top',window.document.body.offsetHeight/2-191+'px');
    }
   jQuery(langWindow)
   .load(url, function() {
      jQuery('body').append(jQuery(langWindow));
   });

   return false;
}

/**
 * showLanguageWindow - draw the languageselection window
 *
 * This function automaticaly disables the page on click and shows the
 * language window. The language window will be given an project param
 * which is needed for showing the supported languages and no others.
 */
function showLanguageWindow(project) {
   showRoundedRedWindow('/module/lib/framework/getLanguageWindow.php?project='+project);
   return false;
}

function showAppSelectWindow(project) {
   showRoundedRedWindow('/module/lib/framework/getAppSelectWindow.php?project='+project);
   return false;
}

function removeAppSelectWindow() {
   $('.layerForAppSwitcher').remove();
}

function toggleLanguage() {
   // @todo implement. should be in a seperate, optional file.
}

function toggleWindowSize() {
   // @todo implement
}

function submitLoginForm() {
   jQuery("#loginForm").submit();
}
/* this is used from cb-in/event/cbstage_status.php */
function cbGetHeight(elem) 
{
  var isIE6 = navigator.userAgent.toLowerCase().indexOf("msie 6.") != -1;
  $elem = jQuery(elem);
  if (!$elem.length) return 0;
  var result = $elem.height(); 
  if (!isIE6)
  {
     result += parseInt($elem.css('margin-top'));
     result += parseInt($elem.css('margin-bottom'));
     result += parseInt($elem.css('padding-bottom'));
     result += parseInt($elem.css('padding-top'));
  }
  return result;
}

function maximizeContentHeight()
{
   var isIE6 = navigator.userAgent.toLowerCase().indexOf("msie 6.") != -1;
                    
   var h_head = cbGetHeight('#top') + cbGetHeight('#tabAnchors') + 
                parseInt(jQuery('#container').css('margin-top'));
   var h_foot = cbGetHeight('#footer');
   var screenHeight = jQuery(window).height() || 600;

   var offset = 0;
   if (!isIE6)
      offset = parseInt(jQuery('#content').css('padding-top')) + 
               parseInt(jQuery('#content').css('padding-bottom')) ;

   jQuery('#content').height(screenHeight - h_head - h_foot - offset);
}

jQuery(document).ready(function(){
   // initialize size
//   cbMaximizeHeight('content');
    if(!jQuery.browser.msie)
        maximizeContentHeight(); // makes trouble in IE

   // install hook for window resizing
    if(!jQuery.browser.msie)
        jQuery(window).resize(maximizeContentHeight);

   // hide all elements with jsHidden class
   jQuery('.jsHidden').hide();

   jQuery('#loginForm').keydown(function(e) {
      if(e.keyCode == 13) {
        submitLoginForm();
      }
   });
});

