$(document).ready(function(){
	
	resizeHandler.init();
	
});

var resizeHandler = {
		
		
	init : function(){
		
		$(window).resize(function() {
			resizeHandler.onResize();
		});
		
		
		$(window).scroll(function () {
			var top = $(window).scrollTop();
			if(top > 0){
				top *= -1;
			}
			$('#ticketLayer').css('bottom', top + 'px');
			//appUtils.log('scrollTp' + $(window).scrollTop());
		});
		
		
		resizeHandler.onResize();
	},
	
	staticHeights	: {
		"page_extra_margin"	:	334,
		"ticket_footer"		:	59,
		"home_social"		:	60,
		"home_menu_margin"	:	340,
		"home_page_margin"	:	170,
		"houses_bar_small"	:	76,
		"houses_bar_large"	:	160
	},
	
	isHomeView	:	null,
	
	onResize : function(){
		var height =	$(window).height();
        var width  = 	$(window).width();
		
        $('#bodyOverlay').css('height', height + 'px');
        $('#bodyOverlay').css('width', width + 'px');
        
        // 290 = top + ticketbar + social icons (180+50+60)
        var needHeight = $('#mainMenu').height() + resizeHandler.staticHeights.page_extra_margin;
        appUtils.log('min needed height: ' + needHeight);
        
        if(height < needHeight){
        	height = needHeight;
        }
        
        var menuHeight		= height - resizeHandler.staticHeights.ticket_footer; 
        var bodyHeight		= height - resizeHandler.staticHeights.page_extra_margin + resizeHandler.staticHeights.home_social; // on a normal page
		var menuOffset		= $('#mainMenuContainer').offset();
		var contentOffset	= $('#pageScroller').offset();
        
        if ($('#mainMenuContainer').hasClass('home')) {
        	/* Adjust menu and body height */
        	menuHeight		= menuHeight - resizeHandler.staticHeights.home_menu_margin;
        	bodyHeight		= bodyHeight - resizeHandler.staticHeights.home_page_margin;
        	
        	/* Adjust housesBar */
        	if (resizeHandler.isHomeView === false || resizeHandler.isHomeView === null) {
	        	//appUtils.reFade($('#mainMenuContainer'));
	        	appUtils.reFade($('#pageScroller'));
	        	appUtils.reFade($('#housesBar'));
				if (parseInt(menuOffset.left)>=0) {
					var xval	= parseInt(menuOffset.left + 85);
					$('#housesBar').css({"background-position":xval+"px 0px"});
					/* Correct -17 pixels because of trees next to large house */
					$('#housesBar').html('<div style="width:120px;overflow:hidden;position:relative;top:0px;left:'+(xval-17)+'px;"><img src="/media/static/images/houses-grachtenhuis-large.png" border="0" height="'+resizeHandler.staticHeights.houses_bar_large+'" /></div>');
				}
				resizeHandler.isHomeView	= true;
				
	        	/* Show splash banner */
				menuHandler.toggleSplashBanner(true);
        	}
        	
        } else {
        	/* Adjust housesBar */
        	if (resizeHandler.isHomeView === true || resizeHandler.isHomeView === null) {
	        	//appUtils.reFade($('#mainMenuContainer'));
	        	appUtils.reFade($('#pageScroller'));
	        	appUtils.reFade($('#housesBar'));
				if (parseInt(contentOffset.left)>=0) {
					var xval	= parseInt(contentOffset.left);
					$('#housesBar').css({"background-position":xval+"px 0px"});
					/* Correct -9 pixels because of trees next to small house */
					$('#housesBar').html('<div style="width:60px;overflow:hidden;position:relative;top:0px;left:'+(xval-9)+'px;"><img src="/media/static/images/houses-grachtenhuis-small.png" border="0" height="'+resizeHandler.staticHeights.houses_bar_small+'" /></div>');
				}
				resizeHandler.isHomeView	= false;
        	} else {
        		//Skip animations
        	}
        	
           	/* Hide splash banner */
			//menuHandler.toggleSplashBanner(false);
        }

        /* Check number of subpages */
        var numSubPages	= 0;
        $('.webpage').each(function () {
        	numSubPages++;
        });

        appUtils.log("HEIGHT =====> " + $('.webPageContainer .viewport').height());
        
        $('#mainMenuContainer').css('height', menuHeight + 'px');
        //if (numSubPages && $('#targetPageContainer? .viewport').height()>=bodyHeight) {   <==== DO THIS TO AVOID A SCROLLBAR
	        $('#pageScroller').css('height', bodyHeight + 'px');
	        $('pageContainer').css('height', bodyHeight + 'px');
	        $('.webPageContainer').css('height', bodyHeight + 'px');
	        $('.webPageContainer .viewport').css('height', bodyHeight + 'px');
	        $('.webPageContainer .scrollbar').css('height', bodyHeight + 'px');
        //}
        
        /* Adjust sub pages */
        $('.webpage').each(function (i, elt) {

        	if (numSubPages>1) {
	        	var eltHeight = $(elt).height();
	        	
	        	if (eltHeight < bodyHeight) {
	        		appUtils.log('==>' + $(elt).attr('id') + '; ' + $(elt).height());
	        		$(elt).css('height', bodyHeight+'px');
	        	}
        	}
        	
        });
        
        resizeHandler.updateScrollbar('relative');
	},
	
	updateScrollbar : function(val) {
		appUtils.log(' ===> updateScrollbar to value [' + val + ']');
		
		var updateWith;
		switch (true) {
			default:
				updateWith = 'relative';
			break;
			case (parseInt(val)>=0):
				updateWith = parseInt(val);
			break;
			case (val==='top'):
				updateWith = 'top';
			break;
			case (val==='bottom'):
				updateWith = 'bottom';
			break;
			case (val==='relative'):
				updateWith = 'relative';
			break;
		}
		
		$('.webPageContainer').each(function(){
			try{
				$(this).tinyscrollbar_update(updateWith);
			} catch(e) { appUtils.log(' === exception in updateScrollbar ==='); appUtils.log(e); }
		});
		
	}

};
