/*
 * Copyright : (c) 2009 Webfish IT Services
 * Website   : http://www.webfish.nl
 * Email     : info@webfish.nl
 * -------------------------------------------------------------------------
 *   $Id: functions.js 32 2009-08-26 11:30:02Z caspar $
 * -------------------------------------------------------------------------
 */

jQuery.fn.delay = function(time,func){
	return this.each(function(){
		setTimeout(func,time);
	});
};

jQuery.fn.clearForm = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
			return $(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};

function appendOnLoadAlert(str_msg)
{
	var appendOnLoadAlert_oldOnload = (window.onload) ? window.onload : function () { };
	window.onload = function () { appendOnLoadAlert_oldOnload(); alert(str_msg); }
}

var prev_archive_year = prev_archive_month = null;
function toggleArchive(str_year, str_month)
{
	if (typeof str_month != 'undefined')
	{
		// toggle month
		str_month = '#archief_'+ str_year + str_month;
		if (prev_archive_month != null)
		{
			$(prev_archive_month).toggle();
		}
		if (prev_archive_month == str_month)
		{
			prev_archive_month = null;
		}
		else
		{
			$(str_month).toggle();
			prev_archive_month = str_month;
		}
	}
	else
	{
		str_year = '#archief_'+ str_year;
		// collapse prev year/month, open year
		if (prev_archive_month != null)
		{
			$(prev_archive_month).toggle();
			prev_archive_month = null;
		}
		if (prev_archive_year != null)
		{
			$(prev_archive_year).toggle();
		}
		if (prev_archive_year == str_year)
		{
			prev_archive_year = null;
		}
		else
		{
			$(str_year).toggle();
			prev_archive_year = str_year;
		}
	}

	return false;
}

var str_prev = null;
function toggleItem(str_item)
{
	if (str_prev != null)
	{
		$(str_prev).toggle();
	}
	if (str_prev == str_item)
	{
		str_prev = null;
	}
	else
	{
		$(str_item).toggle();
		str_prev = str_item;
	}

	return false;
}

$(document).ready(function(){
	$('ul#menu li a img').not('#kop, #zoek, #social').hover(function(){
		$(this).attr('src', $(this).attr('src').replace('menu_1', 'menu_2'));
	},function(){
		$(this).attr('src', $(this).attr('src').replace('menu_2', 'menu_1'));
	});
	$('ul#menu li#kop a img').hover(function(){
		$(this).attr('src', '/images/logoHover.jpg');
	},function(){
		$(this).attr('src', '/images/logo.jpg');
	});

	$('a[rel=external]').attr('target','_blank');

	$('#imageCarousel').infiniteCarousel();

	$('#sponsorCycle').cycle({ 
		delay:  4000, 
		speed:  1000
	});

	$("ul#menu li#nieuwsbrief a").colorbox({width:"400px", height:"210px", inline:true, href:"#newsletterForm"});

	$('ul.papercut li a').colorbox({innerWidth:800, innerHeight:600, current: "Afbeelding {current} van {total}", previous: "Vorige", next: "Volgende", close: "Sluit venster"});

	$('#inputSearch').autocomplete({
		serviceUrl:'/ajax_search_autocomplete.php',
		minChars:2,
		deferRequestBy: 25, //miliseconds
		width: 149,
		// callback function:
		onSelect: function(value, data){ $('#btnSearch').click(); }
	});

	$('#submitAanmelden, #submitAfmelden').click(function (){
		var strEmail = $('#newsletterEmail').val();
		if ((strEmail != ''))
		{
			$.post(
				'/ajax_newsletter.php',
				{email: strEmail, btn: $(this).attr('id')},
				function (strData)
				{
					var blnSuccess = (strData.substr(strData, 1, 1) == '1');
					strData = strData.substr(1);
					if (blnSuccess)
					{
						$('#newsletterEmail').val('');

					}
					alert(strData);
					if (blnSuccess)
					{
						$('#newsletterContent').delay(500, function () { $('#cboxClose').click(); });
					}
				}
			);
		}
		else
		{
			alert('Vul uw e-mailadres in.');
		}
		return false;
	});

	$('#newsletterMessage').delay(2500, function () { $('#newsletterMessage').fadeOut('slow'); });
});
