$(document).ready(function(){

    // Open external links in new window
	$("a[href^=http://]").click(function(){
	   window.open(this.href);
	   return false;
	});
	
	//Current page
	$("#mainNav li ul li a").each(function(){
		if(location.href.match(this.href))
			$(this).addClass("currentPage");
	});
	
	// Font replacement - http://cufon.shoqolate.com/generate/
    Cufon.replace("#castListing h2, #creativeListing h2, #interviewsListing h2, .musicPage h2, h1");
    Cufon.now();
    
    //Ticket Pricing, Seating Chart popup
    $("a[rel]").overlay();
    
    //Puzzle's
	var mySettings = {
    	rows: 3,
    	cols: 3,
    	numbers: false,
    	control: {
    			toggleNumbers: false,
    			counter: false,
    			toggleOriginal: false,
    			timer: false
    	},
    	animation: {
    			shuffleRounds: 1,
    			slidingSpeed: 100,
    			shuffleSpeed: 200
    	}
	};
	$(".puzzle").jqPuzzle(mySettings);
	
	
	
	// ---------- Image Gallery ---------- //
    // Pre-load images
    $("#photoThumbnails ul li a").preload();
    
    // Hide the caption
    $("#photoThumbnails ul li a").next().hide(); 
    
    // Create img element and append it to div
    $("#imageScreen").append('<p><img src="" alt=""></p>'); 
    
    // Show first image by default
    var defaultImage = $("#photoThumbnails ul li:first a").attr("href"); 
    $("#imageScreen img").attr("src", defaultImage);
    
    // Show first caption by default
    var defaultCaption = $("#photoThumbnails ul li:first a").next().clone().show(); 
    $("#caption").append(defaultCaption)
    
    // Assign click event
    $("#photoThumbnails ul li a").click(function(){
    
        // Assign 'a' this href
        var a = $(this).attr("href");
        
        // Fade out image THEN fade in
        // if you don't want fade in effect use
        //$("#imageScreen img").attr("src", a ).show();
        
        $("#imageScreen img").fadeOut(500, function(){
            $(this).attr("src", a ).fadeIn(500);
        });
        
        // Assign 'b' a clone of the element immediately following our anchor
        // Why clone it? If the element isn't cloned it is moved and wouldn't
        // be available for a second click
        var b = $(this).next().clone().hide().fadeIn();
        
        // Replace anything within #caption with 'b'
        $("#caption").fadeOut(500, function(){
            $(this).fadeIn(500).html(b)
        })
        
        // Prevent browser from following link
        return false;
        
    });
    // ---------- End Image Gallery ---------- //
    
    
    // --------- Scroll Webisodes ------------//
    
    $("#chooseWebisode").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        visible: 4,
        circular: false
    });

	//-----------Social Networking Cycle----------//
	$("#cycleConnect").cycle({
		fx: "scrollDown",
		speed: 600,
		timeout: 2000
	});
    
    // ---------- Webisodes Gallery ---------- //

    // Create div element and append
    $("#webisodePlayer").append('<div id=""></div>');
    
    // Show first video by default
    var x = $("#chooseWebisode ul li:first").attr("rel");
    var y = $("#chooseWebisode ul li:first a").attr("href");
    
    $("#webisodePlayer div").attr("id", x);
    
    var flashvars = {};
	var params = {};
	params.wmode = "transparent";
	var attributes = {};
	swfobject.embedSWF(y, x , "378", "310", "9.0.0", false, flashvars, params, attributes);
    
    // Show first caption by default
    var defaultCaption = $("#chooseWebisode ul li:first a").next().clone().show(); 
    $("#caption").append(defaultCaption)
    
    // Assign click event
    $("#chooseWebisode ul li a").click(function(){
    
        var i = $(this).parent().attr("rel");
        var j = $(this).attr("href")
        
        $("#webisodePlayer object").remove();
        
        $("#webisodePlayer").append('<div id="' + i + '"></div>');
                
        var flashvars = {};
    	var params = {};
    	params.wmode = "transparent";
    	var attributes = {};
    	swfobject.embedSWF(j, i , "378", "310", "9.0.0", false, flashvars, params, attributes);
    	
        
        // Assign 'z' a clone of the element immediately following our anchor
        // Why clone it? If the element isn't cloned it is moved and wouldn't
        // be available for a second click
        var z = $(this).next().clone().hide().fadeIn();
        
        // Replace anything within #caption with 'b'
        $("#caption").fadeOut(100, function(){
            $(this).fadeIn(100).html(z)
        })
        
        // Prevent browser from following link
        return false;
        
    });
    // ---------- End Webisodes Gallery ---------- //


      /*  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
    $("#homeForm input:image").click(function() { 

      // First, disable the form from submitting
      $('form#homeForm').submit(function() { return false; });

      // Grab form action
      var formAction = $("form#homeForm").attr("action");

      // Validate Name
      var nameVal = $("#name").val();

      // 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 = "nzjji";
      var emailId = id + "-" + id;
      // Validate email address with regex
      if (!checkEmail(emailId)) {
        alert("Please enter a valid email address");
        return;
      }

      // Validate Bday
      var bdayVal = $("#Birthday-yr").val();
      if (bdayVal > "1997") {
        alert("You must be at least 13 to enter");
        return;
      }

      // Serialize form values to be submitted with POST
      var str = $("form#homeForm").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: "inc/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
          {
            $("#homeForm").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");
          }
        }
      });
    });
});

$(window).load(function() {
	// Fancy pants scroll bars
	$(".scrollPane").jScrollPane({scrollbarWidth:19, scrollbarMargin:10, dragMaxHeight: 44, dragMinHeight: 44});
});

