var pngfix;

$(document).ready(function()
{
	if($('#webpromo').length > 0) {
		var flashvars = { file: 'webpromo_'+ currentLang +'.flv', image: 'flash/webpromo.jpg' };
		var params = { allowfullscreen: 'true', wmode: 'transparent' };
		swfobject.embedSWF("flash/flvplayer.swf", "webpromo", "690", "408", "7", "express.swf", flashvars, params);
	}
	if($('#webpromo_small').length > 0) {
		var flashvars = { file: 'webpromo_small_'+ currentLang +'.flv', image: 'flash/webpromo_small.jpg' };
		var params = { allowfullscreen: 'false', wmode: 'transparent' };
		swfobject.embedSWF("flash/flvplayer.swf", "webpromo_small", "190", "127", "7", "express.swf", flashvars, params);
	}
	
	
	// INSERT BR AFTER H2
	$('#product_teasers .teaser .content h2').after('<br/>');
	$('#home_teasers .box_action h2').after('<br/>');
	$('.home_teasers .box_action h2').after('<br/>');
	
	// PRODUCT TEASERS SAME HEIGHT
	var max_height = 0;
	$('#product_teasers .teaser .content').each(function(){
		var new_height = $(this).height();
		if(max_height < new_height) {
			max_height = new_height;
		}
	});
	if(max_height % 2 != 0) {
		max_height++;
	}
	$('#product_teasers .teaser .content').each(function(){
		$(this).height(max_height);
	});
	
	
	// FANCYBOX
	$("a.iframe").each(function(){
		$(this).fancybox({
			frameHeight: 200
		});
	});
	
	$("a.iframe_big").each(function(){
		$(this).fancybox({
			frameHeight: 500,
			frameWidth: 800
		});
	});
	
	$("a.iframe_custom").each(function(){
		var paramstring = $(this).attr('rel');
		var params = paramstring.split(",");
		
		for (var i in params) {
			var param = params[i].split(":");
			eval(param[0] + "=" + param[1]  +";");
		}
		
		$(this).fancybox({
			frameHeight: height,
			frameWidth: width
		});
	});
	
	
	//* IE rounded corners */
	if($.browser.msie) {
		$('.contentbox').each(function() {
			$(this).append('<div class="corner corner_tl"></div>');
			$(this).append('<div class="corner corner_tr"></div>');
			$(this).append('<div class="corner corner_bl"></div>');
			$(this).append('<div class="corner corner_br"></div>');
		});
		$('.corners').each(function() {
			$(this).append('<div class="corner corner_tl"></div>');
			$(this).append('<div class="corner corner_tr"></div>');
			$(this).append('<div class="corner corner_bl"></div>');
			$(this).append('<div class="corner corner_br"></div>');
		});
		
		$('.ctl').each(function() {
			$(this).append('<div class="corner corner_tl"></div>');
		});
		
		$('.ctr').each(function() {
			$(this).append('<div class="corner corner_tr"></div>');
		});
		
		$('.cbl').each(function() {
			$(this).append('<div class="corner corner_bl"></div>');
		});
		
		$('.cbr').each(function() {
			$(this).append('<div class="corner corner_br"></div>');
		});
		
		$('.corner').each(function() {
			var box_height = $(this).parent().height();
			if (box_height % 2 != 0) {
				// alert($(this).parent() +': '+ box_height);
				$(this).parent().height(box_height + 1);
			}
			
			var box_width = $(this).parent().width();
			if (box_width % 2 != 0) {
				// alert($(this).parent() +': '+ box_width);
				$(this).parent().width(box_width + 1);
			}
			
		});
		
		if(pngfix) {
			pngfix();
		}
	}
	
	
	
	
	// A BTN CHECK FOR SPAN
	$('a.btn').each(function(){
		var spans = $(this).find('span');
		
		if(spans.length < 1) {
			// $(this).wrapInner("<span></span>");
		}
	});
	
	$('a.btn_color').each(function(){
		var spans = $(this).find('span');
		
		if(spans.length < 1) {
			$(this).wrapInner("<span></span>");
		}
	});
	
	
	
	// HOME BLOCKS SAME HEIGHT
	var max_height = 0;
	$('#home_teasers .box_action').each(function(){
		var new_height = $(this).height();
		
		if(max_height < new_height) {
			max_height = new_height;
		}
	});
	$('#home_teasers .box_action').each(function(){
		$(this).height(max_height);
	});
	
	// Convert external links
	$('a[rel=external]').attr('target','_blank');
		
	/* SET TEASER HOME IMAGE */
	/*
	teaser_home = $('#home_teaser');
	
	if(teaser_home) {
		currentActive = teaser_home.find('li.active');
			
		if(currentActive) {
			var link = currentActive.find('a');
			
			var src = 'url("' + currentActive.find('img').attr('src') +'")';
			
			teaser_home.css('background-image', src);
			currentActive.css('background-image', src);
		
			$('a#home_teaser_link').attr('href', link.attr('href'));
		}
	}
	*/
	
    $.ajaxSetup(
	{
		global: false,
		cache: false,
		type: 'POST',
		dataType: 'html'			
	});

	if($.blockUI) {
	    $.blockUI.defaults.message = '<h1>' + orderPageMsg + '</h1>';
	    $.blockUI.defaults.centerX = true;
	    $.blockUI.defaults.centerY = true;
    }
    
    
   
	$(".showFirstParagraphOnly").each(function()
	{
		// showAllParagraph('#' + this.id, 'hide');
		setParagraph('#' + $(this).attr('id'));
	});
	
    refreshOrder();	
});

function setParagraph(element) {
	$(element).find('p:first').prependTo($(element));
	$(element).find('.toggle').hide();
	$(element).append('<a class="showAllLink" href="#" onclick="toggleParagraph(\''+element+'\'); return false;">' + txtWeergeven + '</a>');
}
function toggleParagraph(element) {
	var text = $(element).find('a.showAllLink').text();
	if(text == txtWeergeven) {
		$(element).find('a.showAllLink').text(txtVerbergen);
	} else {
		$(element).find('a.showAllLink').text(txtWeergeven);
	}
	
	$(element).find('.toggle').slideToggle();
}

function showAllParagraph(element, action)
{
	var noOfParagraphs = 0;
	
	$(element).find('p').each(function()
	{
		noOfParagraphs++;
		$(this).show();
		
		if (noOfParagraphs != 1 && action == 'hide')
		{
			$(this).hide();
		};
	});
	
	$(element).find('.showAllLink').remove();
	
	if (noOfParagraphs > 1 && action == 'hide')
	{
		$(element).append('<a class="showAllLink" href="#" onclick="showAllParagraph(\'' + element + '\', \'show\'); return false;">' + txtWeergeven + '</a>');
	}
	else if (noOfParagraphs > 1 && action == 'show')
	{
		$(element).append('<a class="showAllLink" href="#" onclick="showAllParagraph(\'' + element + '\', \'hide\'); return false;">' + txtVerbergen + '</a>');
	}
	
}

function loadTeaserImg(link) {
	currentActive = teaser_home.find('li.active');
	
	if(currentActive) {
		currentActive.attr('class', currentActive.attr('class').replace("active", ""));
		currentActive.css('background-image', "url('img/home_teaser_tab_bg.gif')");
		
		var src = 'url("' + link.find('img').attr('src') +'")';
		
		teaser_home.css('background-image', src);		
		link.parent().css('background-image', src);
		link.parent().addClass('active');
		
		$('a#home_teaser_link').attr('href', link.attr('href'));
	}
	
	
	
	return false;
}

function refreshOrder()
{
	$(".radio").each(function()
	{
		var current = this;
		
		// Plaatsen van de href in click
		$(current).click(function()
		{
			// Plaatsen van loading
			$.blockUI({ css:{ border: 'none', padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: '.7', color: '#fff' }}); 
			
			$.ajax(
			{
				url: orderPage,
				data: $('#subscriptions_adjust').serialize(),
				success: function(response)
				{
					// Plaatsen van de response
					$('#order_from').html(response);
					
					// Re-zetten van de ajax acties
					refreshOrder();
				
					// Verwijderen van de loading
					$.unblockUI();					
				}
			});
			return false;
		});
	});
}



function showProductTeaserInfo(element) {
	element.parent().append('<div class="infobox"></div>');
	element.parent().find('.infobox').html(element.parents('.content').find('.product_teaser_info').html());
	
	return false;
}