(function($){var a={},c="doTimeout",d=Array.prototype.slice;$[c]=function(){return b.apply(window,[0].concat(d.call(arguments)))};$.fn[c]=function(){var e=d.call(arguments),f=b.apply(this,[c+e[0]].concat(e));return typeof e[0]==="number"||typeof e[1]==="number"?this:f};function b(l){var m=this,h,k={},n=arguments,i=4,g=n[1],j=n[2],o=n[3];if(typeof g!=="string"){i--;g=l=0;j=n[1];o=n[2]}if(l){h=m.eq(0);h.data(l,k=h.data(l)||{})}else{if(g){k=a[g]||(a[g]={})}}k.id&&clearTimeout(k.id);delete k.id;function f(){if(l){h.removeData(l)}else{if(g){delete a[g]}}}function e(){k.id=setTimeout(function(){k.fn()},j)}if(o){k.fn=function(p){o.apply(m,d.call(n,i))&&!p?e():f()};e()}else{if(k.fn){j===undefined?f():k.fn(j===false);return true}else{f()}}}})(jQuery);

jQuery.fn.center = function (absolute) {
    return this.each(function () {
        var t = jQuery(this);

        t.css({
            position:    absolute ? 'absolute' : 'fixed', 
            left:        '50%', 
            top:        '50%', 
            zIndex:        '99'
        }).css({
            marginLeft:    '-' + (t.outerWidth() / 2) + 'px', 
            marginTop:    '-' + (t.outerHeight() / 2) + 'px'
        });

        if (absolute) {
            t.css({
                marginTop:    parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(), 
                marginLeft:    parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
            });
        }
    });
};

$.fn.resizeBG = function(){
	imgRatio = $('#fullbg img').width() / $('#fullbg img').height();
	var bgWidth = $(window).width(),
	bgHeight = bgWidth / imgRatio;
	if(bgHeight < $(window).height()) {
		bgHeight = $(window).height();
		bgWidth = bgHeight * imgRatio;
	}
	$("#fullbg img").width( bgWidth ).height( bgHeight );
	$("#checkers").width( bgWidth ).height( bgHeight );
}
    
$(window).resize(function(){$.fn.resizeBG()});

$(function() {
	// alle links in nieuw venster
	$("a[href*='http://']:not([href*='"+location.hostname+"'])").click( function() {
		window.open(this.href);
		return false;
	});
});

$.fn.moveBanners = function(){
	var numContainers = $('.banner-container').length;
	var numBanners = $('#ads .banner').length;
	
	if (numBanners > 0){		
		if (numContainers > 0){
			$('#ads').hide(); 
			
			
			var i = 1;
			$('#ads .banner').each(function(){
				if ((numContainers > 0)&&(i < 4)){
					$('.banner-container:eq(0)').append($(this));
				}
				
			 	if ((numContainers > 1)&&(i > 3)&&(i < 7)){
			 		$('.banner-container:eq(1)').append($(this));
			 	}else{
			 		$('.banner-container:eq(1)').hide();
			 	}
			 	
			 	if ((numContainers > 2)&&(i > 6)&&(i <10)){
			 		$('.banner-container:eq(2)').append($(this));
			 	}else{
			 		$('.banner-container:eq(2)').hide();
			 	}
			i++;
			});
		
			$('.banner-container .banner:first-child').addClass('first-child');
			$('.banner-container .banner:last-child').addClass('last-child');
		}
	}else{
		$('.banner-container').hide();
	}
}

$.fn.addProductFunctions = function(magazine){
	$('.loader').center(true);
	
	$.get('productForm.php', { 'magazine': magazine}, function(data) {
	  $('#mainfull').html(data);
	});
}

productFormCheck = function () {
	
	var error = 0;
	$('.required').each(function(){
		if ($(this).val() === ''){
			error = 1;
		}
	});
	
	if (error === 1){
		alert('Alle met een * gemarkeerde velden zijn verplicht.');
		return false;
	}else{
		return true;
	
	}
}

$.fn.ProductFormFunctions = function(magazine){
	$('#productMag').change(function(){
		$.fn.getProductCats($(this).val());
	});
	
	$('#productForm').ajaxForm({
		target: '#mainfull',
		beforeSubmit:  function(){
			return productFormCheck();
			$('.loader').show();
		},
		success: function(responce){
			$.doTimeout( 1000, function(){
			  $('img.loader').hide();
			  $('#productResult').fadeIn();
			});
			$.doTimeout( 4000, function(){
			  $.get('productForm.php', function(data) {
			    $('#mainfull').html(data);
			  });
			});
		}
	});
}

$.fn.getProductCats = function(magazine){
	if (magazine != ''){
		$.get('getCats.php', { 'magazine': magazine}, function(data) {
		  $('#productCatList').html(data);
		});
	}else{
		$('#productCatList').html('Selecteer een magazine');
	}
}

$.fn.checkImage = function(image, Source){	
	var fileExtention = image.substr(image.lastIndexOf('.'));
	if((fileExtention == '.png')||(fileExtention == '.gif')||(fileExtention == '.jpg')||(fileExtention == '.jpeg')||(fileExtention == '.JPEG')||(fileExtention == '.JPG')) {
		$('#'+Source).val(image);
	}else{
		alert('Deze afbeelding kan niet gebruikt worden.');
		$('input[name='+Source+']').val('');
	}
}


