// HOME

// PAGE LOAD 

$(document).ready(function()
	{
  	   
  	   // note: see royal_common.js for the search functions
  	   
	    $("#newsletter_btn").click(function()
	    {
           newsleterClickHander();
        })
        
         shortcut.add("enter",function() {
	                   
	                   var myfocussedElement = $("*:focus").attr("id");
	                   
	                   if ( myfocussedElement == "email_txt" ) 
	                   {
	                       $("#newsletter_btn").click();
	                   }
	                   
                    });
            
        
        $("#email_txt").focus(function()
	    {
             if ( document.getElementById("email_txt").value == "Enter your email" || document.getElementById("email_txt").value == "Thank you, email received.")
             {
                 document.getElementById("email_txt").value = "";
             }
             
             $("#email_txt").css({"font-size":"11px", "font-family": "Helvetica,Arial, sans-serif", "color": "#666", "border":"2px solid #eee", "border-left": "2px solid #999",  "border-top": "2px solid #999", "width": "150px"});
        })
        
     
        
        var nowPlayingHeight = $("#main_content_div").height();
        var numFilmBlocks =  $(".now_playing_ul").length;
        var filmBlockHeight = 300;
        var addHeightStr;
        
        if ( nowPlayingHeight < 500 ) 
        {
            // addHeightStr = String(550) + "px";
             $("#main_content_div").height(550);
        }
        else
        {
            newFilmHeight = (numFilmBlocks * filmBlockHeight) + 25;
            $("#main_content_div").height(newFilmHeight);
         }
        
       // alert("main height: " + $("#main_content_div").height() );
       // alert("number films: " + $(".now_playing_ul").length );
        
        var mainContentHeight =  $("#main_content_div").height();
        
         updateUIHeight(mainContentHeight);
        
	}
);

// NEWSLETTER
/*
You can supply an address like this:

http://roydev.audt.ca/subscribe/?email=jrudy@theatred.com

Unless you supply an email field in the query string, it raises a 404 error. 
Otherwise, it will return a json object with 2 fields: success and message. 
If the email format is valid and it’s not already in the database, it get’s added 
to the newsletter list and success equals true. If the email is not added to the 
database (because it was already there or if it’s not formatted properly), success equals false. 
message is human readable message for the user.
*/ 

var email = "";

function newsleterClickHander()
{
    var emailStr = document.getElementById("email_txt").value;
    var emailValid = validate_email( emailStr )
       
    if ( emailValid ) 
    {
        newsletterJSONRequest();
    }
    
}

function validate_email(emailStr)
{
    var validEmail = false;
    
      var apos = emailStr.indexOf("@");
      var dotpos = emailStr.lastIndexOf(".");
      if (apos<1||dotpos-apos<2)
      {
        $("#email_txt").css({"font-size":"11px", "font-family": "Arial, Helvetica, sans-serif", "color": "Red", "border":"2px solid #eee", "border-left": "2px solid #999",  "border-top": "2px solid #999", "width": "150px"});
        validEmail = false;
      }
      else 
      {
        $("#email_txt").css({"font-size":"11px", "font-family": "Arial, Helvetica, sans-serif", "color": "#000", "border":"2px solid #eee", "border-left": "2px solid #999",  "border-top": "2px solid #999", "width": "150px"});
        validEmail = true;
        
        email = emailStr;
      }
      
      return validEmail;
}

function newsletterJSONRequest()
{
    var jsonURL = "http://www.theroyal.to/subscribe/?email=" + email;
    $.getJSON(jsonURL, 
        function(data, textStatus){
            document.getElementById("email_txt").value =  data.message;
        });
}

function updateUIHeight(mainContentHeight)
{
            var bufferHeight = 100;
                  
           
           // SLICE
           
            var sliceNewTop = mainContentHeight;
            $('.main_slice_bg').css( { "top" :  String(mainContentHeight) + "px" } );
           
            // SLICE BENEATH
                
            var sliceBoxNewTop = sliceNewTop +  $('.main_slice_bg').height() - 20;
            $('.main_whitebox_beneathslice').css( { "top" :  String(sliceBoxNewTop) + "px" } );
           
           
           // COMING SOON
           $('#comingsoon').css( { "top" :  String( sliceNewTop + 75 ) + "px" } );  
                             
           // FOOTER
           var newFooterTop = sliceBoxNewTop + $('.main_whitebox_beneathslice').height() - 2;
           $('#footer').css( { "top" :  String(newFooterTop) + "px" } );
           
           // SIDEBAR
            var sidebarNewHeight = mainContentHeight + $('.main_slice_bg').height() +  $('.main_whitebox_beneathslice').height() -  $('.sidebar_top').height() - 12;
           $('.sidebar_block').height(sidebarNewHeight);
           
           // MAIN INNNER
           var mainInnerNewHeight = mainContentHeight +  $('.main_slice_bg').height() +   $('.main_whitebox_beneathslice').height() + $('#footer').height() - 50;
           $('#main_inner').height(mainInnerNewHeight);
           
           // MAIN HEIGHT
           
            //alert("main_inner: " + mainInnerNewHeight);
           
            var mainNewHeight = mainInnerNewHeight + 8;
           
          //alert("mainInnerNewHeight: " + mainNewHeight);
           
           $('#main').height(mainNewHeight);
}

