$(document).ready(function() {
    var val = null;

    $(".tab_content").hide(); //Hide all content


    if(location.hash != "") {
        var target = "#"+location.hash.split("#")[1]; // need semicolon at end of line
        $("ul#tabs li:has(a[href="+target+"])").addClass("active").show();
        $(location.hash).show();
    } else {
        $("ul#tabs li:first").addClass("active").show(); //Activate first tab
        $(".tab_content:first").show(); //Show first tab content
    }

    //On Click Event
    $("ul#tabs li").click(function() {
        $("ul#tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content
        var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active content
        return false;
    });

    $('.mail').click(function(e) {
        $content = $('<div />');
        $content.attr("id", "popupcontent");
        $('object').css("visibility", "hidden"); // Hier verstoppen we de flash

        // Formuliertje van de server trekken...
        $.ajax({
            url: '/mailfriends.php',
            success: function(data) {
                $content.html(data);
            }
        });

        $popup = $('<div />');
        $popup.attr("id", "popup");
        $popup.html($content);

        $div = $('<div />');
        $div.attr("id", "popupwrapper");
        $div.html($popup);

        $div.appendTo('body');
    });

    $(window).scroll(function(e) {
        $('#popupwrapper').css('top', $(window).scrollTop());
    });

    $('#submit').live('click', function(e) {
        // Eventjes wat data versturen en valideren

        $('#errors').addClass('load');

        $.ajax({
            url: '/mailfriends.php?link=' + window.location,
            type: "POST",
            data: "submit=true&txtmename=" + $('#txtmename').val() +
            "&txtmeemail=" + $('#txtmeemail').val() +
            "&txttoname=" + $('#txttoname').val() +
            "&txttoemail=" + $('#txttoemail').val() +
            "&txtomschrijving=" + $('#txtomschrijving').val() +
            "&txtonderwerp=" + $('#txtonderwerp').val(),
            success: function(data) {
                $('#errors').removeClass('load');
                if(data != "") {
                    // We krijgen fouten terug...
                    $("#errors").html(data);
                } else {
                    $('#popupwrapper').remove();
                    $('object').css("visibility", "inherit");
                //$('#contents').html('Uw bericht is met succes verzonden. Klik <a href="#" class="close">hier</a> om dit venster te sluiten.');
                }
            }
        });
    });

    // Beetje hele lompe manier om een window te sluiten
    // Kan echter niet anders, want niet alle browsers snappen de z-index. Beetje jammer
    $('#popupwrapper').live('click', function(e) {
        var p = $('#popup').offset();

        if($(p).length > 0) {
            if(e.clientX < p.left || e.clientX > (p.left + $('#popup').width())) {
                $(this).remove();
                $('object').css("visibility", "inherit");
            } else if(e.clientY < p.top || e.clientY > (p.top + $('#popup').height())) {
                $(this).remove();
                $('object').css("visibility", "inherit");
            }
        }
    });

    $('.close').live('click', function(e){
        $('#popupwrapper').remove();
        $('object').css("visibility", "inherit");
    });

    $('.twitter').click(function(e) {
        popUp("http://twitter.com/share?url="+window.location.href, 500, 500);
    });

    $('.facebook').click(function(e){
        popUp("http://www.facebook.com/share.php?u="+window.location.href, 600, 600);
    });

    $('.linkedin').click(function(e){
        popUp("http://www.linkedin.com/shareArticle?mini=true&url=" + window.location.href + "&title=BoomNelissen&summary=&source=", 600, 600);
    });

    $('.print').click(function(e){
        window.print();
    });

    $('#invul_veld').click(function(e) {
        if(val == null || $(this).val() == val) {
            val = $(this).val();
            $(this).val("");
            $(this).removeClass("default");
        }
    });

    $('#invul_veld').blur(function(e) {
        if($(this).val() == "") {
            $(this).val(val);
            $(this).addClass("default");
        }
    });
	
	$('#zoekveld').click(function(e){
		$('#zoekveld').val("");
	});
});

var popUp = function(URL,width,height) {
    day = new Date();
    id = day.getTime();

    var winLeft = (screen.width - width) / 2;
    var winUp = (screen.height - height) / 2;

    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width='+width+',height='+height+',left='+winLeft+',top='+winUp);");
}

