//$.noConflict();
var imagenummer = 0;
var xmldoc = '';
var aantalimages = 0;
var ie6 = false;
$(document).ready(function($) {
    //resizer
    //Maak image array
    var images = new Array();
    var totalImages = 0;
    var actualImageIndex = 0;

    //load background and read xml
    $.ajax({
        type: "GET",
        url: "/xml/backgroundimages.xml",
        dataType: "xml",
        success: function(xml) {
            xmldoc = xml;
            $(xml).find('image').each(function() {
                if ($(this).attr('src') && $(this).attr('src') != "") {
                    images.push($(this).attr('src'));
                }
            });
            totalImages = images.length;
            actualImageIndex = Math.floor(Math.random() * totalImages);
            $.backstretch(images[actualImageIndex], {speed: 150});

        }
    });
    function leftRightClick() {
        if ($(this).hasClass('imageright')) {
            actualImageIndex++;
            if (actualImageIndex >= totalImages)actualImageIndex = 0;
        } else {
            actualImageIndex--;
            if (actualImageIndex < 0)actualImageIndex = (totalImages - 1);
        }
        $.backstretch(images[actualImageIndex]);
    }

    $(".imageright").click(leftRightClick);
    $(".imageleft").click(leftRightClick);
    /*    $(window).bind("resize", function(){
     $("#body-background").ezBgResize();
     });*/

    /*
     * external links
     */
    $('a[rel="external"]').click(function() {
        window.open($(this).attr('href'));
        return false;
    });

    $('a[href$="pdf"]').click(function() {
        window.open($(this).attr('href'));
        return false;
    });

    prevobj = '';

    $('.extracontent > h3').click(function() {
        //alert($(this).parent().children('p').html());
        if (prevobj != '') {
            $(prevobj).parent().children('p').slideUp();
        }
        $(this).parent().children('p').slideDown('slow');
        prevobj = $(this);
        return false;
    });

    //draggable news
    $(function() {
        if($("#rightul").length>0){
            $("#rightul").draggable({handle: $(".topbar")});
        }
    });
    $(".news_open").click(function() {
        $('.nieuwsholder').show('slide', {direction:'up'})
        $(this).hide();
        $(".news_close").show();
    });
    $(".news_close").click(function() {
        $('.nieuwsholder').hide('slide', {direction:'up'})
        $(this).hide();
        $(".news_open").show();
    });

    //fade toggle function
    $.fn.fadeToggle = function(speed, easing, callback) {
        return this.animate({opacity: 'toggle'}, speed, easing, callback);
    };

    //zet bij elke lightbox een image neer (vergrootglas)
    $("a[rel=lightbox]").each(function() {
        $(this).append('<div class="vergrootglas"/>');

    });


    $(function() {
        // Use this example, or...
        $("a[rel=lightbox]").fancybox({
            'type' : 'image',
            'transitionIn' : 'none',
            'transitionOut' : 'none',
            'titlePosition' : 'over',
            'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
                return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
            }
        });
    });
    //set selected classes
    if ($(curSelected)) {
        if(curSelected!=""){
            $(".subMenuItem #" + curSelected).addClass('selected');
            $(".last #l-" + curSelected).addClass('selectedFooter');
        }
    }

    /* resize content
     $('#content').resizable({
     handlers: 'se,s,e',
     minHeight:0,
     minWidth:0,
     start : function(event, ui) {
     $(this).css({'min-height':0+'px'})


     }

     }
     );
     end content resize */

    //slideup/down content
    //get contentheight

    var contentH = $('#content').height();
    $('.contentcontrols').toggle(
            function() {
                $('#content').css({'min-height':0 + 'px'})
                $('#content').stop().animate({
                    height: '0px'
                }, 1500, function() {
                    // Animation complete.
                    $('.contentcontrols img').attr('src', '/images/content_bigger.gif')
                });


            },
            function() {
                //$('#content').css({'min-height':640+'px'})
                $('#content').stop().animate({
                    height: contentH + 'px'
                }, 1000, function() {
                    // Animation complete.
                    $('.contentcontrols img').attr('src', '/images/content_smaller.gif')
                });


            }
            );

    //end slideup/down


    //nieuwe sorteringfunctie:

    $(".sorting").click(function () {
        $(this).toggleClass('selected');
        if ($(this).hasClass('selected')) {
            $(this).attr('value', '1');
        } else {
            $(this).attr('value', '0');
        }
        $('.prjContainer').hide();
        var active = false;
        $.each($('.sorting'), function() {
            var sortmethod = $(this).attr('sortmethod');
            var sortvalue = $(this).attr('value');
            if (sortvalue == '1')active = true;
            //console.log('sortering '+sortmethod+' is nu :'+sortvalue)
            $.each($('.prjContainer'), function() {
                //$(this).hide()
                if (sortmethod == 'shops' || sortmethod == 'offices') {
                    if (sortvalue == '1' && $(this).attr(sortmethod) == 'true') {
                        $(this).show()
                    }
                }
                if (sortmethod == 'gerealiseerd') {
                    if (sortvalue == '1' && $(this).attr('status') == '0') {
                        $(this).show()
                    }
                }
                if (sortmethod == 'actueel') {
                    if (sortvalue == '1' && $(this).attr('status') == '1') {
                        $(this).show()
                    }
                }
            });
        });
        if (!active) $('.prjContainer').show();
    });

    //end sorting

    $(".uitlegBovenButtonOpen").toggle(
            function () {
                $(".uitlegHolderBoven").fadeIn(500);
            },
            function () {
                $(".uitlegHolderBoven").css('display', 'none')
                //$(".uitlegHolderBoven").fadeOut("slow");
                $(".uitlegHolderOnder").delay(1000).css('display', 'none')
            }
            );
    //end document.ready
});


/* Functie voor paging */
function pagingObj() {
    if ($("#content").length > 0) {
        var contentDiv = $("#content");
        var pagesCount = 0;
        var pages = $("#content ul");
        for (i = 0; i < pages.length; i++) {
            if (pages[i].className == 'page') pagesCount++;
        }
        if (pagesCount > 0) goPage(0, (pagesCount));
    }

}

var prev_page = '';

function goPage(s, p) {

    var pObj = '';

    for (i = 0; i < p; i++) {
        if (i > 0) pObj += ' | ';
        // link all pages except current
        pObj += (s == i) ? (i + 1) : '<a href="javascript:goPage(' + i + ', ' + p + ')">' + (i + 1) + '</a>';
        if (i > 0) document.getElementById('paging').innerHTML = pObj;
    }


    s = 'page_' + s;
    if (prev_page != '') {
        document.getElementById(prev_page).style.display = 'none';
    }
    document.getElementById(s).style.display = 'block'

    prev_page = s;
}

/* Deze functies worden gebruikt omdat je geen target=blank meer mag gebruiken */
try {
    document.addEventListener('click', handleClick, false);
} catch(e) {
    if (document.attachEvent) {
        document.attachEvent('onclick', handleClick);
    } else {
        document.onclick = handleClick;
    }
}

function handleClick(e) {
    var event = e || window.event;
    if (event.ctrlKey || event.shiftKey || event.altKey) return true;
    if (event.which && event.which != 1) return true;

    var target = event.target || event.srcElement;
    while (target && !/^a$/i.test(target.nodeName)) {
        target = target.parentNode;
    }

    if (!target || !target.getAttribute('rel')) return true;

    var rel = target.getAttribute('rel');
    var href = target.getAttribute('href');
    switch (rel) {
        case 'disclaimer':
            window.open(href, '', 'width=300,height=250');
            break;
        case 'informatie':
            window.open(href, '', 'width=560,height=450,scrollbars=yes');
            break;
        case 'external':
            window.open(href);
            break;
        case 'community':
            window.open(href, '', 'width=360,height=340,scrollbars=no');
            break;
        case 'routeplanner':
            window.open(href, '', 'width=570,height=520,scrollbars=no');
            break;
        case 'seniorvalley':
            window.open(href, 'seniorvalley', 'width=618,height=444,scrollbars=no');
            break;
        default:
            return true;
    }

    try {
        event.preventDefault();
    } catch(e) {
    }
    return false;
}

function init() {
    pagingObj();
}

this.onload = init;

// opening en closing KvK gegevens adressen lijst
function openCloseKvk(nummer) {
    if (nummer && nummer != "" && document.getElementById("kvk_" + nummer)) {
        if (document.getElementById("kvk_" + nummer).style.display == "block") {
            document.getElementById("kvk_" + nummer).style.display = "none";
        } else {
            document.getElementById("kvk_" + nummer).style.display = "block";
        }
    }
}


/***************************************************************************
 * print_r functie - handig
 **************************************************************************/
function print_r(theObj) {
    if (theObj.constructor == Array || theObj.constructor == Object) {
        document.write("<ul>")
        for (var p in theObj) {
            if (theObj[p].constructor == Array || theObj[p].constructor == Object) {
                document.write("<li>[" + p + "] => " + typeof(theObj) + "</li>");
                document.write("<ul>")
                print_r(theObj[p]);
                document.write("</ul>")
            } else {
                document.write("<li>[" + p + "] => " + theObj[p] + "</li>");
            }
        }
        document.write("</ul>")
    }
}

/***************************************************************************
 * sorting function
 **************************************************************************/


(function($) {
    $.fn.sort = function(options) {
        var defaults = {
            el:null,
            sortmethod: 'status',
            sortvaluestatic: '1',
            showing: 1
        };
        var options = $.extend(defaults, options);
        //console.log('sortmethod is '+options.sortmethod+' en de sortvaluestatic is '+options.sortvaluestatic+' en showing is '+options.showing)
        return this.each(function() {
            $.each(options.el, function() {
                // console.log('het project '+$(this).attr('id')+' met waarde: '+$(this).attr(options.sortmethod)+' is niet gelijk aan de sortvaluestatic van '+options.sortvaluestatic)
                if ($(this).attr(options.sortmethod) == options.sortvaluestatic) {

                    options.showing == 1 ? $(this).show() : $(this).hide();
                    // nu alleen kijken bij shops of iets voorkomt bij alle 2
                    if ((options.sortmethod == 'shops' || options.sortmethod == 'offices') && ($(this).attr('shops') == 'true' && $(this).attr('offices') == 'true')) {
                        $(this).show()
                        console.log($(this).attr('id') + 'is een kantoor en een winkel')
                    }
                }
            });

        });
    };
})($);


