var Dbg = {

	wWidth: 0,						// window width
	wHeight: 0,						// window height
	topImgWidth: 1920,				// real width of top image
	topImgHeight: 681,				// real height of top image
	homeTopImgHeightRatio: 0.7,		// height ratio of top image on homepage
	pageTopImgHeight: 325,			// height of top image on page

	init: function()
	{
		this.wWidth = parseInt($(window).width());
		this.wHeight = parseInt($(window).height());
		$(".home .top-image").css("height", (this.wHeight*this.homeTopImgHeightRatio)+"px");
		$(".lightbox").css("opacity", "0.7");

		$("ul li:first-child").addClass("first");
		$("ul li:last-child").addClass("last");

		if($.browser.msie && $.browser.version=="6.0")
		{
			DD_belatedPNG.fix('.logo img, .top-nav li a, .main-box, .box img, #breadcrumbs a, #breadcrumbs span, .box-link-01, \
							.box-link-02, .box-link-03, .box-link-04, .btn-link, .footer-nav, .close-contact-form, .facebook, .twitter, .hyves \
							.validation-info-correct, .validation-info-error, validation-ico img ');
		}
		// in ie7 the footer runs through the content with a max-height 640px...
		if($.browser.msie && $.browser.version!="7.0")
			$('.container').css('max-height', '640px');
	},
	
	resizeWindow: function()
	{
		this.wWidth = parseInt($(window).width());
		this.wHeight = parseInt($(window).height());
		var n = this.wHeight*this.homeTopImgHeightRatio;
		if(n < 500)
			n = 500;
		if(n > 900)
			n = 900;
		$(".home .top-image").css("height", n+"px");
	},

	loadInfo: function(el, dest)
	{
		var swt = $(window).scrollTop();
		swt += 200;
		
		if($.browser.msie && $.browser.version=="6.0")
		{
			$(".info-box-01, .info-box-02").css("top", swt+"px");
		}
		$.ajax(
		{
			url: $(el).attr("href"),
			type: "GET",
			dataType: "html",
			success: function(data)
			{
				$(dest).children(".info-content").empty().append(data);
				$(dest).fadeIn(200);
			}
		});
	}
}

var validmail = function(s) {
	mailregex = new RegExp('^\\S+@\\S+\\.\\S+$');
	return !!mailregex.exec(s);
};

var contactvalidate = function() {
	elems = [
	'form#sendFriend input[name=recipientmail]',
	'form#sendFriend input[name=sender]'];
	for(i = 0; i < elems.length; i++) {
		var e = $(elems[i]);
		if(!e.val() || e.val() == e[0].defaultValue)
			return false;
	}
	if(!$('form#sendFriend textarea[name=message]').val())
		return false;

	return validmail($('form#sendFriend input[name=recipientmail]').val());
};

$(document).ready(function()
{
	Dbg.init();

	$(window).resize(
		function()
		{
			Dbg.resizeWindow();
		});

	$("a[href=#]").live("click",
		function(e)
		{
			e.preventDefault();
		})

	$("#header a.info-icon").live("click",
		function(e)
		{
			e.preventDefault();
			Dbg.loadInfo($(this), ".info-box-01");
		});

	$("#content a.info-icon").live("click",
		function(e)
		{
			e.preventDefault();
			Dbg.loadInfo($(this), ".info-box-02");
		});

	$(".close-info-box").live("click",
		function()
		{
			$(this).parent().fadeOut(200);
		});

	$(".close-contact-form, .lightbox").live("click",
		function()
		{
			$(".lightbox, .contact, .contactsend").fadeOut(200);
		});
	$(window).scroll(
		function()
		{
			if($.browser.msie && $.browser.version=="6.0")
			{
				$(".page-01").css("background-position", "center "+$(window).scrollTop()+"px");
				$("#content .form-rows li").css("background", "#f2f2f2");
			}
		});
	
	$(".sub-nav li:first-child").addClass("first");
	$(".sub-nav li:last-child").addClass("last");

	$(".outbound-click").click(function() {
		recordOutboundLink(this, 'Conversion', 'kleurjeleven.mentalshareproducts.nl');
		return false;
	});

	var resetSendFriend = function() {
		if($('#sendFriend').length != 0)
			$('#sendFriend')[0].reset();
	};

	$("a.contact-form, a.x-delen").click(function() {
		resetSendFriend();
		$(".lightbox").css("height", $("#page").css("height"));
		$(".lightbox, .contact").fadeIn(200);
		return false;
	});

	$(".x-contactsend").click(function() {
		$('.contacterror, .contactversturen-success, .contactversturen-error').hide();
		$('.contactversturen-send').show();

		if(!contactvalidate()) {
			$('.contacterror').slideDown();
			return false;
		}

		$(".contact").fadeOut(200, function() {
			$('contactversturen-opt').hide();
			$('contactversturen-send').show();
			var get = function(name, elem) {
				if(!elem)
					elem = 'input'
				return $('form#sendFriend '+elem+'[name='+name+']').val();
			};
			$('.contact-tomail').text(get('recipientmail'));
			$('.contact-from').text(get('sender'));
			$('.contact-message').text(get('message', 'textarea'));
			$(".contactsend").fadeIn(200);
		});
		return false;
	});

	var tellafrienderror = function(r) {
		$('.contactversturen-error').slideDown();
	};
	var tellafriendok = function(r) {
		if(r.status != 'ok')
			return tellafrienderror();
		$('.contactversturen-send').slideUp(null, function() {
			$('.contactversturen-success').slideDown();
		});
		resetSendFriend();
	};

	$(".x-contactsendsubmit").click(function() {
		$('.contactversturen-success, .contactversturen-error').hide();
		$.post('/cgi/tellafriend/', $('form#sendFriend').serialize(), tellafriendok, 'json').error(tellafrienderror);
		return false;
	});

	$(".x-contactsendclose").click(function() {
		$(".lightbox, .contactsend").fadeOut(200);
		return false;
	});

	var w = $(window).width();
	var suf = '1030';
	if(w <= 1030) suf = '-1030';
	else if(w <= 1300) suf = '-1300';
	else if(w <= 1640) suf = '-1640';
	else if(w <= 1960) suf = '-1960';
	else suf = '-full';
	$('.top-image').addClass('top-image'+suf);

	$(window).resize();
});

