$(document).ready(function(){
    /* Tabs op homepage */
    $("#tabs > a:first").addClass('selected');
    $("#tabs > a:last").addClass('last');
    $("#nieuws_agenda_vacatures > div:eq(1)").addClass('selected');
	
    $("#tabs > a").click(function(){
        $("#tabs > a.selected").removeClass('selected');
        $(this).addClass('selected');
        $("#nieuws_agenda_vacatures > div.selected").removeClass('selected');
        $("#nieuws_agenda_vacatures > div:eq("+($("#tabs > a").index(this)+1)+")").addClass('selected');
    });
    
    successMessage();
    errorMessage();
    
    $('a.divlink').each(function(){
        var link = $(this).attr('href');
        var welkvenster = (($(this).attr('rel')=='external')?true:false);
        $(this).parents('div').eq(0).click(function(){
            if(welkvenster){
                window.open(link);
            }else{
                window.location = link;
            }
        }).css("cursor","pointer").find('a').removeAttr('href');
    });
	
    //Tabjes inlogvenster
    $('#tabjesinhoud .tabinhoud').hide().eq(0).show();
    $('#tabjes .tab').eq(0).addClass("active");
	
    $('#tabjes .tab').click(function(){
        $('#tabjes .tab').removeClass("active");
        $(this).addClass("active");
        var hoeveelste = $(this).parent().children(".tab").index(this);
        $('#tabjesinhoud .tabinhoud').hide().eq(hoeveelste).show();
    });
	
    //EINDE Tabjes inlogvenster
	
    // hover items
    $(".item, .artikel, .agenda_item, .vacatures_item, .nieuws_item, .first_item").hover(function(){
        $(this).addClass("hover");
    },function(){
        $(this).removeClass("hover");
    });
	
    //	nieuwsitem margin verwijderen
    $("#kort_nieuws .nieuws_item:first").css("margin-right","15px");
    $("#kort_nieuws .nieuws_item:last").css("margin-left","15px");
	
    /*Divs klikbaar maken op homepage.*/
    $("#nieuws_agenda_vacatures .artikel,#nieuws_agenda_vacatures .vacatures_item").click(function(){
        document.location = $(this).find("a:first").attr("href");
    });
    $("#nieuws_agenda_vacatures .agenda_item").click(function(){
        window.open($(this).find("a:first").attr("href"));
    })
	
    $("#vacatures .vacatures_item").click(function(){
        if($(this).find("a:first").size() > 0){
            document.location = $(this).find("a:first").attr("href");
        }
    });
	
	
    /* Input stylen */
    $("input[type=file]").filestyle({
        image: "/images/kies_bestand.gif",
        imageheight:21,
        imagewidth:100,
        width:100
    });
	
    $("a[href^='http://'],a[href^='www.'],a[rel='external']").attr('target','_blank');
	
    //$("a[href^='http://'],a[href^='www.'],a[rel='external']").not("a[href^='http://www.publish.nl'],a[href^='http://publish.nl']").attr('target','_blank');
		
    /*Deze maand in VM, p gevolgd door UL krijgt geen margin-bottom.*/
    $(".dmiv p + ul").prev("p").css("margin-bottom","0px");
	
    /*Afbeelding Gesnapt in het schap centreren*/
    $("#gihs_artikel #foto img").css("margin-top", ($("#gihs_artikel").height() - $("#gihs_artikel #foto img").height())/2+"px");
	
    footerposition();


    /** Confirmation subaccount removal **/
    $(".confirm-account-delete").click(function(e) {
        e.preventDefault();
        confirmBackground();
        var $this = $(this);
        var $popup = $("<div class='popup'>Weet u zeker dat u het subaccount wilt verwijderen?</div>");
        $popup.css({
            'z-index' : '2000',
            'background-color' : '#FEF8E4',
            'border' : '2px solid #525252',
            'display' : 'block'
        });
        $("body").append($popup);
        var $underbar = $("<div id='underbar'></div>");
        var $buttonOk = $("<input type='button' value='Ok' />");
        var $buttonAnnuleren = $("<input type='button' value='Annuleren' />");
        $underbar.append($buttonOk);
        $underbar.append($buttonAnnuleren);
        $popup.append($underbar);

        $buttonOk.click(function() {
            window.location = $this.attr("href");
        });

        $buttonAnnuleren.click(function() {
            $popup.fadeOut(function(){
                $(this).remove();
                myBgDelete();
            });
        });


        return false;
    });
});

function footerposition(){
    var ajaxhoogte = ($(window).height() - $("#top").height() - $("#whitebreak_bottom").height() - $("#footer").height() -23);
    $("#content").css("min-height",ajaxhoogte+"px");
}

function create_http_object(){
    var ActiveXTypes=["Microsoft.XMLHTTP","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP"];
    for(var i=0;i<ActiveXTypes.length;i++){
        try{
            return new ActiveXObject( ActiveXTypes[i] );
        }catch(e){}
    }
    try{
        return new XMLHttpRequest();
    }catch(e){}
    return false;
} 

var httpObject = create_http_object();

function setProgress(){
    var box = document.createElement('div');
    box.id = 'ajaxPopup';
    document.body.appendChild(box);
    document.getElementById('ajaxPopup').style.display = 'block';
    document.getElementById('ajaxPopup').innerHTML = "";
    document.getElementById('ajaxBg').style.display = 'block';
    $("#ajaxBg").animate( {
        opacity: 0.7
    }, 1000);
    $("#ajaxBg").click(function(){
        resetProgress()
    });
    $("#ajaxPopup").animate({
        height: "520px"
    }, 1000);
}
function resetProgress(){	
    $("#ajaxBg").animate({
        opacity:0
    },0);
    document.getElementById('ajaxBg').style.display='none';
    document.getElementById('ajaxPopup').innerHTML = "";
    document.getElementById('ajaxPopup').style.display='none';
    document.body.removeChild(document.getElementById('ajaxPopup'));
}

var zoekbox = "<div id='ajSluiten'><a onclick='resetProgress(); return false' href='#'>Sluit venster <img src='/images/ajSluiten.jpg' /></a></div>"
+ "<div id='ajForm'>U zoekt: <input type='text' name='q' id='ajQ' />&nbsp;"
+ "<img src='/images/ajButton.jpg' onclick='reZoeken(document.getElementById(\"ajQ\").value)' id='ajSubmit' /></div>"
+ "<div id='ajZoektop'>Zoekresultaten voor \"<span id='ajTrefwoord'></span>\"</div><div id='ajResultaten'></div>Heeft u niet gevonden wat u zocht? Neem contact op: 0297 - 242501";

function zoeken(trefwoord){
    setProgress();
    if(!httpObject){
        alert('Je browser ondersteunt deze feature niet.');
        return false;
    }
    httpObject.open("GET", "/js/ajax-zoeken.php?q=" + trefwoord + "&rand=" + Math.random(), true);
    httpObject.onreadystatechange = resultaten;
    httpObject.send('');
    document.getElementById('ajaxPopup').innerHTML = zoekbox;
    document.getElementById('ajTrefwoord').innerHTML = trefwoord;
}
function reZoeken(trefwoord){
    document.getElementById('ajResultaten').innerHTML = "";
    httpObject.open("GET", "/js/ajax-zoeken.php?q=" + trefwoord + "&rand=" + Math.random(), true);
    httpObject.onreadystatechange = resultaten;
    httpObject.send('');
}
function resultaten(){
    if(httpObject.readyState == 4){
        document.getElementById('ajResultaten').innerHTML = httpObject.responseText;
    }
}
function confirmBackground(){
    var box = document.createElement('div');
    box.id = 'ajaxBg';
    document.body.appendChild(box);
}
function myBgDelete(){
    document.body.removeChild(document.getElementById('ajaxBg'));
}
function successMessage() {
    if($('.success').length){
        confirmBackground();
        //$('.success').center();
        $('.success').css({
            'z-index' : '2000',
            'background-color' : '#FEF8E4',
            'border' : '2px solid #525252',
            'display' : 'block'
        });
        /*setTimeout(function(){
            $('.success').fadeOut("normal", myBgDelete);
        }, 2500);*/
        $('.success').append('<div id=\'underbar\'><input type="button" value="Ok" onclick="$(\'.success\').fadeOut(\'normal\', myBgDelete);" /></div>');
    }
}
function errorMessage() {
    if($('.error').length){
        confirmBackground();
        //$('.error').center();
        $('.error').css({
            'z-index' : '2000',
            'background-color' : '#FEF8E4',
            'border' : '2px solid #525252',
            'display' : 'block'
        });
        /*setTimeout(function(){
            $('.error').fadeOut("normal", myBgDelete);
        }, 3000);*/
        $('.error').append('<div id=\'underbar\'><input type="button" value="Ok" onclick="$(\'.error\').fadeOut(\'normal\', myBgDelete);" /></div>');
    }
}

$("#fotoalbum a:odd").css({
    'float':'right'
});
   
/*Doen we nu met Jquery **** window.onload = function(){
	fix_external_links();
}*/
