jQuery.noConflict();
(function($){
	$(document).ready(function() {
		$('#normal_left .n1 img').each(function() {
			$(this).attr('id', 'home-flash');
			swfobject.embedSWF("/fileadmin/templates/html/images/flash/151_ProvNB_Banner_301x301_1.1.1.swf", "home-flash", "302", "302", "9.0.0", "/fileadmin/templates/html/images/flash/expressInstall.swf");
		});

		initHighlight.init($('#highlight'));
		$('a.print').bind('click', function(evt) {
			evt.preventDefault();
			window.print();
		});

		// absolute link is considered external
		$('a[href^="http://"], a[rel="external"]').attr('target', '_blank');

		// default settings voor velden op de homepage
		var naam = 'Naam';
		var email = 'E-mailadres';
		var plaats = 'Plaats';
		var straatnaam = 'Straatnaam';
		var huisnummer = 'Nr';

		// haal datum op
		var d = new Date();
		date_hours = d.getHours();

		$('#form-hour').val(date_hours);
		$('#form-minutes').val('05');

		autoFillFields('tt_address_name', naam);
		autoFillFields('tt_address_email', email);
		autoFillFields('plaats', plaats);
		autoFillFields('straatnaam', straatnaam);
		autoFillFields('huisnummer', huisnummer);

		if ($('#bestemmingswijzer-frame').html() != null) {
			// bestemmingswijzer iframe
			var querystring = window.location.search.substring(1);

			if (querystring == '') {
				return false;
			}

			var query_array = querystring.split('&');

			var categorien = query_array[0].split('=');
			categorien_split = categorien[1];

			var plaats = query_array[1].split('=');
			plaats_split = plaats[1];
			plaats_split = plaats_split.replace(/\+/g, ' ');

			var straat = query_array[2].split('=');
			straat_split = straat[1];
			straat_split = straat_split.replace(/\+/g, ' ');

			var nummer = query_array[3].split('=');
			nummer_split = nummer[1];
			nummer_split = nummer_split.replace(/\+/g, ' ');

			var hour = query_array[4].split('=');
			hour_split = hour[1];

			var minute = query_array[5].split('=');
			minute_split = minute[1];

			if (!query_array) {
				return false;
			}

			$('#bestemmingswijzer-frame').load(function(){
				// negeer form input/submit als je al geforward bent
				if ($(this.contentDocument).find('th').html() == 'Selecteer locatie') {
					return false;
				}

				// categorien
				if (categorien_split != 'Toon+alle+categorien') {
					// zet alle checkboxes uit
					$(this.contentDocument).find('.CheckBox').attr('checked', false);

					// zet alleen de geselecteerde checkbox aan
					var input = 'input[name="' + unescape(categorien_split) + '"]';
					$(this.contentDocument).find(input).attr('checked', true);
				} else {
					$(this.contentDocument).find('.CheckBox').attr('checked', true);
				}

				// fill fields
				$(this.contentDocument).find('input[name="user_town"]').val(plaats_split);
				$(this.contentDocument).find('input[name="user_street"]').val(straat_split);
				$(this.contentDocument).find('input[name="user_number"]').val(nummer_split);

				// selects
				$(this.contentDocument).find('select[name="hour"]').val(hour_split);
				$(this.contentDocument).find('select[name="minutes"]').val(minute_split);
			});
		}

		// laad classes
		powermail_message.init();
		direct_mail_message.init();

	/*
	// fix menu
	var kapje = $('#nav ul li:last-child a');

	$('#nav ul li.last:eq(0) a').after($(kapje));
	$('#nav ul li.last:eq(1)').remove();

	var liwidth = 0;

	$('#nav ul li.last a').each(function(){
		liwidth +=$(this).width();
		$(this).css('float','left')
	});

	$('#nav ul li.last').width(liwidth);

	$('#nav ul li:not(.active) a').hover(
		function(){
			jQuery(this).parent().addClass('hover');
		},
		function(){
			jQuery(this).parent().removeClass('hover');
		}
	);

	$('#nav ul li.last.active a:last-child').css('background-image',$('#nav ul li.last.active a:last-child').css('background-image'));
	*/
	});
})(jQuery);

function autoFillFields(fieldId, defaultValue) {
	fieldId = '#' + fieldId;

	jQuery(fieldId).attr('value', defaultValue);

	jQuery(fieldId).bind('focus', function(evt) {
		if (jQuery(fieldId).attr('value') == defaultValue) {
			jQuery(fieldId).attr('value', '');
		}
	});

	jQuery(fieldId).bind('blur', function(evt) {
		if (jQuery(fieldId).attr('value') == '') {
			jQuery(fieldId).attr('value', defaultValue);
		}
	});
}

var powermail_message = {
	box : null,
	init : function(){
		powermail_message.box = jQuery('#alertbox');

		jQuery('fieldset>div').hover(function() {
			if (jQuery(this).hasClass('gbb-validation-failed')) {
				var message = jQuery('.powermail_mandatory_js' , jQuery(this)).html();
					if (message){
						var position_top = jQuery(this).position().top+25;
						var position_left = jQuery(this).position().left+109;
						powermail_message.box.html(message).css('top', position_top).css('left',position_left).show();
					}
				}
			},
			function() {
				powermail_message.box.hide();
			}
		);
	},
	hover : function(obj){
		if (obj.hasClass('gbb-validation-failed')){
			var message = jQuery('.powermail_mandatory_js' , obj).html();
			if (message){
				var offset = obj.offset();
				powermail_message.box.html(message).offset(offset).show();
			}
		}
	},
	unhover : function (){
		powermail_message.box.html().hide();
	}
}
var direct_mail_message = {
		box : null,
		init : function(){
			direct_mail_message.box = jQuery('#alertbox');
			jQuery('fieldset>div').hover(
				function(){

					if (jQuery(this).hasClass('error')) {
						var message = jQuery('.dm_mandatory_js' , jQuery(this)).html();
						if (message){
							var position_top = jQuery(this).position().top+30;
							var position_left = jQuery(this).position().left+10;
							direct_mail_message.box.html(message).css('top', position_top).css('left',position_left).show();
						}
					}
				},
				function(){
					direct_mail_message.box.hide();
				}
			);
		},
		hover: function(obj){
			if (obj.hasClass('error')){
				var message = jQuery('.dm_mandatory_js' , obj).html();
				if (message){
					var offset = obj.offset();
					direct_mail_message.box.html(message).offset(offset).show();
				}
			}
		},
		unhover: function (){
			direct_mail_message.box.html().hide();
		}
	}
var initHighlight = {
	currentPosition : 1,
	oldPosition : 0,
	numberOfRows : 0,
	previous : '<a href="#" class="control" id="previous"><span>vorige</span></a>',
	next : '<a href="#" class="control" id="next"><span>volgende</span></a>',
	thisObj : null,
	timer : null,
	init : function(thisObj) {
		initHighlight.thisObj = thisObj;
		initHighlight.numberOfRows = jQuery(".row", initHighlight.thisObj).length;
		//jQuery("#highlight-nav").append(initHighlight.previous);
		for (x=1; x<=initHighlight.numberOfRows; x++) {
			var bulletClass = '';
			if (x == initHighlight.currentPosition) { bulletClass = 'active'; }
			jQuery("#highlight-nav").append('<a href="#" class="controlNumber '+bulletClass+'" id="b_'+x+'"><span>'+x+'</span></a>');
		}
		//jQuery("#highlight-nav").append(initHighlight.next);
		initHighlight.manageControls(initHighlight.currentPosition);

		jQuery('.controlNumber').bind('click', function(evt){
			evt.preventDefault();
			initHighlight.oldPosition = initHighlight.currentPosition;
			var temp = (jQuery(this).attr('id')).split("_");
			initHighlight.currentPosition = parseInt(temp[1]);
			initHighlight.controlIt(initHighlight.oldPosition, initHighlight.currentPosition);
			initHighlight.manageControls(initHighlight.currentPosition);
		});
		jQuery('.control')
			.bind('click', function(evt){
			evt.preventDefault();
			initHighlight.oldPosition = initHighlight.currentPosition;
			initHighlight.setNewCurrentPosition(this, initHighlight);
			initHighlight.controlIt(initHighlight.oldPosition, initHighlight.currentPosition);
			initHighlight.manageControls(initHighlight.currentPosition);
		});

		initHighlight.setTimerval();
		initHighlight.thisObj.hover(initHighlight.mouseover, initHighlight.mouseout);
	},
	setNewCurrentPosition : function(caller, initHighlight){
		if(jQuery(caller).attr('id')=='next') {
			if(initHighlight.currentPosition != initHighlight.numberOfRows) initHighlight.currentPosition++;
			else initHighlight.currentPosition = 1;
		} else {
			if(initHighlight.currentPosition != 1) initHighlight.currentPosition--;
			else initHighlight.currentPosition = initHighlight.numberOfRows;
		}

		initHighlight.setTimerval();
	},
	mouseover : function() {
		clearInterval(initHighlight.timer);
	},
	mouseout : function() {
		initHighlight.setTimerval();
	},
	setTimerval : function() {
		if(initHighlight.timer) {
			clearInterval(initHighlight.timer);
		}
		initHighlight.timer = setInterval('initHighlight.automate()', 6500);
	},
	manageControls : function(position){
//		if(position==1){ jQuery('#previous').hide() } else{ jQuery('#previous').show() }
//		if(position==initHighlight.numberOfRows){ jQuery('#next').hide() } else{ jQuery('#next').show() }
	},
	controlIt : function(oldPosition, newPosition){
		jQuery("a#b_"+oldPosition).removeClass('active');
		jQuery("a#b_"+newPosition).addClass('active');
		jQuery(".highlight"+oldPosition).hide();
		jQuery(".highlight"+newPosition).show();
	},
	automate : function(){
		var old = initHighlight.currentPosition;
		if (initHighlight.currentPosition == initHighlight.numberOfRows) {
			var newposition = 1;
			initHighlight.currentPosition=1;
		} else {
			var newposition = initHighlight.currentPosition + 1;
			initHighlight.currentPosition++;
		}
		initHighlight.controlIt(old, newposition);
		initHighlight.manageControls(newposition);
	}
};


