﻿$('document').ready(function() {
    init_autovalue();
    init_validation();
    check();  
    init_scroll();
    init_tooltip();
    init_quotes();
});

function pageLoad() {
    init_bullets();
}



function init_validation() {
    /*** validatie ***/
    $('.validatie_pointer').each(function() {
            active = ($(this).css('display'));
            if (active == "inline") { $(this).parents('.row').find('.textbox').addClass('error'); }
        });    
        $('input, textarea').blur(function() {
            $('.validatie-pointer:hidden').parents('.row').find('.textbox').removeClass('error');
            $('.validatie-pointer:visible').parents('.row').find('.textbox').addClass('error');
            $(this).removeClass('has-focus');
        });
        $('input, textarea').focus(function() {
            $('.validatie-pointer:hidden').parents('.row').find('.textbox').removeClass('error');
            $('.validatie-pointer:visible').parents('.row').find('.textbox').addClass('error');
            $(this).addClass('has-focus');
        });
        $('.do-validate').click(function() {
            $('.validatie-pointer:hidden').parents('.row').find('.textbox').removeClass('error');
            $('.validatie-pointer:visible').parents('.row').find('.textbox').addClass('error');
            setTimeout(check, 100);
        });
}

function check() {
    $('.validatie-pointer:hidden').parents('p.row').find('.textbox').removeClass('error');
    $('.validatie-pointer:visible').parents('p.row').find('.textbox').addClass('error');
    if ($('.textbox').hasClass('error')) { 
            $('.textbox.error').parents('.form-container').find('.errorlist').show();             
            centreer(); //in een popup? centreer dan opnieuw na het laten zien van de errorlist
            }
}


function init_bullets() {
   //verwijder alle bullets eerst ivm herinitialisatie van bullets 
   $('.required-bullet').remove();

   //zet verplichte bullets aan
    $('[rel="required"]').each(function() {
        //breedte van de textbox
        width = $(this).css('width');
        //linkpadding van de .row
        padding = $(this).css('left');
        //de 'px' eraf halen
        width = width.replace("px", "");     
        padding = padding.replace("px", "");
        //positie van de 'required' bullet
        afstand = parseInt(width) + parseInt(padding) + 12;
        //de required bullet goed neerzetten
        $(this).after('<div class="required-bullet" style="left:' + afstand + 'px">&nbsp;</div>');
    });
}

function init_autovalue() {
    $('textarea[autovalue],input[type=text][autovalue]').each(function() {
        var autovalue = $(this).attr('autovalue');
         $(this).val(autovalue);
        $(this).focus(function() {
            if ($(this).val() == autovalue) {
                $(this).val('');
            }
        });
        $(this).blur(function() {
            if ($(this).val() == '') {
                var autovalue = $(this).attr('autovalue');
                $(this).val(autovalue);
            }
        });
    });
}

function init_cufon() {
    Cufon.replace('.mixage', { fontFamily: 'ITC Mixage' });
    Cufon.replace('.mixage-book', { fontFamily: 'ITC Mixage Book' });
}


function init_scroll() {
    $('.scroll-up').click(function() {
        $('html, body').animate({scrollTop:0}, 'slow');
    });
}



/******* popups ******/
function startOverlay_div(element) {
        //start lightbox
        $('form:first').before("<div id=\"overlay\">&nbsp;</div>");
        $('#overlay').css('opacity', '0.4');
        $('#overlay').fadeIn(500, overlay_ingeladen(element));
}

function overlay_ingeladen(element) {
       $('#overlay').click(close_overlay);
       $('#'+element+' .close').click(function() {close_overlay();});
       $('#'+ element).center();
       $('#'+ element).css({'z-index':'9500'})
       $('#'+ element).show();
       init_bullets();
}

function close_overlay() {  
    $('.popup').hide(); 
    overlay_gesloten(); 
}

function overlay_gesloten() {
        $('#overlay').fadeOut(500);
        setTimeout("$('#overlay').remove()", 500);
}

$(window).resize(function() {
        centreer();
});

jQuery.fn.center = function() {
        this.css("position", "absolute");
        this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
        this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
        return this;
}

function centreer() {
        $('.popup').center();
}
/******** einde popups *****/


function init_tooltip() {

   
    // List of all paramaters and their default values:  
        
    $('a.tooltip').aToolTip({  
        clickIt: true,                     // set to true for click activated tooltip  
        closeTipBtn: 'aToolTipCloseBtn',    // you can set custom class name for close button on tooltip  
        fixed: true,                       // Set true to activate fixed position  
        inSpeed: 0,                       // Speed tooltip fades in  
        outSpeed: 0,                      // Speed tooltip fades out  
        tipContent: '',                     // Pass in content or it will use objects 'title' attribute  
        toolTipClass: 'aToolTip',           // Set custom class for tooltip  
        xOffset: -100,                         // x Position  
        yOffset: 5                          // y position  
    });  
}





(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);


function init_quotes() {
    //0, 1, 2
    var welkequote = 'quote_' + Math.floor(Math.random()*3);
    //alert(welkequote);
    $('.'+welkequote).show();
}
