$(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 ---------- //

});

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