$(function(){
    
    // Hide and show text input value on email sign up
    $(".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;
            }
        });
    });

    // Costume slides
    $("#slides") 
        .cycle({ 
            fx: "scrollHorz", 
            speed: 400, 
            timeout: 0, 
            pager:  "#slides-nav",
            pagerAnchorBuilder: function(index, DOMelement) {
                return "<span></span>"
            },
            next: "#next-slide",
            prev: "#prev-slide"
        });
    

    // Open external links in a new window
    $("a[href^='http:']").attr('target','_blank');
    
    
    // Shake glass button
    $(window).load(function(){
        $("#home-page .bt_glass a")
            .effect("shake",
            {
                times:6,
                distance: 5
            },
            200)
            
            // Launch looking glass
            // Creates 3 divs overlay, #glass, #glass-object
            // SWFObject inserts looking glass into #glass-object
            
            $(".bt_glass").click(function(e){
                e.preventDefault();
                
                
                var overlay =   $("<div>", {
                                    css: {
                                        "position": "absolute",
                                        "top": 0,
                                        "left": 0,
                                        "width": "100%",
                                        "height": $(window).height(),
                                        "background-color": "#191919",
                                        "background-position": "50% 50%",
                                        "background-repeat": "no-repeat",
                                        "background-image": "url(_images/ajax-loader.gif)",
                                        "z-index": "100",
                                        "opacity": "0.98"
                                    },
                                    click: function() {
                                        $(this).next().remove();
                                        $(this).remove();
                                    }
                                })
                                .appendTo("body")
                
                var glass =     $("<div>",{
                                    id: "glass",
                                    css: {
                                            "position": "absolute",
                                            "top": "130px",
                                            "left": "50%",
                                            "width": "760",
                                            "height": "550",
                                            "margin-left": "-380px",
                                            "z-index": "1000"
                                    }
                                })
                                .append('<div id="glass-object">')
                                .insertAfter(overlay)
                                
                var close =     $("<div>",{
                                    css: {
                                        "position": "absolute",
                                        "top": "-30px",
                                        "right": "10px",
                                        "color": "#DEBC8C",
                                        "font-size": "18px",
                                        "cursor": "pointer"
                                    },
                                    text: "CLOSE X",
                                    click: function() {
                                        $(this).parent().prev().remove();
                                        $(this).parent().remove();
                                    }
                                })
                                .appendTo("#glass")

                var flashvars = {};
        		var params = {
        		    wmode: "transparent"
        		};
        		var attributes = {};
        		
                swfobject.embedSWF("looking_glass_camera.swf", "glass-object", "760", "550", "9.0.0", false, flashvars, params, attributes);

            })
            
    })
    
    


    /*  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);
    }
    
    $("#confirmation").hide();
    
    // 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 = "bgdyt";
      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: "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
    ------------------------------------------------------------------------ */
    
    
    /*  Video
    ------------------------------------------------------------------------ */

    $("#video-gallery").youtube({
        video: "NbDHhFt563U",
        width: "550",
        height: "329"
    });

    $("#video-menu li a").click(function(e){
        var ytid = $(this).attr("rel");
        $("#video-menu li a img").removeClass("select-shadow");
        $(this).children("img").addClass("select-shadow");
        $("#video-gallery").youtube({
            video: ytid,
            width: "550",
            height: "329",
    		play: "1"
        });
        e.preventDefault;
    });

    $(".scroll-pane").jScrollPane({
        scrollbarWidth: 8,
        showArrows: false,
        dragMinHeight:45,
        dragMaxHeight:45
    });

    /*  Referral ticket links
    ------------------------------------------------------------------------ */
    function tixref(tixclass, url) {
        $(tixclass).attr("href",url);
    }

    $(document).ready(function() {
        if ( $.cookie('refersource') == null ) {
            var origin = location.hash;
            var ref = origin.split("#");
            $.cookie( 'refersource', ref[1] );
        }
        if ( $.cookie('refersource') == 'refgoog' ) {
            tixref('.tixlink','http://www.ticketmaster.com/artist/1517745?camefrom=CFC_NED_WDLD_GGL');
        }
        else if ( $.cookie('refersource') == 'reffb' ) {
             tixref('.tixlink','http://www.ticketmaster.com/artist/1517745?camefrom=CFC_NED_WDLD_FACEBOOK');
        }
        else if ( window.location.pathname == '/ticket-info.html') {
            tixref('.tixlink','http://www.ticketmaster.com/artist/1517745?camefrom=CFC_NED_WDLD_TICKETS');
        }
        else {
            tixref('.tixlink','http://www.ticketmaster.com/artist/1517745?camefrom=CFC_NED_WDLD_SITE');
        }
    })


    /*  Track music downloads
    ------------------------------------------------------------------------ */

        function recordOutboundLink(link, category, action) {
        	try{
        		var pageTracker = _gat._getTracker("UA-19001942-1");
        		pageTracker._trackEvent(category, action);
        	} catch(err) {}
        }

        $("a[rel*='mp3download']").click(function(){
             recordOutboundLink(this, 'MP3 Downloads', 'wonderlandonbroadway.com');
             setTimeout('document.location = "' + this.href + '"', 300);
             return false;
        });
    
    
    
})

    
