var addthis_options = 'facebook, twitter, linkedin, delicious, google, more';
var addthis_exclude = 'email, favorites';
var addthis_brand = "ECB Project";

var autoadvance = true;

$(function () {

    // ---- Add classes to the body element for IE6 and IE7 ---- //

    if ($.browser.msie && $.browser.version.substr(0, 1) <= 6) $("body").addClass("ie6");
    if ($.browser.msie && $.browser.version.substr(0, 1) <= 7) $("body").addClass("ie7");


    // ---- Stripey tables ---- //

    $("table.stripe tr:even").addClass("alt");


    // ---- Rounded corners ---- //

    $("div.rounded").each(function (i) {
        if (!$(this).hasClass("leavealone")) {
            divContent = document.createElement("div");
            divContent.className = "rounded-content";
            $(this).wrapInner(divContent);

            divBottom = document.createElement("div");
            divBottom.className = "rounded-bottom";
            divBottom.appendChild(document.createElement("div"));
            $(this).append(divBottom);

            $(this).addClass("rounded-active");
        }
    });

    // ---- Equal heights ---- //

    $("div.equalize").equalHeights();


    // ---- Quotes ---- //

    $("blockquote").prepend("<div class=\"jumboquote\" />");


    // ---- Front page feature flipper ---- //

    if (!($.browser.msie && $.browser.version.substr(0, 1) < 5)) {
        // This doesn't work in IE6
		// why not ???

        $("#flipper a").each(function (i) {
            if (i == 0) $(this).addClass("selected");
            $(this).click(function () {
                autoadvance = false;
                GotoFeature(i);
            });
        });

        var timer = setInterval(AdvanceFlipper, 6000);
        var counter = 0;
    } else {
        $("#flipper").hide();
        $("div.feature").hide();
        $("div.feature:first").show();
    }

    function GotoFeature(i) {
        $("#flipper a").removeClass("selected");
        $("#flipper a:eq(" + i + ")").addClass("selected");
        $("div.feature").css({ opacity: 0.5 });
        $("div.feature:eq(" + i + ")").css({ opacity: 1 }).css('filter', '');    // Need to remove filter for IE text smoothing
        $("#home-masked").animate({ left: "-" + (600 * i) + "px" }, 750);
    }

    function AdvanceFlipper() {
        if (autoadvance) {
            counter < $("#flipper a").length - 1 ? counter++ : counter = 0;
            GotoFeature(counter);
        }
    }

    // ---- Newsletter watermark ---- //

    $("#newsletter #email").addClass("faint").click(function () {
        if ($(this).hasClass("faint")) $(this).val("").removeClass("faint");
    });


    // ---- Form validation ---- //

    $("form.validate").validate({
        invalidHandler: function (form, validator) {
            $("form.validate div.form-field em").hide();
        }
    });


    // ---- Partner matrix ---- //

   $("div.matrix div.matrixitem a").mouseenter(function () { ShowCallout(this); }).click(function (event) { ShowCallout(this); event.preventDefault(); });

    function ShowCallout(a) {
        $("div.matrixcallout").hide();

        //var callout = $($(a).attr("href"));
		var calloutparts = ($(a).attr("href")).split('#');
        var callout = $(document.getElementById(calloutparts[1]));
        callout.css("left", $(a).position().left - 200);
        callout.css("top", $(a).position().top - 210);
        callout.addClass("pngfix");
        callout.show();

        if ($.browser.msie && $.browser.version.substr(0, 1) <= 7 && $("div.matrix").length > 0) {
            $("#search").hide();
        }
    }


    // ---- Map locations ---- //

    $("div.map a.pin").click(function (event) { ShowLocation(this); event.preventDefault(); });
    $("div.map img.closeoverlay").click(function () { $("div.maplocation").hide(); $("div.mapmask").hide(); });
	$("div.mapmask").click(function () { $("div.maplocation").hide(); $("div.mapmask").hide(); });	
	

    function ShowLocation(a) {
        $("div.maplocation").hide();
        $("div.mapmask").addClass("pngfix").show();
		var locationparts = ($(a).attr("href")).split('#');
        var location = $(document.getElementById(locationparts[1]));
        location.css("left", $(a).position().left - 253);
        location.css("top", $(a).position().top - 210);
        location.addClass("pngfix");
        location.show();

        if ($.browser.msie && $.browser.version.substr(0, 1) <= 7) {
            $("#search").hide();
        }
    }


    // ---- Download form ---- //

    $("#newsletter_true, #newsletter_false").change(function () {
        if ($("#newsletter_true").is(":checked")) {
            $("#newsletterarea").show();
            $("#newsletterpersuade").hide();
            $("#newsletter_true").next().addClass("bold");
        } else {
            $("#newsletterarea").hide();
            $("#newsletterpersuade").show();
            $("#newsletter_true").next().removeClass("bold");
        }
    });
    $("#newsletter_true, #newsletter_false").change();


    // ---- Admin functions ---- //

    $("#pagetree li a:first-child").mouseenter(function () { ShowTreeTools(this); });

    if ($("#reorder").length != 0) {
        $("#reorder").sortable();

        $("form").submit(function () {
            // At the point the form is submitted, put the new ID order into the hidden weights field
            var neworder = "";
            $("#reorder div").each(function () {
                if (neworder != "") { neworder += "|"; }
                neworder += $(this).attr("id");
            });
            $("#neworder").val(neworder);
        });
    }

    $("tr .focusicon img").hide();
    $("tr").mouseenter(function () { $(this).find(".focusicon img").show(); });
    $("tr").mouseleave(function () { $(this).find(".focusicon img").hide(); });

    if ($("#Page_Title").length == 1 && $("#Page_Link").length == 1) {
        $("#Page_Title").change(function () {
            if ($("#Page_Link").val().length == 0) {
                var link = $("#Page_Title").val();
                link = link.replace(/^(.)|\s(.)/g, function ($1) { return $1.toUpperCase(); });
                link = link.replace(/ /g, "");
                $("#Page_Link").val(link);
            }
        });
    }


    // ---- Admin map ---- //

    if ($("#adminmap").length > 0) {
        $("#adminmap").click(function (e) {
            var x = e.pageX - $("#adminmap").offset().left;
            var y = e.pageY - $("#adminmap").offset().top;

            // x and y must be scaled to be in the range 1 - 100
            x = (x / $("#maparea").width()) * 100;
            y = (y / $("#maparea").height()) * 100;

            // round to 3dp
            x = x.toFixed(3);
            y = y.toFixed(3);

            // write to form fields
            $("#Location_PosX").val(x);
            $("#Location_PosY").val(y);

            UpdateMarkerPosition();
        });

        UpdateMarkerPosition();
    }

    function UpdateMarkerPosition() {
        $("#mapmarker").css("left", Math.round($("#maparea").width() * $("#Location_PosX").val() / 100 - $("#mapmarker").width() / 2));
        $("#mapmarker").css("top", Math.round($("#maparea").height() * $("#Location_PosY").val() / 100 - $("#mapmarker").height() / 2));
    }


    // ---- Admin page tree ---- //

    function ShowTreeTools(a) {
        $(a).after($("#pagetreetools"));

        $($("#pagetreetools a")[0]).attr("href", "/Pages/Edit/" + $(a).attr("pageid"));
        $($("#pagetreetools a")[1]).attr("href", "/Pages/ReOrder/" + $(a).attr("parentid"));
        $($("#pagetreetools a")[2]).attr("href", "/" + $(a).attr("link"));
        $($("#pagetreetools a")[3]).attr("href", "/Pages/Delete/" + $(a).attr("pageid"));

        $("#pagetreetools").hide();
        $("#pagetreetools").fadeIn();
        $("#pagetreetools").css("left", $(a).outerWidth() + 15);
    }


    // ---- Other admin bits ---- //

    $("#Feature_Type").change(function () {
        var type = $("#Feature_Type").val();
        
        $("div[featuretype]").each(function () {
            if ($(this).attr("featuretype").indexOf(type.toString()) >= 0) {
                $(this).show();
            } else {
                $(this).hide();
            }
        });
    }).trigger("change");


});


function showVideo(vid) {
    $('#video-' + vid).show();
}

function closeVideo(vid) {
    $('#video-' + vid).hide();
}


/*-------------------------------------------------------------------- 
* JQuery Plugin: "EqualHeights"
* by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
*
* Copyright (c) 2008 Filament Group
* Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)

* Version: 2.0, 08.01.2008
--------------------------------------------------------------------*/

$.fn.equalHeights = function () {
    $(this).each(function () {
        var currentTallest = 0;
        $(this).children().each(function (i) {
            if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
        });
        // for ie6, set height since min-height isn't supported
        if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({ 'height': currentTallest }); }
        $(this).children().css({ 'min-height': currentTallest });
    });
    return this;
};
