var hetdomein = "http://" + window.location.toString().split("//")[1].split("/")[0] + "/";

$(document).ready(function() {
    init_tabs();
    init_validation();
    init_containerHeight();
    init_CaseDpImages();
    init_GoogleMap();
    init_cufon();


});
 
function pageLoad() {
    check();
    init_roundedcorners();
    init_waterMarkText();
}

function init_GoogleMap() {
    if (($.browser.msie) && ($.browser.version == '6.0')) {
        $('#map_canvas').remove();
        $('#mapblock').addClass('ie6');
    }
}

function init_cufon() {
    Cufon.replace('.cufon', {
        fontFamily: 'MiloOT',
        hover: true
    });

    Cufon.replace('.cufon-medium', {
        fontFamily: 'MiloOT-Medi',
        hover: true
    });

    Cufon.replace('.cufon-bold', {
        fontFamily: 'MiloOT-Bold',
        hover: true
    });

    Cufon.replace('.cufon-xbold', {
        fontFamily: 'MiloOT-Xbold',
        hover: true
    });

    Cufon.replace('.cufon-sauna', {
        fontFamily: 'Sauna',
        hover: true
    });
};

function init_homeSlider() {
    $('.homeSlider')
    .cycle({ 
        fx:     'scrollHorz', 
        speed:  '800', 
        timeout: 5000, 
        pager:  '.pager',
        next:   '.next', 
        prev:   '.prev',
       
        before: onbefore,
        after: onafter
    }); 
    function onbefore() {
         $('.slide:hidden').find('.headerInfo dl').hide();
    }
    function onafter() {
          $('.slide:visible').find('.headerInfo dl').fadeIn('800');
         
    }
}

function init_waterMarkText() {
    //functie door Tim voor watermark text
    $('[rel="watermark"]').each(function() {
        //maak attribuut aan met de standaard waarde
        $(this).attr('watermark', $(this).val());
        //onfocus maak veld leeg
        $(this).focus(function(){
            //als huidige overeenkomt met watermerk leegmaken anders niet.
            var watermerk = $(this).attr('watermark');
            var current = $(this).val();
            if (current == watermerk) {  $(this).val(''); }
        });
        //onblur eventueel veld weer vullen met waarde?
        $(this).blur(function(){
            var watermerk = $(this).attr('watermark');
            var current = $(this).val();
            //veld leeggelaten weer invullen met waarde
            if (current == "") {  $(this).val(watermerk);  }
        });
    });
}

function init_tabs() {
    $('#tabs').each(function() {
        var active = $(this).find('ul.tabs li.active').attr('title');
        $(this).find('.tab').hide();
        $(this).find('.tab#' + active).show();

        $(this).find('ul.tabs li').click(function() {
            $('.errorlist').hide();
            $('ul.tabs li a').blur();
            $(this).parents('.tabs').find('li.active').removeClass('active');
            $(this).addClass('active');
            active = $(this).attr('title');
            $(this).parents('#tabs').find('.tab').hide();
            $('#' + active).fadeIn();
            //$(this).parents('#tabs').find('#' + active).show();
            $(this).parents('#tabs').find('#' + active + ' .errorlist').hide();
            $('html').addClass('cufon-ready');
            $('img').lazyload({
                placeholder: "/assets/images/loading.gif",
                effect: "fadeIn"
            });

            if (active == 'tab1') {
                $(window).resize(function() {
                    //$("#tab2").css({'display':'block'});
                    $("#map_canvas").css({'width':'630px', 'height':'400px'});
                    initialize();
                });
            }
            return false;
        });

        $('.moreCases').click(function() {
            $('#tab1').hide();
            $('#tab2').show();
            $('.tab1').removeClass('active');
            $('.tab2').addClass('active');
        });

        if (window.location.hash) {
            var hash = window.location.hash.substring(1);
            if (hash == 'tab2') {
                $('.tab1').removeClass('active');
                $('.tab2').addClass('active');
                $('.tab3').removeClass('active');

                $('#tab1').hide();
                $('#tab2').show();
                $('#tab3').hide();
            }
        }

        $('.contactlink').click(function() {
            $('#tab1').hide();
            $('#tab2').show();
            $('#tab3').hide();

            $('.tab1').removeClass('active');
            $('.tab2').addClass('active');
            $('.tab3').removeClass('active');
        });
    });
}

function init_HideMainNav() {
    $('#ActionPage').parents('body').addClass('actionpage');
}

function init_roundedcorners() {
    $('header').addClass('roundedcorners yellow');
    $('.roundedcorners .lb, .roundedcorners .rb, .roundedcorners .lo, .roundedcorners .ro').remove();    
    $('.roundedcorners').append('<span class="lb">&nbsp;</span><span class="rb">&nbsp;</span><span class="lo">&nbsp;</span><span class="ro">&nbsp;</span>');
};


function init_validation() {
    $('.validatie_pointer').each(function() {
        active = ($(this).css('display'));
        $(this).parents('div.data').find('.textbox, label').addClass('error');
    });

    $('input, textarea').blur(function() {
        $(this).removeClass('has-focus');
    });
    $('input, textarea').focus(function() {
        $(this).addClass('has-focus');
    });

    $('.do-validate').click(function() {
        $('.validatie-pointer:hidden').parents('div.data').find('.textbox').removeClass('error');
        $('.validatie-pointer:visible').parents('div.data').find('.textbox').addClass('error');

        $('#TellAFriend .form, #TellAFriend .buttonOrange').hide();
        $('#TellAFriend .overlay-content').append('<div class="loader"><img src="/assets/images/loading.gif" /><span></span></div>');

        setTimeout(check, 100);
    });
}

function check() {
    $('.validatie-pointer:hidden').parents('div.data').find('.textbox, label').removeClass('error');
    $('.validatie-pointer:visible').parents('div.data').find('.textbox, label').addClass('error');
    if ($('.textbox').hasClass('error')) {
        $('.validatie-pointer:visible').parents('fieldset').find('.errorlist').show();
    }
}

function startOverlay_div(element) {
    $('form').after("<div id=\"overlay\">&nbsp;</div>");
    $('#overlay').css('opacity', '0.9');
    $('#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': '4000' })
    $('#' + element).show();
}

function close_overlay() { $('.popup, .popup-home').hide(); $('#overlay-content').hide(); $('#overlay-content img#zoom-image').remove(); $('#zoom-image-container').removeClass('flashcontent'); overlay_gesloten(); }

function overlay_gesloten() {
    $('#overlay').fadeOut(500);
    setTimeout("$('#overlay').remove()", 500);
}

$(window).resize(function() {
   $('.popup').center();
});

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 init_containerHeight() {
    var containerHeight = $(window).height();
    var contentHeight = containerHeight - 347;
    var tabsHeight = containerHeight - 502;

    $('#conditions').css('min-height', contentHeight);

    if (contentHeight <= '448') {
        $('#services').css('min-height', '448px');
    } else {
        $('#services').css('min-height', contentHeight);
    }
    $('#blog.blogs').css('min-height', contentHeight);
    $('#vacatures').css('min-height', contentHeight);
    $('#vacatures article').css('min-height', contentHeight - 64);
    $('#tabs #tab1, #tabs #tab2, #tabs #tab3').css('min-height', tabsHeight);
    
    if ($.browser.msie) {
        if ($.browser.version == '6.0') {
            $('#services').css('height', contentHeight);
            $('#blog.blogs').css('height', contentHeight);
            $('#vacatures').css('height', contentHeight);
            $('#vacatures article').css('height', contentHeight - 64);
            $('#tabs #tab1, #tabs #tab2, #tabs #tab3').css('height', tabsHeight);
        }
    }
}

function init_CaseDpImages() {
    for (i = 0; i < $("#images .slides img").length; i++) {
        $('#images .slides li').hide();
        $('#images .nav').append('<a href="javascript:void(0);" class=image'+i+'>&nbsp;</a>');
    };

    $('#images .slides ul').find(">:first-child").css('display', 'block').addClass('active');
    $('#images .nav').find(">:first-child").addClass('active');

    $('#images .nav a').click(function() {
        var clicked = $(this).attr('class');
        if (!$(this).hasClass('active')) {
            $('#images .slides li').removeClass('active').css('display', 'none');
            $('#images .slides li a img.' + clicked + '').parents('li').addClass('active').css('display', 'block');

            $('#images .nav a.active').removeClass('active');
            $(this).addClass('active');
        }
        $(this).blur();
    });
}

function init_tooltip() {  
    $('.mouse-tooltip').mousetip({
            cssClass: 'mousetip',
            snap: true,
            eventOpen: 'mouseenter',
            eventClose: 'mouseleave'
    });
    
    $('.mouse-tooltip').click(function() {
        $('.mousetip').remove();
    });

}
