$(document).ready(function() {
    $("#logscroller").scrollable();

    //$("a[rel^='photoBox']").prettyPhoto();
    if ($("#photoscroller a").length > 0) {
        $("a[rel^='photoBox22222']").prettyPhoto();
        $("#gallery .large a").live("click", function(e) {
            e.preventDefault();
            images = [];
            titles = [];
            descriptions = [];
            currenthref = $(this).attr("href");
            rotatesteps = 0;

            $("#photoscroller a").each(function(key, val) {
                var o = $(val);
                if (o.attr("rel") == currenthref) {
                    rotatesteps = key;
                }
                images.push(o.attr("rel"));
                titles.push(o.find("img").attr("alt"));
                descriptions.push("");
            });


            //images.rotate(-rotatesteps);
            //titles.rotate(-rotatesteps);

            //descriptions.rotate(rotatesteps);

            $.prettyPhoto.open(images, titles, descriptions, rotatesteps);

        });

        $("#photoscroller a").click(function(el) {
            var alreadyloaded = $("#gallery .large a[href='" + $(this).attr("rel") + "']");
            if (alreadyloaded.length > 0) {
                if ($("#gallery .large a:last").attr("href") != alreadyloaded.attr("href")) {
                    var node = alreadyloaded;
                    node.fadeOut(1, function() {
                        $("#gallery .large").append(node);
                        node.fadeIn("slow");

                    });

                }
            } else {
                var node = null;

                if ($(this).parent().hasClass("ytmovie")) {
                    node = $("<a class=\"ytlink\" rel=\"photoBox\" style=\"display:none\" title=\"" + $(this).attr("title") + "\" href=\"" + $(this).attr("rel") + "\"><img src=\"" + $(this).attr("href") + "\" alt=\"" + $(this).children().first().attr("alt") + "\"/><img src=\"/Content/images/button_play_blue.png\" class=\"playbutton\" alt=\"\"/></a>");
                } else {
                    node = $("<a rel=\"photoBox\" style=\"display:none\" title=\"" + $(this).attr("title") + "\" href=\"" + $(this).attr("rel") + "\"><img src=\"" + $(this).attr("href") + "\" alt=\"" + $(this).children().first().attr("alt") + "\"/></a>");

                }
                var img = $("img", node);
                img.load(function() {
                    $("#gallery .large").append(node);
                    node.fadeIn("slow");


                });
            }
            return false;
        });
    } else {
        $("a[rel^='photoBox']").prettyPhoto();
    }

    $("#gallery").scrollable({size:5,nextPage : ".nextPageG",prevPage : ".prevPageG",globalNav:true});

    if ($("#comments").length > 0) {
        $.post("/vriendenajax", { Start: 0, End: 9 }, function(comments, textStatus) {
            var totalcomments = comments.Count;

            $("#comments").data("totalcomments", totalcomments);

            if (comments.Comments.length > 0) {
                for (i = 0; i < comments.Comments.length; i++) {
                    $("#comments ol").append(GetComment(comments.Comments[i], totalcomments--));
                }
            }

            $("#comments").scrollable({
                size: 3,
                onSeek: function() {
                    if (this.getIndex() % 3 == 0 && this.getPageAmount() - 2 == this.getPageIndex()) {
                        if (this.getItems().length < $("#comments").data("totalcomments")) {
                            var start = this.getItems().length;
                            $.post("/vriendenajax", { Start: start, End: start + 6 }, function(comments, textStatus) {
                                var api = $("#comments").scrollable({ api: true });

                                var totalcomments = $("#comments").data("totalcomments") - api.getItems().length;

                                if (comments.Comments.length > 0) {
                                    for (i = 0; i < comments.Comments.length; i++) {
                                        $("#comments ol").append(GetComment(comments.Comments[i], totalcomments--));
                                    }
                                }
                                api.reload();
                            });
                        }
                    }
                }
            });
        });
        $("#newcomment").toggle();

        $(".newcommentlink").bind("click", function(e) {
            $("#newcomment").toggle("slow");
            e.preventDefault();
        });


    }

    if ($("#map").length > 0) {
        var map = new GMap2(document.getElementById('map'));
        map.setMapType(G_SATELLITE_MAP);
        var center = new GLatLng(0, 0);
        map.setCenter(center, 1);
        map.setUIToDefault();


        $.post("/getlocations", {}, function(locations, textStatus) {


            var polyOptions = { geodesic: true };
            var polypoints = [];
            for (i = 0; locations.length > i; i++) {
                var point = new GLatLng(locations[i].Latitude, locations[i].Longitude);
                polypoints.push(point);

                markerOptions = {};
                var marker = new PdMarker(point, markerOptions);

                marker.setTooltip(locations[i].DatumString + ": " + locations[i].Name);



                map.addOverlay(marker);


            }



            var polyline = new GPolyline(polypoints, "#ff0000", 3, 1, polyOptions);

            map.addOverlay(polyline);
        });
    }
});




function GetComment(comment, nummer) {
    var ret = "<li><span class=\"header\"><strong>Naam:</strong> " + escapeHTML(comment.Name) + "<br/>";
    if (comment.Email != "") {
        ret += "<strong>Contact:</strong>";
        ret += " <a href=\"mailto:" + escapeHTML(comment.Email) + "\">E-mail</a>";
        ret += "<br/>";

    }
    ret += "</span><span class=\"berichtnummer\">" + nummer + "</span>"

    ret += "<p><strong>Bericht, " + escapeHTML(comment.PostedString) + "</strong><br/><br/><span>" + escapeHTML(comment.Text).replace("\r\n", "<br/><br/>") + "</span></p>";

    return ret + "</li>"
}

function escapeHTML(str) {
    var div = document.createElement('div');
    var text = document.createTextNode(str);
    div.appendChild(text);
    return div.innerHTML;
};

Array.prototype.rotate = function(p) {
    for (var l = this.length, p = (Math.abs(p) >= l && (p %= l), p < 0 && (p += l), p), i, x; p; p = (Math.ceil(l / p) - 1) * p - l + (l = p))
        for (i = l; i > p; x = this[--i], this[i] = this[i - p], this[i - p] = x);
    return this;
}
