// #header .widget date/time stuff
var monthArr = ['JAN','FEB','MAR','APR','MEI','JUN','JUL','AUG','SEP','OCT','NOV','DEC'];
	
function setMyDate() {
	var date = new Date();
	var iDate = date.getDate();
	var sMonth = date.getMonth();
	var iHour = date.getHours();
	var iMinutes = date.getMinutes();
	if(iMinutes<= 9){
		iMinutes = '0'+iMinutes;
	}

	var iSec = date.getSeconds();
	$('#header span.iDateToday').text(iDate + ' ' + monthArr[sMonth]);
	$('#header span.iTime').text(iHour + ':' + iMinutes);
};

function changeVid(mediaURL){
  var so = new SWFObject('http://www.breedveldstaal.nl/includes/plugins/flvplayer/player.swf','mpl','490','300','9');
  so.addParam('allowfullscreen','true');
  so.addParam('allowscriptaccess','always');
  so.addParam('wmode','transparent');
  so.addVariable('file','http://www.breedveldstaal.nl/filelib/interview-alain-witry2.flv');
  so.addVariable('author','Breedveldstaal');
  so.addVariable('title','Interview Alain Witry2');
  so.addVariable('frontcolor','ffffff');
  so.addVariable('lightcolor','ffffff');
  so.addVariable('stretching','exactfit');
  so.addVariable('skin','http://www.breedveldstaal.nl/includes/plugins/flvplayer/snel.swf');
  so.addVariable('controlbar','bottom');
  so.addVariable('autostart','true');
  so.write('videoBox');
}

function IsNumericComma(sText,bWhole) {
   var ValidChars = "0123456789,";
   if ( bWhole ){
    ValidChars = "0123456789";
   }
    
   var IsNumber=true;
   var Char;
   
   for (i = 0; i < sText.length && IsNumber == true; i++) { 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
   }
   return IsNumber;
}


function NumberHasComma(string) {
	if(IsNumericComma(string,true)){
		return false;
	} else {
		return true;
	}
}

function converToDot(string) {
	var newString = string.toString();
	if(NumberHasComma(string)){
		var passedString = string;
		var splitString = passedString.split(',');
		return splitString.join('.');			
	} else {
		return string;
	}
}

$(document).ready(function() {
	// #header .widget date/time stuff
	setMyDate();
	var timer = setInterval('setMyDate();', 10000);
	
	// sitemap
	$('ul.quickTree').quickTree();
	
	// fancy intro
	$("a#intro-layer").fancybox({
		'hideOnContentClick': false,
		'frameWidth' : 567,
		'frameHeight' : 312
	}).trigger('click');

	// fancy vids
	$("a.vids").fancybox({
		'hideOnContentClick': false,
		'frameWidth' : 490,
		'frameHeight' : 300
	});
	
	$("div.ivids a").fancybox({	
		'hideOnContentClick': false,			  
		'frameWidth' : 510,
		'frameHeight' : 315,
		'padding': 0,
		'callbackOnClose': function() {
		   $("#videoBox").empty();
		   }
		}); 

	//belMij Pop up stuff
	
	$("#belMijBtn").fancybox({
		'hideOnContentClick': false,
		'frameHeight': 260,
		'callbackOnShow' : function() {
			var $forms = $('#fancy_div form');
			$forms.validate({
				submitHandler: function(form) {
					var $this = $(this);
					var $serialize = $(form).serialize();
					//AJAX POST HERE	
					$.post('/modules/forms/front/task.php', $serialize);
					$(this.currentForm).hide();
					$(this.currentForm).parent().find('.thankyou').show();
					$('#simplemodal-container').css({'height':'auto'});
					return false;
				}
			});
		}
	});
	
	// fancy pics
	$("a.group").fancybox({
		'hideOnContentClick': false
	});

	//move the image in pixel
	var move = 0;
	
	//zoom percentage, 1.2 =120%
	var zoom = 1.1;

	//On mouse over those thumbnail
	$('.zitem').hover(function() {
		
		//Set the width and height according to the zoom percentage
		width = $('.zitem').width() * zoom;
		height = $('.zitem').height() * zoom;
		
		//Move and zoom the image
		$(this).find('img').stop(false,true).animate({'width':width, 'height':height, 'top':move, 'left':move}, {duration:200});
		
		//Display the caption
		$(this).find('div.caption').stop(false,true).fadeIn(200);
	},
	function() {
		//Reset the image
		$(this).find('img').stop(false,true).animate({'width':$('.zitem').width(), 'height':$('.zitem').height(), 'top':'0', 'left':'0'}, {duration:100});	

		//Hide the caption
		$(this).find('div.caption').stop(false,true).fadeOut(200);
	});
	
	//Set the number attribute with javascript to avoid a xhtml strict validation error.
	$('.step1 tr.data input.afmetingVak').attr('number', 'true');
	$('.step1 tr.dataModel input.afmetingVak').attr('number', 'true');
	
	$('input.afmetingVak').live('change', function() {
		var $this = $(this);
		var value = $this.val();
		$this.val(converToDot(value));
	});
	
	var formSerialization; // variable to keep the serialization publicly available.
	
	$('a.terugStap1').live('click',function() {
		var $this = $(this);
		var $thisForm =  $this.parents('form');
		$thisForm.siblings('.step1').show();
		$thisForm.hide();
		return false;
	});
	
	//offerte addMore
	$('a.addOneMore').live('click',function() {
		var $this = $(this);
		var $form = $this.parents('form');
		var $dataLength = parseInt($form.find('tr.data').length);
		var $dataModel = $form.find('tr.dataModel');
		$this.parents('tr').before($dataModel.clone().removeClass('dataModel hidden').addClass('data'));
		$form.find('tr.data').find('.ignore').removeClass('ignore');
		$form.find('tr.data:last').find('[name^=p0]').each(function() {
			var $this = $(this);
			var re = /p0/gi;
			var oldValue = $this.attr('name').toString();
			var newValue = oldValue.replace(re, 'p'+($dataLength+1));
			$this.attr('name',newValue)
		});
		
		$('#simplemodal-container').css({'height':'auto'});
		return false;
	});
	
	//offerte delete function
	$('tr.data td.delete a.deleteRow').live('click', function() {
		var $this = $(this);
		$this.parents('tr.data').remove();
		return false;
	});
	
	$('.formFancyBox').fancybox({
		frameWidth: 930,
		frameHeight: 480,
		hideOnContentClick: false,
		callbackOnShow: function() {
			var $forms = $('#fancy_div');
			var $step1 = $forms.find('.step1');
			var $step2 = $forms.find('.step2');
			var $step3 = $forms.find('.step3');
			
			$step1.validate({
				debug: true,
				ignore: '.ignore',
				showErrors: function(errorMap, errorList) {
					if(errorList.length >= 1) {
						$(this.currentForm).find('td.errorContainer').empty().append('<p>Het formulier bevat '
							+ this.numberOfInvalids() 
							+ ' fouten, gemarkeerd met een rode rand. Let op de volgende zaken.</p>'
							+ '<ul class="listErrors">'
							+ '<li>er moet een productomschrijving gekozen zijn</li>'
							+ '<li>het veld \'aantal\' mag alleen getallen bevatten</li>'
							+ '<li>het veld \'lengte\' mag alleen getallen bevatten</li>'
							+ '</ul>').show().parent().show();
						$(errorList).each(function() {
							$(this.element).addClass('ui-state-error');
						});
						$('#simplemodal-container').css({'height':'auto'});
					} else {
						$(this.currentForm).find('td.errorContainer').hide().parent().hide();
						$(this.currentForm).find('.ui-state-error').removeClass('ui-state-error');
					}
					//this.defaultShowErrors();
				},
				submitHandler: function(form) {
					var $this = $(this);
					var $serialize = $(form).serialize();
					formSerialization = $serialize+"&";
					$(this.currentForm).hide();
					$(this.currentForm).parent().find('.step2').show();
					$('#simplemodal-container').css({'height':'auto'});
					return false;
				}
			});
			
			$step2.validate({
				submitHandler: function(form) {
					var $this = $(this);
					var $serialize = $(form).serialize();
					formSerialization += $serialize;
					//AJAX POST HERE
					//alert(formSerialization);	
					$.post('/modules/forms/front/task.php', formSerialization);
					$(this.currentForm).hide();
					$(this.currentForm).parent().find('.step3').show();
					$('#simplemodal-container').css({'height':'auto'});
					return false;
				}
			});
			
			$step1.show();
		}
	});
	
	//image fader function
	$('.flashCont .fader').cycle({ 
	    fx:     'scrollRight', 
	    speed:   3000, 
	    timeout: 10000, 
	    next:   '.fader', 
	    pause:   1 
	});
	
	$('ul.expandable li.header').click(function(){
		var $this = $(this);
		$this.toggleClass('open');
		//$this.children('ul.liContent').toggle();
	});

	$('ul.expandable li.header ul').click(function(){
		//this stops toggeling the $('.box ul.expandable li.header > span').click() function
		return false;
	}); 

});	//END JQUERY READY
