var storyHeight = "206px";

$(document).ready(function()
{
    $(".default-text").defaultText();

    $("#navigation ul li.menu").hover(
        function() {
            $(this).find('ul.secondary').show();
        },
        function() {
            $(this).find('ul.secondary').hide();
        }
    );

    $("#navigation li.secondary-menu").hover(
        function() {
            $(this).find('ul.tertiary').show();
        },
        function() {
            $(this).find('ul.tertiary').hide();
        }
    );

    $.getJSON("/ui/scripts/twitter.php?username=oneworldnow&count=2", twitterCallback);

    $.fn.containsValidEmail = function()
    {
        return $.isValidEmail($(this).val());
    }

    $.isValidEmail = function(email)
    {
        return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(email);
    };

    $("#newsletter-form").bind("submit", function(event)
    {
        event.preventDefault();
        if ($("#newsletter_email").containsValidEmail())
        {
            $(this).unbind("submit");
            $(this).submit();
        }
        else
        {
        }
    });

    if ($("#google-map").length)
    {
        var latLng = new google.maps.LatLng(47.601726,-122.334084);

        var myOptions = {
            zoom              : 14,
            center            : latLng,
            mapTypeId         : google.maps.MapTypeId.ROADMAP,
            navigationControl : true,
            mapTypeControl    : false,
            scaleControl      : false
        };

        var map = new google.maps.Map(document.getElementById("google-map"), myOptions);

        var markerPin = new google.maps.MarkerImage("/img/marker.png");
        var markerShadow = new google.maps.MarkerImage("/img/marker-shadow.png",
            new google.maps.Size(37, 32),
            new google.maps.Point(0, 0),
            new google.maps.Point(7, 32));

        var marker = new google.maps.Marker({
              map      : map,
              icon     : markerPin,
              shadow   : markerShadow,
              position : latLng,
              title    : "OneWorld Now!"
        });

        google.maps.event.addListener(marker, 'click', function() {
            window.location = $("#google-map-link").attr("href");
        });
    }

    $(".bio").click(function(event)
    {
        event.preventDefault();
        $(".bio-container .long").hide();
        $(".bio-container .short").show();
        var div = $(this).parent().parent().parent();
        var long = div.find(".long");
        var short = div.find(".short");
        long.show();
        short.hide();
    });

    $(".bio-close").click(function(event)
    {
        event.preventDefault();
        $(".bio-container .long").hide();
        $(".bio-container .short").show();
    });

    $(".video .thumb").hover(
        function() {
            $(this).parent().find('.play').show();
        },
        function() {
            $(this).parent().find('.play').hide();
        }
    );

    $(".read-more a").click(function(event)
    {
        event.preventDefault();
        var text = null;
        var cont = null;
        $(".story").css("height", storyHeight);
        $(".full").hide();
        $(".excerpt").show();
        if ($(this).text() == "read less")
        {
            cont = $(this).parent().prev();
            cont.css("height", storyHeight);
            text = "read more";
            cont.find(".excerpt").show();
            cont.find(".full").hide();
        }
        else
        {
            cont = $(this).parent().prev();
            cont.css("height", "auto");
            text = "read less";
            cont.find(".excerpt").hide();
            cont.find(".full").show();
        }
        $(this).text(text);
    });
});

if (window.Cufon != undefined)
{
    Cufon.replace(["h2", "h3"], {
        fontFamily : "gotham",
        hover      : true
    });
}

if (window.Shadowbox != undefined)
{
    Shadowbox.init({
        players : ['img', 'html', 'iframe', 'swf']
    });
}
