$(document).ready(function() {
	//$(window).trigger("resize");	
	
	$(".hide").hide();
    
    if($('img.tooltip').length > 0) {
        $('img.tooltip').tooltip({
                track: true,
                delay: 0,
                showURL: false, 
                bodyHandler: function() {
                    return $($(this).prev('.tooltipTxt')).html();
            }
        });    
    }
     
    if($(".row-validate").length > 0) {
        SetBlurValidationField();
    } 
});
 
function SetColor(Color){
  $('h1').css('color', Color);
  $('h2').css('color', Color);
  $('h3').css('color', Color);
  $('h4').css('color', Color);
  $('h5').css('color', Color);
  $('h6').css('color', Color);
  $('#navigation li.current a').css('background', Color + ' none'); 
 }
 
function SetBlurValidationField() {
    $(".row-validate").each(function(){
        var Container = $(this);

        $(this).find(".input-subsidy-form").blur(function(){
            var Val = $(this).val();
            var Defaultvalue = "0";

            // replace comma with dot
            Val = Val.replace(/,/gi, ".");

            // search for dot
            var DotPos = Val.indexOf(".");

            if(DotPos != -1) {
                if(!isNaN(Val)) {
                    Val = Math.round(Val*100) / 100;
                    Val = Val.toFixed(2);
                }
            } else {
                if(Val.length == 0) {
                    Val = "0";
                } else {
                    Val = Val + ".00";
                }
            }

            // check for not nummeric numbers
            if(isNaN(Val)) {
                Val = Defaultvalue;
            }

            $(this).val(Val);

            if($(Container).find("input.styled").attr("checked")) {
                validateSubsidyForm(Container, false);
            }
        });
    });
} 

function validateSubsidyForm(ContainerSelector, bSubmit)
{
    var bItemsChecked = false;
    var bFieldValid = true;
    var bErrorsFound = false;

    var Validation = new RegExp(/^([0-9]*\.[0-9]+|[0-9]*)$/);

    $(ContainerSelector).each(function(){

        if($(this).find("input.styled").attr("checked")) {
            bFieldValid = true;

            $(this).find(".input-subsidy-form.validate").each(function(){
                if(!Validation.test($(this).val())) {
                    bFieldValid = false;
                    bErrorsFound = true;
                } else {
                    if($(this).val() < 1) {
                        bFieldValid = false;
                        bErrorsFound = true;
                    }
                }
            });

            if(bFieldValid) {
                $(this).find("span.checkbox").css("background-position","0px -68px");
            } else {
                $(this).find("span.checkbox").css("background-position","0px -34px");
            }

            bItemsChecked = true;
         } else {
            //bSubmit = false;
         }
    });

    if(bSubmit && !bErrorsFound && bItemsChecked) {
        return true;
    } else {
        if(bSubmit && bErrorsFound) {
            alert("Het formulier is niet compleet. Controleer de rode vinkjes");
        }
    }

   return false;
}


/**
*    vCenterContent              - center content vertical inside container
*    @param: 'Selector'          - Selector of content that has to be centered
*    @param: 'ContainerHeight'   - Height of selector container
*/
function vCenterContent(Selector, ContainerHeight)
{
    var ContainerHeight = ContainerHeight;
    var oElement = $(Selector);

    oElement.each(function(){
        var PaddingVal = (ContainerHeight - $(this).height()) / 2 + "px 0px";
        $(this).attr("style", "padding: " +  PaddingVal);     
    });   
}

/**
*    moveFirstItemToLastPosition - eg. put first li to the last position
*    @param: 'oElement'          - Current element
*/
function moveFirstItemToLastPosition(oElement) {
    oElement.parent().append(oElement); 
}
 
/**
*    SetActiveSubSubNavigation  - Function for setting the subsubnavigation active based on the url (weggebruikers, hot issues)
*/ 
function SetActiveSubSubNavigation() {
    if($("#subsubnavigation li").length > 0) 
    {
        var documentUrl = document.URL;
        var aUrlParts   = documentUrl.split("/");
        
        if(aUrlParts[aUrlParts.length-1] == "") {
            aUrlParts.pop();    
        }
        
        var CurrentItem = aUrlParts[aUrlParts.length-1];
        var CurrentItemLength = CurrentItem.length;
        
        $("#subsubnavigation li a").each(function() {
            var Str = $(this).attr("href").substr(-CurrentItemLength);
            
            if(Str == CurrentItem) {
                $(this).parent().addClass("current");    
            }
        });
    }
}
 
/**
* showSubsidyForm   - slideIn subsidy form, hides the others
*	@param: 'Element' - the current element
*	@param: 'DivId'   - this DivId slides in
*/
function showSubsidyForm(ElementId, DivId) {
	var SlideDiv = $("#" + DivId);
	var ElementDiv = $("#" + ElementId);
	var AnimatingClass = "animating";
	
	if(!ElementDiv.hasClass(AnimatingClass)) {
		ElementDiv.addClass(AnimatingClass);
		SlideDiv.slideToggle(300, function(){ ElementDiv.removeClass(AnimatingClass); });
	}
	
	/*
	if($("#" + ElementId + ":checked").length > 0) {
		SlideDiv.slideDown(300, function(){ $("input[type=checkbox]").removeAttr("disabled"); });	
	} else {
		SlideDiv.slideUp(300, function(){ $("input[type=checkbox]").removeAttr("disabled"); });		
	}
	*/
	
	
	/** SlideUp elements that are not current	
	  * Disabled
	  */
	//if($("input[type=checkbox]:checked").length > 1) { 
	//	$("input[type=checkbox]:checked:not(input[id='" + ElementId + "'])").trigger("click");
	//} 
	
	/* checkbox functionality disabled
	$("input[type=checkbox]").attr("disabled", "disabled");
	
	if($("#" + ElementId + ":checked").length > 0) {
		SlideDiv.slideDown(300, function(){ $("input[type=checkbox]").removeAttr("disabled"); });	
	} else {
		SlideDiv.slideUp(300, function(){ $("input[type=checkbox]").removeAttr("disabled"); });		
	}
	*/
}

/**
* SetRowColors, function for setting the background color of even and uneven rows in form
*/

function SetRowColors(ContainerElement) {
    var EvenClassName = "even";
    var OddClassName = "uneven";
    
    $(".rowcolors").each(function(){ 
        $(this).find(".row:even").addClass(EvenClassName);      
        $(this).find(".row:odd").addClass(OddClassName);     
    });       
}

 
function showBlock(){     
  var FormContainer = $('.item-form');          
  var FormContainerDiv = $('#form_container');          
  var ArrowContainer = $('.name .arrow');          
  if(FormContainer.css('height') == '0px'){
    FormContainer.css({'border': '1px solid #bfbfbf'});
    var Height = FormContainerDiv[0].scrollHeight;
    FormContainer.animate({
      height: Height
    }, function(){
      FormContainerDiv.css('display','none');
      FormContainerDiv.css('visibility','visible');
      FormContainerDiv.fadeIn("slow");
      ArrowContainer.html('<img src="/pics/arrow-up.gif" alt="" />');
      return true;
    });
  } else {
    var Height = FormContainerDiv[0].scrollHeight;
    FormContainerDiv.fadeOut("slow",function(){
      FormContainerDiv.css('display','block');
      FormContainerDiv.css('visibility','hidden');
      FormContainer.animate({
        height: '0px'
      }, function(){
        ArrowContainer.html('<img src="/pics/arrow-down.gif" alt="" />');
        FormContainer.css({'border': '0px solid #bfbfbf'});
        return true;
      });
      
    });
  }
}

