$(document).ready(function() {

    if ($("#wedstrijdschema").length > 0) {

        /**
        * create date
        */

        var currentTime = new Date();
        var day = (currentTime.getDate().toString().length == 1) ? '0' + currentTime.getDate().toString() : currentTime.getDate().toString();
        var month = ((currentTime.getMonth() + 1).toString().length == 1) ? '0' + (currentTime.getMonth() + 1).toString() : (currentTime.getMonth() + 1).toString();
        var year = currentTime.getFullYear();
        var today = parseInt(year + month + day);

        /**
        * hide events
        */

        var tmp = true;
        $("#wedstrijdschema tr").each(function(i) {
            var originalId = $(this).attr('id');
            thisDate = originalId.substr(1, 8);
            if (thisDate != '') {
                // .. in the past
                if (thisDate < today)
                    $("#" + originalId).hide();

                // .. and based on match type

                /** 
                *
                *     to hide matches of a certain type, copy+paste this code in correct case
                *
                *     $("#wedstrijdschema tr:eq(" + i + ")").hide();
                *
                */

                switch ($("#wedstrijdschema tr:eq(" + i + ") td:last").html()) {
                    case 'Champions League':
                        break;
                    case 'Eredivisie':
                        break;
                    case 'KNVB beker':
                        break;
                    case 'Interland':
                        break;
                    case 'Johan Cruijff Schaal':
                        break;
                }

            }
        });
    }

    /*
    * create navigation for photoslider (homepage)
    */

    if ($("#photoslider .photo div").length > 1) {

        var count = $("#photoslider .photo div").length;

        var itemHtml = "";
        for (var i = 0; i < count; i++) {
            if (i)
                itemHtml += "	<img src=\"images/photoslider_photoInact.gif\" class=\"item\" alt=\"" + i + "\" />\n";
            else
                itemHtml += "	<img src=\"images/photoslider_photoAct.gif\" class=\"item\" alt=\"" + i + "\" />\n";
        }

        var html = "<div id=\"navigation\" class=\"floatright\">\n" +
			"	<div class=\"divider\"></div>\n" +
			"	<a id=\"photoslider_left\" href=\"#\"></a>\n" +
			itemHtml +
			"	<a id=\"photoslider_right\" href=\"#\"></a>\n" +
			"</div>\n";

        $("#photoslider").attr('title', '1'); 		// use title attribute to 'store' current state
        $("#photoslider .photo div").removeClass('hidden').hide();
        $("#photoslider .photo div:eq(0)").show();
        $("#photoslider .footer").prepend(html);

        active = true;

        /*
        * show previous picture
        */

        $("#photoslider_left").bind("click", function() {
            active = stopTimer(active);
            var current = parseInt($("#photoslider").attr('title'));
            var previous = current - 1;
            showItem(current, previous, count);
            active = resumeTimer(active);
        });

        /*
        * show next picture
        */

        $("#photoslider_right").bind("click", function() {
            active = stopTimer(active);
            var current = parseInt($("#photoslider").attr('title'));
            var next = current + 1;
            showItem(current, next, count);
            active = resumeTimer(active);
        });

        /*
        * animate
        */

        $("#photoslider").everyTime(3000, 'controlled', function(i) {
            if (active) {
                var current = parseInt($("#photoslider").attr('title'));
                var next = current + 1;
                showItem(current, next, count);
            }
        });

    }

    /*
    * show corresponding picture
    */

    function showItem(current, nr, count) {

        // alert('current: ' + current + ' | nr: ' + nr + ' | count: ' + count);

        if (nr > count)
            nr = 1;
        else if (nr == 0)
            nr = count;

        // set navigation items inactive + hide images
        $("#photoslider #navigation img").attr('src', 'images/photoslider_photoInact.gif');

        if (!($.browser.msie && $.browser.version < 7)) {
            $("#photoslider .photo div").stop();
            $("#photoslider .photo div").fadeTo(0, 1).hide().css('z-index', '1');
        }

        $("#photoslider .photo div:eq(" + (current - 1) + ")").show().css('z-index', '2');                      // show current on z-index 2
        $("#photoslider .photo div:eq(" + (nr - 1) + ")").show();                                               // show new below (set as z-index 1)
        $("#photoslider #navigation img:eq(" + (nr - 1) + ")").attr('src', 'images/photoslider_photoAct.gif');

        if ($.browser.msie && $.browser.version < 7) {
            $("#photoslider .photo div:eq(" + (current - 1) + ")").hide().css('z-index', '1');
        } else {
            $("#photoslider .photo div:eq(" + (current - 1) + ")").fadeTo(1000, 0, function() {                     // fadeout current and with callback restore z-index (to 1)
                $(this).fadeTo(0, 1).hide().css('z-index', '1');
            });
        }

        // active navigation item + show image

        // display alt-value
        var alt = $("#photoslider .photo div:eq(" + (nr - 1) + ") span").html();
        $("#photoslider .footer span").html(alt);

        // set title
        $("#photoslider").attr('title', nr);
    }

    function stopTimer(active) {
        if (active) {
            active = !active;
            $("#photoslider").stopTime('controlled');
        }
        return active;
    }

    function resumeTimer(active) {
        if (!active) {
            active = !active;
            $("#photoslider").everyTime(3000, 'controlled', function(i) {
                if (active) {
                    var current = parseInt($("#photoslider").attr('title'));
                    var next = current + 1;
                    showItem(current, next, count);
                }
            });
            return active;
        }
    }

    $(document).pngFix();

    //cufon
    Cufon.replace('h1', { hover: true });
    Cufon.replace('h2', { hover: true });

});
