// Adds CSS code dynamically
function addCss(cssCode) {
    var styleElement = document.createElement("style");
    styleElement.type = "text/css";
    if (styleElement.styleSheet) {
        styleElement.styleSheet.cssText = cssCode;
    } else {
        styleElement.appendChild(document.createTextNode(cssCode));
    }
    document.getElementsByTagName("head")[0].appendChild(styleElement);
}

// Advances the slideshow
function autoSlideSwitch() {
    if ($stopslides == false) {
        slideSwitch(1000);
    }
}
function slideSwitch(timing) {
    var $active = $('#slideshow .slide.active');
    if ( $active.length == 0 ) $active = $('#slideshow .slide:last');
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow .slide:first');
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, timing, function() {
            $active.removeClass('active last-active');
        });
}


// Hide elements that will be faded in later
addCss('img.fadeonload {visibility: hidden;} div#container {visibility: hidden;}')

$(document).ready(function() {
    // Fade in the main containers
    $('#container').css("visibility","visible").hide().fadeIn(500);
    
    // Email address sign-up AJAX handler
    $('#giveyouremail').submit(function() {  
        if (($("#address").val() == "") || ($("#address").val().indexOf("@") < 1)) {
            $('#address').focus()
            return false;  
        }
        $.ajax({
          type: "POST",
          url: "script/sendemail.php",
          data: "address=" + $('#address').val(),
          success: function() {
              $('#giveyouremail').fadeOut(200);
              $('#thankyou').delay(210).fadeIn(200);
          }
        });
        return false;
    });
    
    // Email address sign-up field
    $('input#address').focusin(function() {
        if  ($('input#address').val() == 'Can I have your email?') {
            $('input#address').val('')
        }
    });
    $('input#address').focusout(function() {
        if  ($('input#address').val() == '') {
            $('input#address').val('Can I have your email?')
        }
    });
    
    // Download code sign-up field
    $('input#code').focusin(function() {
        if  ($('input#code').val() == 'CODE GOES HERE') {
            $('input#code').val('')
        }
    });
    $('input#code').focusout(function() {
       if  ($('input#code').val() == '') {
           $('input#code').val('CODE GOES HERE')
       }
    });
    
    // Slideshow start/stop
    $stopslides = false;
    $('#slideshow').mouseenter(function() {
        $stopslides = true;
    });
    $('#slideshow').mouseleave(function() {
        $stopslides = false;
    });
    // Slide show timer
    setInterval( "autoSlideSwitch()", 7000 );
});



