/* --------------------
various start up scripts
 - used to add the relevent events onto the search buttons
-------------------- */

var firstPage = "#news-page";
var currentPage = firstPage;

var offsetApplied = false;
var currentOffset = 226;

var flickrIframe = "<iframe align=\"center\" src=\"http://www.flickr.com/slideShow/index.gne?set_id=###\" frameBorder=\"0\" width=\"450\" scrolling=\"no\" height=\"450\"></iframe>";

function MoveTo(page) {
    
    if (page == currentPage)
        return;
    
    var cp = $(currentPage).offset().left; // the currentPage's offet
    var np = ($(page).offset().left); // the newPage's offset
    var diff = np - cp;    // how much we need to scroll the page by - the positioning of the "blocks" container

    if (page != firstPage && !offsetApplied) {
        diff = diff + currentOffset;
        offsetApplied = true;
    }
    else if (page == firstPage && offsetApplied)
    {
        diff = diff - currentOffset;
        offsetApplied = false;
    }

    if (diff < 0)
        $("#container").animate({ "left": "+=" + (diff * -1) + "px" }, "slow", function () { $.rerun(); });
    else
        $("#container").animate({ "left": "-=" + diff + "px" }, "slow", function () { $.rerun(); });
    
    currentPage = page;
}

function checkHash() {
    if (location.hash + "-page" != currentPage && location.hash != "")
    {
        $(".navigation").removeClass("selected");
        $("#nav_" + location.hash.replace("#", "")).addClass("selected");
        MoveTo(location.hash + "-page");
    }
}

var currentStatus = 1;
var currentImage = 0;

function NextPhoto() {

    if ((currentImage + 1) < $("#viewing .photo").length) {
        currentImage += 1;
        var _this = $("#viewing .photo")[currentImage];
        $("#the-photo").html("<img src=\"" + $(_this).attr("href") + "\" />");        
    }
}

function PreviousPhoto() {
    if ((currentImage - 1) >= 0) {
        currentImage -= 1;
        var _this = $("#viewing .photo")[currentImage];
        $("#the-photo").html("<img src=\"" + $(_this).attr("href") + "\" />");
    }
}

function ApplyPhotoEvents() {

    $(".photo").each(function (index, obj) {
        $(this).click(function () {

            currentImage = index;
            $("#photos-overlay").css("height", $(window).height() + "px");
            $("#photos-overlay").css("height", $(window).width() + "px");

            var marginTop = ($(window).height() - 593) / 2;
            if (marginTop < 0) marginTop = 0;
            $("#photos-holder").css("margin-top", marginTop + "px");

            var marginLeft = ($(window).width() - 720) / 2;
            if (marginLeft < 0) marginLeft = 0;
            $("#photos-holder").css("margin-left", marginLeft + "px");

            $("#the-photo").html("<img src=\"" + $(this).attr("href") + "\" />");

            $('#photos-overlay').css('filter', 'alpha(opacity=85)');
            $('#photos-overlay').fadeIn(200, function () {
                $("#photos-holder").css("display", "block");
            });

            return false;
        });
    });

    $(window).resize(function () {
        if ($("#photos-overlay").css("display") == "block") {
            var marginTop = ($(window).height() - 593) / 2;
            if (marginTop < 0) marginTop = 0;
            $("#photos-holder").css("margin-top", marginTop + "px");

            var marginLeft = ($(window).width() - 720) / 2;
            if (marginLeft < 0) marginLeft = 0;
            $("#photos-holder").css("margin-left", marginLeft + "px");
        }
    });

    $("#closePhotos").click(function () {
        $("#photos-holder").css("display", "none");
        $("#photos-overlay").fadeOut("fast");
        $("#photos-frame").attr("src", "");
    });
}



function ApplyFacebookPagerEvents() {

    $($(".status")[0]).css("display", "block");

    $("#previous").click(function () {
        if (currentStatus == 1)
            return;
        $("#status_" + currentStatus).css("display", "none");
        $("#status_" + (currentStatus - 1)).css("display", "block");
        currentStatus--;
    });

    $("#next").click(function () {
        if (currentStatus == $(".status").length)
            return;
        $("#status_" + currentStatus).css("display", "none");
        $("#status_" + (currentStatus + 1)).css("display", "block");
        currentStatus++;
    });
}

function ApplyNavigationEvents() {
    if (location.pathname == "/default.aspx" || location.pathname == "/default.html" || location.pathname == "/" ||  location.pathname.indexOf("/news") >= 0 )
        $("#nav_news").addClass("selected");

    if (location.pathname.indexOf("gigs") > 0)
        $("#nav_gigs").addClass("selected");

    setInterval("checkHash();", 100);

    $(".navigation").click(function () {
        $(".navigation").removeClass("selected");
        $(this).addClass("selected");
    });
}

$(document).ready(function () {
    ApplyFacebookPagerEvents();
    ApplyNavigationEvents();

    $("#btn-register").click(function () {

        var fn = $("#field-firstname").val();
        var sn = $("#field-surname").val();
        var em = $("#field-email").val();
        var loc = $("#field-location").val();

        var filter = /^\w+([-+.\']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;

        if (fn.length == 0) {
            alert("Please provide your first name.");
            $("#field-firstname").focus;
            return false;
        }

        if (em.length == 0) {
            alert("Please provide your email address.");
            $("#field-email").focus;
            return false;
        }

        if (!filter.test(em)) {
            alert("Please provide a valid email address");
            $("#field-email").focus;
            return false;
        }
        var url = "/common/handlers/register.ashx";

        $.post(url, { firstname: fn, surname: sn, email: em, location: loc }, function (data) {
            if (data.Complete) {
                $("#processing").css("display", "none");
                $("#thank-you").css("display", "block");
            }
            else {
                alert(data.Message);
                $("#the-form").css("display", "block");
                $("#processing").css("display", "none");
            }
        }, 'json');

        // doing something...
        $("#the-form").css("display", "none");
        $("#processing").css("display", "block");
    });

});
 
