/**
 * @author Bernhard Berger
 */
jQuery.noConflict();	

jQuery(document).ready(function(){
	
	/**
	 * @var destroy		boolan		;tells jQuery if the content should
	 * 								 be destroyed on a click outside of the box
	 */
	var destroy = false;
	var url;

	var content = jQuery("#content");
	var container = jQuery("#container");

	jQuery(container).hide();
	jQuery(container).before('<div id="preload"><div id="progress-bar"></div></div>');
	jQuery(container).hide();
	
	jQuery(content).removeClass("noJS");
	
	var init = function() {
		clickables = jQuery("#menu li a, #infobar a.imprint, #infobar a.anfahrt, #tt_address_form a, a");
	}
	
	jQuery("#progress-bar").progressbar({
		value: 1
	});

	var img2preload = jQuery('#images-to-preload').html();
	preloadImages = img2preload.substr(0, img2preload.length - 1).split(',');
	
	preloadImages[preloadImages.length + 1] = "fileadmin/templates/img/bg.png";
	
	var stepSize = Math.floor(100 / preloadImages.length + 1);
	var progress = 1

	jQuery("#progress-bar").progressbar('option','value', progress);

	jQuery.preload(preloadImages, {
	    base:'',
	    ext:'',
		onComplete : function(pbar) {
			progress += stepSize;
			jQuery("#progress-bar").progressbar('option','value', progress);
		},
		onFinish : function() {
			jQuery("#progress-bar").progressbar('option','value', 100);
			jQuery("#preload").hide();
			jQuery(container).fadeIn(500);
			
			        jQuery(content).hide();
		
		
		destroy = true;
	

		if (jQuery(content).html() != "<!--TYPO3SEARCH_begin--><!--TYPO3SEARCH_end-->") {
			jQuery(content).dialog({
				autoOpen : true,
				hide: 'scale',
				show : 'scale',
				minWidth: 300,
				maxWidth : 830,
				width: getDialogWidth(content),
				height: getDialogHeight(content),
				maxHeight: 601,
				modal: true,
				resizable : true,
				position : 'center',
				dialogClass: 'content-3sixty'
			});
		
		init();
		}
		}
	});
	
	var resizeViewport = function() {
		if (jQuery(window).height() < jQuery(container).height()) {
			jQuery(container).addClass('small-viewport-y');
		} else {
			jQuery(container).removeClass('small-viewport-y');
		}
		if (jQuery(window).width() < jQuery(container).width()) {
			jQuery(container).addClass('small-viewport-x');
		} else {
			jQuery(container).removeClass('small-viewport-x');
		}
	}
	
	resizeViewport();
		
	jQuery(window).resize( function() {
		resizeViewport();
	});
		
	var getDialogHeight = function getDialogHeight(dlgContent) {
		var cMaxHeight = 600;
		var cHeight = jQuery(content).height();
		
		if (cHeight >= cMaxHeight) {
			return cMaxHeight;
		} else {
			return "auto";
		}
	}		
		
	var getDialogWidth = function getDialogWidth (dlgContent) {
		var cMaxWidth = 830;
		var cWidth = jQuery(content).width();
		
		if (cWidth > cMaxWidth) {
			return cMaxWidth;
		} else {
			return cWidth + 30;
		}
	}	
		
		
	var clickables = jQuery("#menu li a, #infobar a.imprint, #infobar a.anfahrt, #tt_address_form a");	
	
		
	/**
	 * 	click on button -> content opens
	 */
    jQuery(clickables).click(function(){
        jQuery(content).hide();
		jQuery(content).html("");
		
		
		destroy = true;
		
		var url = jQuery(this).attr("href");
		url = url + "&type=1707";
		
		var dialogTitle = jQuery(this).attr("title");
		
		jQuery(content).load(url, function() {
			jQuery(content).dialog({
				autoOpen : true,
				hide: 'scale',
				show : 'scale',
				minWidth: 300,
				maxWidth : 830,
				width: getDialogWidth(content),
				height: getDialogHeight(content),
				maxHeight: 601,
				modal: true,
				resizable : true,
				position : 'center',
				title : dialogTitle,
				dialogClass: 'content-3sixty'
			});
		});
		
		init();
		
        return false;
    });
	
	
	/**
	 * bounce effect on button-hover
	 */
	jQuery("#menu li a").hover(function() {
		jQuery(this).effect("bounce", {	
			times : 5,
			distance : 5,
			direction: "up" 
		}, 300);
	});
	
	
	
	jQuery(content).hover(function() {
		destroy = false;
	}, function() {
		destroy = true;
	});

	jQuery(content).bind('dialogclose', function(event, ui) {
		jQuery(this).dialog('destroy');
	});
	
	jQuery("body, #content-close").click(function() {
		if (destroy) {
			jQuery(content).dialog('close');
		}
	});
});

