/**
 * jQuery Functions
 */

jQuery.fn.exists = function(){
  return jQuery(this).length>0;
}


/**
 * jQuery Styles
 */

$(window).load(function () {
  
  /* 
   * Calculating the size of the right column (#col1)
   */
  
  // CALCULATING-START
   
  var col3 = $('#col3');
  var col3s = $('#col3 .screen');
  
  var deltaHeight = 63;
  
  /*
   * In IE 7 and below, the right-panel will not be as height than in other browsers,
   * therfore, we increase the padding under the panel when using the scroll-pane.
   */
  if (jQuery.browser.msie && (parseInt(jQuery.browser.version) < 8)) {
    deltaHeight += 17;
  }
  
  if ($('#col1').exists()) {
   var col1 = $('#col1');
   var col1c = $('#col1_content');
   col1c.height(col3s.height());
   //col1c.height(col3s.outerHeight(true) - 10);
   if ($('#ds-panel_content').exists()) {
    var dsp_content = $('#ds-panel_content');
    if (dsp_content.height() > col3s.outerHeight(true) - deltaHeight) {     
      dsp_content.height(col3s.outerHeight(true) - deltaHeight);
      $('.scroll-pane').jScrollPane({
        scrollbarMargin:10,
        showArrows:true
       });
    }
   }
  } else {
   col3.css({margin: 0});
  }
  
  // CALCULATING-END
  
});

$(document).ready(function(){
	
	/* 
	 * jQuery Containers 
	 */
	
	$('#promo_rotator').jshowoff({ 
	  autoPlay: false,
		links: false,
		effect: 'fade',
		controlText: { play:'', pause:'', previous:'', next: '' }
	});

 /**
  * Date-Picker 
  */
 $('.datePicker').datepicker({
   changeMonth: true,
   changeYear: true,
   dateFormat: 'dd.mm.yy',
   minDate: 0,
   showOn : 'both',
   buttonImage : '/cms/images/calendar.png',
   buttonImageOnly : true,
   buttonText : 'calendar'
 });
	
	// DIALOGES

	var $loading = $('<img src="images/loading.gif" alt="loading" class="ui-loading-animation">');

	// DIALOG-IMAGE
	$('a.dialog-image').each(function() {
		var $link = $(this);
		var dw = ($link.attr('width') === undefined) ? 660 : parseInt($link.attr('width'));
		var dh = ($link.attr('height') === undefined) ? 530 : parseInt($link.attr('height'));
		var dt = ($link.attr('title') === undefined) ? 'INOVIS' : $link.attr('title');
		var dd = ($link.attr('description') === undefined) ? '' : '<br /><span>' + $link.attr('description') + '</span>';
		$link.click(function() {
			var $dialog = $('<div></div>')
				.html('<img src="' + $link.attr('href') + '" />' + dd)
				.dialog( {
					title : dt,
					width :  dw,
					height : dh,
					modal : true,
				close : function(event, ui) {
					$(this).remove();
				}
			});
			$dialog.dialog('open');
			return false;
		});
	});

	// DIALOG-LINK
	$('a.dialog-link').each(function() {
		var $dialog = $('<div></div>');
		var $link = $(this);
		var dw = ($link.attr('width') === undefined) ? 640 : parseInt($link.attr('width'));
		var dh = ($link.attr('height') === undefined) ? 480 : parseInt($link.attr('height'));
		var dt = ($link.attr('title') === undefined) ? 'INOVIS' : $link.attr('title');
		$link.click(function() {
			$dialog.html($loading.clone());
			$dialog.load($link.attr('href')).dialog( {
				title : dt,
				width : dw,
				height : dh,
				modal : true,
				close : function(event, ui) {
					$(this).remove();
				}
			});
			$dialog.dialog('open');
			return false;
		});
	});

});

