$(document).ready(function() {
	__initBtnFlip();
	__initRoundedCorners();
	__initSlideShowHome();
	//__initActiesHome();
	__initLinkBlokkenHome();
	doAftellen(2012,9,2,10,30,00);

	__initFixSubmenu();

});



/**
* SLIDESHOW HOME
*/
function __initSlideShowHome() {
	$('#slides').cycle({
		x: 'fade'
	});
}
	    
/**
* Makkelijk buttons togglen
*/
function __initBtnFlip() {
	$('img').each(function(index, value) {
		if($(this).hasClass('btn_flip') && !$(this).hasClass('active')) {
			// Mouseover binden
			$($(this)).bind('mouseover', function() {
	    		$(this).attr('src',$(this).attr('src').substr(0,($(this).attr('src').length-5)) + '1' + $(this).attr('src').substr(($(this).attr('src').length-4),4));;
			});
			
			// Mouseout binden
			$($(this)).bind('mouseout', function() {
	    		$(this).attr('src',$(this).attr('src').substr(0,($(this).attr('src').length-5)) + '0' + $(this).attr('src').substr(($(this).attr('src').length-4),4));;
			});
		}
	});
}


/**
* RONDE HOEKEN
*/
function __initRoundedCorners() {
	$.fn.corner.defaults.useNative = false;
	$('.rounded').each(function(index, value) {
		// Class attribuut opvragen
		sClasses = $(this).attr('class');
		
		// Splitten
		aClasses = sClasses.split(' ');
		for(x in aClasses) {
			if(aClasses[x].substring(0,3) == 'px_') {
				// class met px als ronding, toepassen op element
				$(this).corner(aClasses[x].substring(3) + "px");
			}
		}
	});
}

function __initActiesHome() {
	$('.snippet.geel > .omschrijving > ul > li').bind('mouseover', function() {
		$(this).addClass('hover');
	});
	$('.snippet.geel > .omschrijving > ul > li').bind('mouseout', function() {
		$(this).removeClass('hover');
	});
}

function __initLinkBlokkenHome() {
	$('.linkblok > ul > li').bind('mouseover', function() {
		$(this).addClass('hover');
	});
	$('.linkblok > ul > li').bind('mouseout', function() {
		$(this).removeClass('hover');
	});
}

function doAftellen(toYear,toMonth,toDay,toHour,toMinute,toSecond){
	var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
	var today = new Date()									
	
	var nowYear = today.getYear();
	if(nowYear < 1000) {
		nowYear += 1900;
	}

	var nowMonth = today.getMonth();
	var nowDay = today.getDate();
	var nowHour = today.getHours();
	var nowMinute = today.getMinutes();
	var nowSecond = today.getSeconds(); 

	// datestring maken van nu en tijd waarnaartoe
	var nowDateString = montharray[nowMonth] + " " + nowDay + ", " + nowYear + " " + nowHour + ":" + nowMinute + ":" + nowSecond;
	var futureDateString = montharray[toMonth-1] + " " + toDay + ", " + toYear + " " + toHour + ":" + toMinute + ":" + toSecond;

	// Verschil											
	dd=Date.parse(futureDateString)-Date.parse(nowDateString)

	dday=Math.floor(dd/(60*60*1000*24)*1);
	dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
	dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
	dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)

	if(dd <= 0){
		/**
		* Aftellen is voorbij.
		**/
		$('#dagen_cijfers').html("0");
		$('#dagen_tekst').html("dagen");
		$('#uren_cijfers').html("00:00:00");

		/*window.location='http://www.1dagactie.nl';*/
	} else {
		/**
		* De tijd is nog niet voorbij
		**/							
		if(dday == 1) {
			$('#dagen_tekst').html("Dag");
		} else {
			$('#dagen_tekst').html("Dagen");
		}				
		$('#dagen_cijfers').html(dday);
		$('#uren_cijfers').html(pad(dhour,2) + ':' + pad(dmin,2)+ ':' + pad(dsec,2));
		
		setTimeout(function(){doAftellen(toYear,toMonth,toDay,toHour,toMinute,toSecond)},1000);

	}

}

function pad(number, length) {
   
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
   
    return str;

}
is_at_mouse_out = false;
var t = null;
var submenu = null;
function doHideSubmenu(el) {
	if(t != null && submenu != null) {
		submenu.hide();
	}
	t = null;
	submenu = null;
	//is_at_mouse_out=false;
}
function __initFixSubmenu () {
	// Mouseout fix voor button naast open submenu
   	$('.mouseout').each(function(index, value) {
		$($(this)).bind('mouseover', function() {
			// Parent submenu zoeken en hiden
			is_at_mouse_out = true;
			$(this).parent().parent().hide();
			
		});
		$($(this)).bind('mouseout', function() {
			// Parent submenu zoeken en hiden
			is_at_mouse_out = false;
		});
   	});

   	$('ul#menu > li').each(function(index, value) {
		$($(this)).bind('mouseover', function() {
			if (t!=null)
				clearTimeout(t);
						
			t = null;
			submenu = null;
			
			// Alle submenu's verbergen
			$('.submenu').each(function(index, value) {
				$(this).hide();
			});
			
			
			// Parent submenu zoeken en hiden
			if(is_at_mouse_out == false) {
				submenu = $(this).find('.submenu').show();
				if(submenu.length > 0) {
					submenu.each(function(index, value) {
						image = $(this).parent().find('img');
						image.attr('src',image.attr('src').substr(0,(image.attr('src').length-5)) + '1' + image.attr('src').substr((image.attr('src').length-4),4));;
					});
				}

				
				if (!$(this).find('.fix_ie7').hasClass('fixed'))
				{
					$(this).find('.fix_ie7').addClass('fixed');
					$(this).find('.fix_ie7').css('width',($(this).find('.content').width()-10) + "px");
					$(this).find('.fix_ie7').css('margin-top',"-" + ($(this).find('.content').height()) + "px");
					$(this).find('.fix_ie7').css('height',($(this).find('.content').height()) + "px");
				}
				
			}
			
			
		});
		$($(this)).bind('mouseout', function() {
			submenu = $(this).find('.submenu').show();
			if(submenu.length > 0) {
				submenu.each(function(index, value) {
					image = $(this).parent().find('img');
					image.attr('src',image.attr('src').substr(0,(image.attr('src').length-5)) + '0' + image.attr('src').substr((image.attr('src').length-4),4));;
				});
			}
		});
   	});
   	$('ul#menu > li > .submenu').each(function(index, value) {
		$($(this)).bind('mouseout', function() {
			
			submenu = $(this);
			t = setTimeout("doHideSubmenu()",1000);
		});
		$($(this)).bind('mouseover', function() {
			
			if (t!=null)
				clearTimeout(t);

			submenu = null;
			t = null;
		});
	});
	/**
	* CLICK op hoofditem
	*/
	$('ul#menu > li > .submenu > .top').bind('click', function() {
		a = $(this).parent().parent().find('a');
		if(a.length > 0) {
			window.location=a.attr('href');
		}		
	});
}

