pageContent = "";

showing_different_content = "";

copyrightHtml = "<div id=\"copyright\" class=\"popup\">\
<div class=\"first\">\
<h1>Copyright</h1>\
<a href=\"javascript:close_different_content();\" class=\"close\">close</a>\
<p>All photographs, graphics, and text<br>\
© 2009 John B. Weller, unless noted.</p>\
<p>Music © 2009 Jeff Taylor</p>\
<p>The Last Ocean is an outreach project. All content is available for use by non-profits and FORSE members. Larger image files are available. Please contact us with any requests.</p>\
</div>\
</div>";

contactHtml = "<div id=\"contact\" class=\"popup\">\
<div class=\"first\">\
<h1>Contact</h1>\
<a href=\"javascript:close_different_content();\" class=\"close\">close</a>\
<p>We would love to hear from you.</p>\
<p>David Ainley<br>\
01.415.332.5718<br>\
<a href=\"mailto:david@lastocean.com\">david@lastocean.com</a></p>\
<p>John Weller (Media Contact)<br>\
01.303.817.3788<br>\
<a href=\"mailto:john@lastocean.com\">john@lastocean.com</a></p>\
<p>Peter Young<br>\
64.3.389.8818<br>\
<a href=\"mailto:peter@lastocean.com\">peter@lastocean.com</a></p>\
</div>\
</div>";

whatsNewHtml = "<div id=\"whats_new\" class=\"popup\">\
<div class=\"first\">\
<h1>What’s New</h1>\
<a href=\"javascript:close_different_content();\" class=\"close\">close</a>\
<p>We will continually be updating the site. Right now, everything is new. Check back here for the latest site updates.</p>\
</div>\
</div>";

function strip_tags(s){
    return s.replace(/<\/?[^>]+>/gi, '');
}

function validate_email(value){
    // adapted from jQuery validation plug-in 1.5.1
    // by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
    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(value);
}

function show_different_content(thisID, thisHtml){
    if(showing_different_content !== thisID){
        if(showing_different_content !== ""){
            $("div.scrollable").after(thisHtml);
            $("div.popup#"+thisID).css("opacity","0.0001").fadeTo(300, 1, function(){
                $("div.popup#"+showing_different_content).remove();
                showing_different_content = thisID;
            });
        }else{
            showing_different_content = thisID;
            $("div.scrollable").after(thisHtml);
            $("div.popup").css("opacity","0.0001").fadeTo(300, 1);
        }
    }
}

/* function show_different_content(thisID, thisHtml){
    if(showing_different_content !== thisID){
        if(showing_different_content !== ""){
            $("div.popup").fadeOut(300, function(){
                $(this).remove();
                showing_different_content = thisID;
                $("div.scrollable").after(thisHtml);
                $("div.popup").css("opacity","0").fadeTo(300, 1);
            });
        }else{
            showing_different_content = thisID;
            $("div.scrollable").after(thisHtml);
            $("div.popup").css("opacity","0").fadeTo(300, 1);
        }
    }
} */

function close_different_content(){
    $("div.popup").fadeOut(300, function(){
        showing_different_content = "";
        $(this).remove();
    });
}

function openInstallFlash(){
    installFlashPopup = window.open( "http://get.adobe.com/flashplayer/","install_flash_popup","width=790,height=650,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable,left="+(screen.width-750)+",top=0");
    if((installFlashPopup!=null) && (typeof installFlashPopup!="undefined")){
        installFlashPopup.focus();
    }
}

function viewportHeight(){
    return self.innerHeight || jQuery.boxModel && document.documentElement.clientHeight || document.body.clientHeight;
};

function resizeToFit(){
    /* thisWrapperHeight = 698;
    thisHeaderTop = 0;
    thisColumnTop = 7;
    thisFooterBottom = 35; */
    thisViewportHeight = viewportHeight();
    if(thisViewportHeight < 704){
        if(thisViewportHeight > 598){
            thisTableHeight = thisViewportHeight - 2;
            thisWrapperHeight = thisViewportHeight - 6;
            thisHeaderTop = (thisViewportHeight - 679) / 3.57142857;
            thisColumnTop = (thisViewportHeight - 687) / 2.42857143;
            thisFooterBottom = (thisViewportHeight - 304) / 11.4285714;
        }else{
            // alert("minimum height reached");
            thisTableHeight = 596;
            thisWrapperHeight = 592;
            thisHeaderTop = -22;
            thisColumnTop = -36;
            thisFooterBottom = 26;
        }
        // $("html, body, table.centering_wrapper").css("height",thisTableHeight+"px");
        $(".wrapper").css("height",thisWrapperHeight+"px");
        $(".header").css("top",thisHeaderTop+"px");
        $(".bodycontent .left_column, .bodycontent .right_column").css("top",thisColumnTop+"px");
        $(".footer").css("bottom",thisFooterBottom+"px");
    }
}

$(document).ready(function(){
    bodyID = $("body").attr("id");
    bodyClass = $("body").attr("class");
    $(".header a.contact").click(function(){
        show_different_content("contact",contactHtml);
        return false;
    });
    $(".header a.copyright").click(function(){
        show_different_content("copyright",copyrightHtml);
        return false;
    });
    $(".player div, #soundslider").mouseover(function(){
        $(this).css("cursor","pointer");
    }).mouseout(function(){
        $(this).css("cursor","auto");
    }).click(function(){
        openInstallFlash();
    });
    if(bodyID == "index"){
        $("a.whats_new").click(function(){
            show_different_content("whats_new",whatsNewHtml);
            return false;
        });
    }else{
        resizeToFit();
        var resizeTimer = null;
        $(window).bind('resize', function(){
            if(resizeTimer) clearTimeout(resizeTimer);
            resizeTimer = setTimeout(resizeToFit, 500);
        });
        if(bodyID == "story" && bodyClass == "physical_characteristics"){
            $("span.continental_shelf").css("text-decoration", "underline").mouseover(function(){
                $(this).append("<span style=\"background:url(/images/story/physical_characteristics/rollovers.jpg) no-repeat 0 -126px;width:101px;height:73px;position:absolute;left:436px;top:439px;\"></span>");
            }).mouseout(function(){
                $("span", this).remove();
            });
            $("span.continental_slope").css("text-decoration", "underline").mouseover(function(){
                $(this).append("<span style=\"background:url(/images/story/physical_characteristics/rollovers.jpg) no-repeat 0 -199px;width:62px;height:91px;position:absolute;left:426px;top:456px;\"></span>");
            }).mouseout(function(){
                $("span", this).remove();
            });
            $("span.france").css("text-decoration", "underline").mouseover(function(){
                $(this).append("<span style=\"background:url(/images/story/physical_characteristics/rollovers.jpg) no-repeat;width:124px;height:126px;position:absolute;left:312px;top:259px;\"></span>");
            }).mouseout(function(){
                $("span", this).remove();
            });
        }else if(bodyID == "story" && bodyClass == "background" && $.browser.msie && parseInt($.browser.version) <= 6){
            $(".maximum_catch").mouseover(function(){
                $(this).css("background-position-y","-270px");
            }).mouseout(function(){
                $(this).css("background-position-y","0");
            });
        }else if(bodyID == "community" && bodyClass == "join_forse"){
            $("input").focus(function(){
                if(this.value == "Name" || this.value == "Email Address"){
                    this.value = "";
                }
                this.select();
            }).blur(function(){
                if(this.value == ""){
                    if($(this).attr("name") == "name"){
                        this.value = "Name";
                    }else if($(this).attr("name") == "email"){
                        this.value = "Email Address";
                    }
                }
                // alert($(":submit").css("height"));
            });
            $(":submit").click(function(){
                hasErrors = false;
                $("input").each(function(){
                    if($(this).attr("name") == "name"){
                        $("~ em.name", this).remove();
                        this.value = strip_tags(this.value);
                        if(this.value == "" || this.value == "Name"){
                            hasErrors = true;
                            $(this).after("<em class=\"name\">Required field</em>");
                        }
                    }else if($(this).attr("name") == "email"){
                        $("~ em.email", this).remove();
                        this.value = strip_tags(this.value);
                        if(this.value == "" || this.value == "Email Address"){
                            hasErrors = true;
                            $(this).after("<em class=\"email\">Required field</em>");
                        }else if(validate_email(this.value) === false){
                            hasErrors = true;
                            $(this).after("<em class=\"email\">Valid email required</em>");
                        }
                    }
                });
                this.blur();
                if(hasErrors === true) return false;
            });
            if($.browser.msie && parseInt($.browser.version) <= 6){
                $("input.button").mouseover(function(){
                    // alert("1");
                    $(this).css("background-position-y","-51px");
                }).mouseout(function(){
                    // alert("2");
                    $(this).css("background-position-y","0");
                }).click(function(){
                    // alert("3");
                    $(this).css("background-position-y","0");
                });
            }
        }
    }
  /* $(".bodycontent .left_column ul li a").mouseover(function(){
    $("span", this).fadeIn(300);
  }).mouseout(function(){
    $("span", this).fadeOut(300);
  });
  $(".bodycontent .left_column ul li a span").css({display:"none",zIndex:"0"});
  thisID = $("body").attr("id");
  if(thisID == "index"){
      $(".footer ul li").mouseover(function(){
        // document.title += " mouseOVER";
        $(this).animate({opacity:"0.6"},1000);
      }).mouseout(function(){
        // document.title += " mouseOUT";
        $(this).animate({opacity:"0"},300);
      });
  }else{
      $(".footer ul li").mouseover(function(){
        // document.title += " mouseOVER";
        $(this).animate({opacity:"0"},1000);
      }).mouseout(function(){
        // document.title += " mouseOUT";
        $(this).animate({opacity:"0.6"},300);
      });
      $(".footer ul li a:hover").css({opacity:"0.6"});
  } */
});