$(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});
});

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}