var iActive = 1;
var bActive = false;
var iLastResize = true;
var oCheckInterval = false;

$(document).ready( function() {
	
	
	setTimeout( function() {
		backgroundInit(false);	
	},200);
	
	menuInit();
	preLoad();

	
	$(window).resize(function() {
		
		iLastResize = microtime();
		
		if(!oCheckInterval) {
			
			oCheckInterval = window.setInterval( function() {
				iDiv = (microtime() - iLastResize);
				
				if( iDiv > 100) {
					backgroundInit(false);
					window.clearInterval(oCheckInterval);
					oCheckInterval = false;
				}
			},50)
		}
		
	});
	
});

function clearInt() {
	clearInterval(oCheckInterval);
}

function microtime(get_as_float) {  
  
	var now = new Date().getTime() 
	return parseInt(now);
}  

function menuInit() {

	
	$('ul.web4nav a').click( function() {
		if(bActive) {
			return false;
		} else {

		
			
			if($(this).parent().parent().hasClass('level_1')) {
				// Main
				$('ul.web4nav a').removeClass('active');
				$(this).addClass('active');	
				Cufon.refresh('#web4nav_1 a');
				
				loadMenuItem(this.id.replace('menu_',''),true);
			} else {
				// Sub
				$(this).parent().parent().find('a').removeClass('active');
				$(this).addClass('active');	
				Cufon.refresh('#web4nav_1 a');
				
				loadMenuItem(this.id.replace('menu_',''),false);
			}
			return false;
			
			
		}
		
	});
}

function showSubmenu(sId) {
	
	
	
	var iMenu = sId.replace('menu_','');
	var sId = 'menu_li_'+iMenu;
	
	$('#menu li ul').each( function() {
		var sParent = $(this).parent().attr('id');
		if(sParent != sId) {
			$(this).css({marginRight: -1000});
		}
	});
	
	$('#'+sId+' ul').animate({marginRight: 0},'medium','easeInOutQuint');
	
}

function backgroundInit(bAnim) {
	

		
	
	
	var iImageWidth = aBackgroundWidth[iActive];
	var iImageHeight = aBackgroundHeight[iActive];

	
	
	
	var iWindowWidth = $(window).width();
	var iWindowHeight = $(window).height();

	
	
	
	var iOver = iImageWidth / iImageHeight;
	var iUnder = iImageHeight / iImageWidth;
	
		
	

	if( (iWindowWidth / iWindowHeight) >= iOver) {
		var iNewImgWidth = iWindowWidth;
		var iNewImgHeight = iUnder*iWindowWidth;
	} else {
		var iNewImgWidth = iOver*iWindowHeight;
		var iNewImgHeight = iWindowHeight;
	}
	
	

	// Diff
	iDiffWidth = (iNewImgWidth - iWindowWidth)/2;
	iDiffHeight = (iNewImgHeight - iWindowHeight)/2;

	iMarginLeft = iDiffWidth * -1;
	iMarginTop = iDiffHeight * -1;


	
	
	
	if(iMarginLeft > 0) {
		iMarginLeft = 0;	
	}

	if(iMarginTop > 0) {
		iMarginTop = 0;	
	}	
	

	$('#backgroundimage').css({width: iNewImgWidth, height: iNewImgHeight, marginTop: iMarginTop, marginLeft: iMarginLeft});	
	
	if(bAnim) {
		$('#backgroundimage').fadeIn();
	} else {
		$('#backgroundimage').show();
	}



}





function preLoad() {
	var sHtml = '';
	$.each(aBackgroundFiles, function(sKey,sValue) {
		
		sHtml += '<img src="'+LINKROOT+'/'+sValue+'" />';
	})
	
	
	
	$('#preload').html(sHtml);
}

function loadMenuItem(iItem,bDoSubs) {

	
	bActive = true;
	iActive = iItem;
	var iWidth = $('#content').width();
	var iPadding = parseInt( $('#content').css('paddingLeft')) + parseInt( $('#content').css('paddingRight'));
	var iMargin = (iWidth + iPadding) *-1;

	if(bDoSubs) {
		sSelector = 'ul#web4nav_1 ul'; 
	} else {
		sSelector = 'div#dummy';
	}

	
	$(sSelector).animate({marginRight: -1000},'medium','easeInOutQuint', function() {
		
	});
	
	$('#backgroundimage').fadeOut('fast', function() {

		// Switch background
		var sBackground = LINKROOT+'/'+aBackgrounds[ iItem ];
		$('#backgroundimage').attr({src:sBackground});
		backgroundInit(true);
		
		
		setTimeout(function() {

			$('#content').animate({marginLeft:iMargin},'medium','easeInOutQuint', function() {


				

				$('#content-wrap').load(LINKROOT+'/view/frontend/request/'+iItem+'?ajax=true', function() {
				
					if(bDoSubs) {
						showSubmenu(iItem);
					} 
					$('#content').removeClass().addClass('fill').addClass('item'+iItem);
					$('#content').animate({marginLeft:0},'medium','easeInOutQuint', function() {
								
					});
				});
			});
			

			bActive = false;
		},200);
	});
	
			
}
