
$(window).load(function(){
    // $("#vid-countdown").show();
    // // Video countdown
    // $("#vid-countdown .countdown").countdown({
    //     compact: true,
    //     layout: '{sn}',
    //     until: 3,
    //     onExpiry: launchVideo
    // });
    
    // $("#btn-enter-uk").animate({
    //     right: 600
    // }, {
    //     duration: 800,
    //     easing: "easeOutBounce",
    //     complete: function() {
    //         $("#btn-enter-us").animate({
    //             right: 530
    //         }, {
    //             duration: 800,
    //             easing: "easeOutBounce"
    //         })
    //     }
    // })
    
    $("#btn-play-video").animate({
        top: 453
    }, {
        easing: "easeOutBounce",
        duration: 800
    })
    
    
})


// Open external links in a new window
$("a[href^='http:']").attr('target','_blank');

// Hide and show text input values
$(".default-value").each(function() {
    var defaultValue = this.value;
    $(this).focus(function() {
        if(this.value == defaultValue) {
            this.value = '';
        }
    });
    $(this).blur(function() {
        if(this.value == '') {
            this.value = defaultValue;
        }
    });
});
    
function launchVideo() {
    $("#show-title, #quotes, #vid-countdown").hide();
    $("#media-player-wrapper").show().css("opacity", "0").animate({
        top: 30,
        left: 0,
        width: 538,
        height: 309,
        opacity: 1
    }, function(){
        var flashvars = {};
        var params = {
            play: "true",
            wmode: "transparent",
            allowscriptaccess: "always"
        };
        var attributes = {};
        swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&version=3", "media-player-object", "538", "309", "9.0.0", false, flashvars, params, attributes);
        $("#media-controls, #btn-close").show()
        
    })
    var progress = $("#progress-bar");
    var elapsed = $("#elapsed");
	interval = window.setInterval(function() {
	    elapsed.width( ( (ytplayer.getCurrentTime()  / ytplayer.getDuration()  )  * 100  ) + '%');
	},1000);
   
}

function onPlayerStateChange(newState) {
    //alert(newState);
    if ( newState == 0 ) {
        
        $("#media-player-wrapper").animate({
            top: 453,
            left: 787,
            width: 155,
            height: 43,
            opacity: 0
        }, function() {
            $("#show-title, #quotes").show();
            $(this).hide()
            // $("#media-player-object").css("visibility", "visible");
        })
    }
}

function playVideo() {
  if (ytplayer) {
    ytplayer.playVideo();
  }
}

function pauseVideo() {
  if (ytplayer) {
    ytplayer.pauseVideo();
  }
}

function stopVideo() {
    if (ytplayer) {
        ytplayer.stopVideo();
    }
}

function closeVideo() {
    
    // $("#media-controls, #btn-close").hide();
    // $("#media-player-object").css("visibility", "hidden");
    // ytplayer.mute();
    ytplayer.stopVideo();
    //var theEnd = ytplayer.getDuration();
    //ytplayer.seekTo(1000);
    clearInterval(interval)
    $("#media-player-wrapper").animate({
        top: 453,
        left: 787,
        width: 155,
        height: 43,
        opacity: 0
    }, function() {
        $("#show-title, #quotes").show();
        $(this).hide()
        // $("#media-player-object").css("visibility", "visible");
    })
    
}

// function progressBar() {
//     var progress = $("#progress-bar");
//     var elapsed = $("#elapsed");
//  interval = window.setInterval(function() {
//      elapsed.width( ( (ytplayer.getCurrentTime()  / ytplayer.getDuration()  )  * 100  ) + '%');
//  },1000);
    
    
    // progress.click(function(e) {
    // 
    //  // When a click occurs on the progress bar, seek to the
    //  // appropriate moment of the video.
    // 
    //  var ratio = (e.pageX-progress.offset().left)/progress.outerWidth();
    // 
    //  elapsed.width(ratio*100+'%');
    //  ytplayer.seekTo(Math.round(data.duration*ratio), true);
    //  return false;
    // });
// }

function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("media-player-object");
    ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
    ytplayer.cueVideoById("hTPdd3OqzXc",'', "large");
    ytplayer.playVideo();
}


// Play video button - the big button that says play video
$("#btn-play-video").click(function(){
    launchVideo();
    if($.browser.msie) {
        $("#btn-play").trigger("click")
    }
    return false;
})

// Play pause button - the play button control
$("#btn-play").click(function() {
    playVideo();
    return false;
})

$("#btn-pause").click(function() {
    pauseVideo();
    return false;
})

$("#btn-close").click(function() {
    closeVideo();
    return false;
})


/*  Submit e-list form via ajax
------------------------------------------------------------------------ */

// Check for valid email
function checkEmail(email) { 
  var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  var emailVal = $("#" + email).val();
  return pattern.test(emailVal);
}

// Sumbit e-list sign-up via ajax
$("#elist-form input:image").click(function() { 
  
  // First, disable the form from submitting
  $('form#elist-form').submit(function() { return false; });
  
  // Grab form action
  var formAction = $("form#elist-form").attr("action");
  
  // Hacking together id for email field
  // Replace the xxxxx below:
  // If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
  var id = "uuyuui";
  var emailId = id + "-" + id;
  // Validate email address with regex
  if (!checkEmail(emailId)) {
    alert("Please enter a valid email address");
    return;
  }
  
  // Serialize form values to be submitted with POST
  var str = $("form#elist-form").serialize();
  
  // Add form action to end of serialized data
  // CDATA is used to avoid validation errors
  //<![CDATA[
  var serialized = str + "&action=" + formAction;
  // ]]>
  
  // Submit the form via ajax
  $.ajax({
    url: "php/proxy.php",
    type: "POST",
    data: serialized,
    success: function(data){
      // Server-side validation
      if (data.search(/invalid/i) != -1) {
        alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
      }
      else
      {
        $("#elist-form").hide(); // If successfully submitted hides the form
        $("#confirmation").fadeIn("slow");  // Shows "Thanks for subscribing" div
        $("#confirmation").tabIndex = -1;
        $("#confirmation").focus(); // For screen reader accessibility
        // Fire off Google Analytics fake pageview
        //var pageTracker = _gat._getTracker("UA-XXXXX-X");
        //pageTracker._trackPageview("/newsletter_signup");
      }
    }
  });
});

/*  End elist form
------------------------------------------------------------------------ */

