var changed = false;

$(function(){
	$('.left-content a.tip').cluetip({
		 local:true
		,topOffset:0
		,leftOffset: -25
		,positionBy:'fixed'
		,dropShadow:false
		,sticky:true
		,mouseOutClose:true
		,closePosition:'title'
		,closeText:''
		,onShow:function(){ 
			$('#cluetip-inner ' + $(this).attr('href') +' a.cueTipLightBox').lightBox(); 
		}
	});	  
	$('.leftcontent a.tip').cluetip({
		 local:true
		,topOffset:0
		,leftOffset: -25
		,positionBy:'fixed'
		,dropShadow:false
		,sticky:true
		,mouseOutClose:true
		,closePosition:'title'
		,closeText:''
		,onShow:function(){ 
			$('#cluetip-inner ' + $(this).attr('href') +' a.cueTipLightBox').lightBox(); 
		}
	});	  
	
	$('.right-content a.tip').cluetip({
		 local:true
		,topOffset:0
		,leftOffset: -250
		,positionBy:'fixed'
		,dropShadow:false
		,sticky:true
		,mouseOutClose:true
		,closePosition:'title'
		,closeText:''
		,onShow:function(){ 
			$('#cluetip-inner ' + $(this).attr('href') +' a.cueTipLightBox').lightBox(); 
		}
	});
	$('.rightcontent a.tip').cluetip({
		 local:true
		,topOffset:0
		,leftOffset: -250
		,positionBy:'fixed'
		,dropShadow:false
		,sticky:true
		,mouseOutClose:true
		,closePosition:'title'
		,closeText:''
		,onShow:function(){ 
			$('#cluetip-inner ' + $(this).attr('href') +' a.cueTipLightBox').lightBox(); 
		}
	});
	
	$('a.newWindow').click(function(){
		window.open($(this).attr('href')); 
		return false;
	}); 		   
		   
	$('form#contactdata').submit(function(){
		var checkFields = new Array("project_desc","bestandsnaam","bouwwerk_desc", "bouwwerk_adres", "name", "email");
		var returnVal = true;
		for(i=0;checkFields.length>i;i++){
			if($('form#contactdata #'+checkFields[i]).val().length == 0){
				returnVal = false;
				$('form#contactdata #'+checkFields[i]).addClass('error');
			}else
				$('form#contactdata #'+checkFields[i]).removeClass('error');

		}
		if(!returnVal)
			alert('Niet alle verplichte velden zijn ingevuld!');
			
		return returnVal;
	});	   
		   
	$('form#RekentoolForm').submit(function(){
							  
		var form	= $(this);			  
		var GUID 	= $('#CALC_GUID').val();
		var ACTION 	= $('#ACTION').val();
		
		if(GUID.length == 0 || changed || ACTION == 'calc' || ACTION == 'test'){
			form.animate({ opacity : '0.25' }, 200, function(){
				$('.nextstep').hide();
				$('.resultDiv').addClass('faded');
				$('#reductieresultholder').addClass('reductiefaded');
				
				form.after('<div id="loadingbox">Herberekening gestart 1 moment geduld aub</div>');
				
				var LoadingBox		= $('#loadingbox');
				var DataString		= '';
				var DataSpacer		= '';
				var ajaxUrl			= '/calculate.aspx';
				var ajaxType		= form.attr('method').toUpperCase();
	
				$('#'+form.attr('id')+' input, #'+form.attr('id')+' select').each(function(){
					if($(this).attr('name') != undefined && $(this).attr('name').length > 0){
						if($(this).attr('type') != 'checkbox'){
							var _val = $(this).is(':disabled')? '__disabled' : $(this).val();
							DataString += DataSpacer+$(this).attr('name')+'='+_val;
							DataSpacer = '&';
						}else if($(this).attr('type') == 'checkbox'){
							DataString += DataSpacer+$(this).attr('name')+'='+($(this).is(':checked')?'1':'0');
							DataSpacer = '&';
						}
					}
				});
	
				$.ajax({
					type: ajaxType,
					data: DataString,
					url: ajaxUrl,
					cache: false,
					error:  function(result){
						$('#'+form.attr('id')+' input#CALC_GUID').val('');
						LoadingBox.html('Foutieve invoer');
						LoadingBox.addClass('error');
						setTimeout(function(){
							LoadingBox.remove();
							form.animate({ opacity : 1 }, 200);
						}, 2000);
						
					},
					complete: function(result){
						var _json = $.parseJSON(result.responseText);
						if(!_json.error.found){
							$.each(_json.data, function(k, v) {
								if(k.length > 0)
									setToolInputValue(form.attr('id'), k, v);
							});
							
							$.each(_json.reductie.data, function(k, v) {
								if(k.length > 0)
									setToolInputValue(form.attr('id'), k, v);
							});
							
							changed = false;
							$('#CALC_GUID').val(_json.guid);
							
							form.attr('action', '?' + _json.guid + '/contactdata');
							LoadingBox.html('Berekening voltooid.').addClass('done');
							
							if(_json.reductie.unfade)
								$('#reductieresultholder').removeClass('reductiefaded');
							
							$('.resultDiv').removeClass('faded');
							$('.nextstep').show();
						}else{
							$('#'+form.attr('id')+' input#CALC_GUID').val('');
							LoadingBox.html(_json.error.message);
							LoadingBox.addClass('error');
						}
						
						window.location = '#right-content';
						
						setTimeout(function(){
							LoadingBox.remove();
							form.animate({ opacity : 1 }, 200);
						}, 4000);
					}
				});
			});
																	  
			return false;
		}else if(ACTION == 'contactdata'){
			if(GUID.length == 0 || changed){
				alert('Klik eerst op Herbereken');
				return false;
			}
			return true;
		}
		
		return false;
	});
	
	$('form input, form select').change(function(){
		changed = true;
		$('.nextstep').hide();
	});
});

function setToolInputValue(formId, fieldId, Value){
	var _obj = $('#'+formId+' input#'+fieldId);
	
	if(!_obj.is(":disabled")){
		if(_obj.attr('type') == 'checkbox'){
			_obj.attr('checked', (Value=='1'));
		}else{
			$('#'+formId+' span#'+fieldId+'_visible').html(Value);
			$('#'+formId+' input#'+fieldId).val(Value);
		}
	}	
}

function goToLocation(p_strLocationFields){
	$('input.radio').each(function(){
		if($(this).attr('name') == p_strLocationFields && $(this).is(":checked")){
			window.location = $(this).val();
			return false;
		}
	});
}

function checkAndAble(val, num, id){
	var input = $('#'+id);
	var label = $('#'+id+'_label');

	if(val == num){
		input.removeAttr('disabled');
		input.removeClass('disabled');
		label.removeClass('disabled');
	}
	else{
		input.attr('disabled', 'disabled');
		input.addClass('disabled');
		input.val('');
		label.addClass('disabled');		
	}
}

function checkboxAndAble(bln, id, oldVal){
	var input = $('#'+id);
	var label = $('#'+id+'_label');

	if(bln){
		input.removeAttr('disabled');
		input.removeClass('disabled');
		label.removeClass('disabled');
	}
	else{
		input.attr('disabled', 'disabled');
		input.val(oldVal);
		input.addClass('disabled');		
		label.addClass('disabled');		
	}
}


function doOnLoad(){
	var wiH = $(window).height();
	var wrH = getCompleteHeight($('#wrapper'));
	var foH = getCompleteHeight($('#footer'));
	
	if(wrH+foH < wiH)
		$('#footer').css({'margin-top' : (wiH-wrH-foH)+'px'})
}
