/// <reference path="jquery-1.4.1-vsdoc.js" />
// JavaScript document based on jQuery 1.4.1
// by Tim van den Hombergh 2009

// Constants
var pathToImage = "/images/spinner.gif";

$(document).ready(function() {

    //Preload loading  image for Ajax interaction
    imgLoading = new Image();
    imgLoading.src = pathToImage;
    imgHtml = "<p class=\"center\"><br/><img src=\"" + imgLoading.src + "\"></p><p class=\"center\">Loading...</p></p>";

    //Init external hyperlinks
    $("a[rel=external]").each(function() {
        this.target = "_blank";
    });

    //Init fancybox behaviors
    $("a.fancybox").fancybox({ autoDimensions: false, width: 564 });
    $("a.thickbox").fancybox({ scrolling: 'no' });

    //Init form validation
    if ($("form").hasClass("jsValidation")) $.getScript("/scripts/formvalidation.js");

    //Init ajax post urls
    $("body").AjaxPost();

    //Init auto hide behaviors on hyperlinks
    $("a.jsAutoHide").click(function() {
        $(this).hide();
    });

    $("body").ScrollToAnchor();

    // Foto dropdown list change event
    $("#photocollist").change(function() {

        //Display loading image
        $("#photocoldata").html(imgHtml);

        var url = $("#frmPhotos").attr("action").replace("#photocollist", "") + "?ajaxPost=true";
        $.ajax({
            type: "post",
            url: url,
            data: { photocollist: $(this).val() },
            error: function() {
                $("#frmPhotos").submit();
            },
            success: function(html) {
                $("#photocoldata").hide().html(html).fadeIn();
                $("#photocoldata a.thickbox").fancybox();
            }
        });
    });
});

$.fn.ScrollToAnchor = function() {

    ahrefInit = function(a) {
        a.anchor = "#" + a.href.split("#")[1];
        $(a).click(function() {
            $.scrollTo(a.anchor, 800);
            return false;
        });
    };

    //Init scrollTo behaviors
    if (this.find("a").hasClass("jsScrollTo")) $.getScript("/scripts/scrollTo.js");

    this.find("a.jsScrollTo").each(function() {
        ahrefInit(this);
    });
};

//Ajax post method
$.fn.AjaxPost = function() {

    var previous;

    // init a href
    ahrefInit = function(a) {

        a.postId = "#" + a.href.split("#")[1];
        a.url = a.href.replace(a.postId, "");
        $(a).click(function() {
            if ($(a.postId).size()) {
                $(a.postId).html(imgHtml);
                doPost(a);
                $(previous).find("span").removeClass('highlight');
                $(a).find("span").addClass('highlight');
                previous = a;
                return false;
            }
        });
    };

    // init select boxes
    selectInit = function(s) {

        $(s).change(function() {
            s.url = $(this).val().split("#")[0];
            s.postId = "#" + $(this).val().split("#")[1];
            $(s.postId).html(imgHtml);
            doPost(s);
        });
    };

    // do the ajax call
    doPost = function(a) {
        $.ajax({
            type: "get",
            url: a.url + "&ajaxPost=true",
            error: function() {
                location.href = a.url;
            },
            success: function(html) {
                $(a.postId).hide().html(html).fadeIn().AjaxPost();
                $(a.postId).ScrollToAnchor();
                $(a.postId).find("a.thickbox").fancybox({ scrolling: 'no' });
                $(a.postId).find("a.fancybox").fancybox({ autoDimensions: false, width: 564 });
            }
        });
    };

    this.find("a.jsAjaxPost").each(function() {
        ahrefInit(this);
    });

    this.find("select.jsAjaxPost").each(function() {
        selectInit(this);
    });
};
