function reloadTypefaceText()
{
	$('.typeface-js, h1, h2, h3, h4, h5').each(function(i) {	
		_typeface_js.replaceText(this); 
		$(this).css({ 'visibility' : 'visible' }); 
	});
}


$(document).ready(function() {
	$('div').supersleight();
	$('img').supersleight();

	$(window).scroll(function() {
        $('.popup').each(function() {
    		if ($(this).css('display') != 'none') {
    			//var popup_id      = $(this).attr('id');
	    		//var scrollTimeout = setTimeout("$('#" + popup_id + "').centerPopup({animate: true})", 200);
    		}
    	});
    });
    
    // Popups centreren bij scherm resize
    $(window).resize(function() {
    	if (curPopup != '') {
    		$(this).stop();
    		resizeBackground();
    	}
    	
    	$('.popup').each(function() {
    		if ($(this).css('display') != 'none') {
    			$(this).centerPopup({animate: true});
    		}
    	});
    });
    
    
    
    // Popups laden
	$('.popup-button:not(.loader)').click(function() {
		var rel = $(this).attr('rel');
		$('#' + rel).centerPopup({animate: false});
		$('#' + rel).loadPopup();
	});
    
	
	// Popups pas laten zien na inladen ajax data
	$('.popup-button.loader').click(function() {
		var rel   = $(this).attr('rel');
		var popup = $('#' + rel);
		
		$('#popup-loader').centerPopup();
		$('#popup-loader').loadPopup({speed: 1500});
				
		switch (rel)
		{
			case 'test1':
				popup.load('ajax/get_content.php', {}, function() {
					popup.centerPopup({animate: false});
					popup.loadPopup();
				});
				break;
			
			default:
				popup.centerPopup({animate: false});
				popup.loadPopup();
				break;
		}
	});
	
	
	// Webshop product
	$('.product-box').livequery(function() {
		$(this).hover(
			function() {
				$(this).css("background", "url('/site/img/bg-webshop-product-box.gif')");
			},
			function() {
				$(this).css('background', '');
			}
		);
	});

	// Webshop detail popup close button
	$('.webshop-close').livequery(function() {
		$(this).click(function() {
			$('#popup-webshop').disablePopup({fadeBg: true});
		});
	});
	
	
	
	// Popups laden zonder page-refresh
	$('.widget').not('.popup-button, .no-button').click(function() {
		var rel   = $(this).attr('rel');
		var href  = $(this).attr('href');
		
		if ($('#' + rel).length > 0) 
		{
			$('#' + rel).centerPopup({animate: false});
			$('#' + rel).loadPopup();
			
			return false;
		} 
		else 
		{
			var div = $('<div class="popup" id="' + rel + '">');
			
			if (href.indexOf('?') == -1) {
				var href_new = href + '?popup=1';
			} else {
				var href_new = href + '&popup=1';
			}
			
			div.load(href_new, function() {
				$('#popups').append(div);
				$('#' + rel).centerPopup({animate: false});
				$('#' + rel).loadPopup();
				
				reloadTypefaceText();
				$('div').supersleight();
			});
			
			return false;
		}
	});
	
	
	// Widget hovers
	$('.widget').not('.popup-button, .no-button').hover(
		function() {
			var bgImg = $(this).css('background-image');
			var href  = $(this).attr('href');

			$("#widget-menu a[href='" + href + "']").addClass('active');
			
			// IE6 PNGFix?
			var background_original = $(this).css('background-image-original');
			if (background_original != undefined && background_original != '') {
				bgImg = background_original;
			}
			
			// Achtergrond hover?				
			if (bgImg != undefined && bgImg.indexOf('-off') != -1) {
				var newBgImg = bgImg.replace('-off', '-on');
				$(this).css('background-image', newBgImg);
				$(this).supersleight();
			}
			
			// Image hover binnen widget?
			if ($(this).children('img').length > 0) {
				var imgSrc = $(this).children('img').attr('src');
				
				// IE6 PNGFix?
				if ($(this).children('img').attr('src_original') != undefined) {
					imgSrc = $(this).children('img').attr('src_original')
				}
				
				if (imgSrc.indexOf('-off') != -1) {
					var newImgSrc = imgSrc.replace('-off', '-on');
					$(this).children('img').attr('src', newImgSrc);
				}
				
				$(this).children('img').supersleight();
			}
		},
		function() {
			var bgImg = $(this).css('background-image');
			var href  = $(this).attr('href');
			
			$('#widget-menu a[href=' + href + ']').removeClass('active');
			
			// IE6 PNGFix?
			var background_original = $(this).css('background-image-original');
			if (background_original != undefined && background_original != '') {
				bgImg = background_original;
			}
			
			// Achtergrond hover?
			if (bgImg != undefined && bgImg.indexOf('-on') != -1) {
				var newBgImg = bgImg.replace('-on', '-off');
				$(this).css('background-image', newBgImg);
				$(this).supersleight();
			}
			
			// Image hover binnen widget?
			if ($(this).children('img').length > 0) {
				var imgSrc = $(this).children('img').attr('src');
				
				// IE6 PNGFix?
				if ($(this).children('img').attr('src_original') != undefined) {
					imgSrc = $(this).children('img').attr('src_original')
				}
				
				if (imgSrc.indexOf('-on') != -1) {
					var newImgSrc = imgSrc.replace('-on', '-off');
					$(this).children('img').attr('src', newImgSrc);
				}
				
				$(this).children('img').supersleight();
			}
		}
	);
	
	
	// Overlay popup exit
	$("#overlay-bg").click(function() {
		$('.popup').not('#popup-agecheck, #popup-preview').disablePopup({fadeBg: true});
		return false;
	});
	
	
	
	// Preloader
	$('.widget, .widget-image-hover').each(function() {
		var bgImg  = $(this).css('background-image');
		var srcImg = $(this).attr('src');
		var newImg = false;
						
		if (bgImg != undefined && bgImg.indexOf('-off') != -1) {
			var newImg = bgImg.replace('url("', '').replace('")', '').replace('-off', '-on');
		}
		
		if (srcImg != undefined && srcImg.indexOf('-off') != -1) {
			var newImg = srcImg.replace('-off', '-on');
		}
		
		if (newImg != false) {
			var imageObj = new Image();
			imageObj.src = newImg;
		}
	});
});