// Initialize plugins

$(document).ready(function() {
    
    // Open external links in a new window
    $("a[href^='http:']").not("[href*=window.location.host]").attr('target','_blank');
    $("a[href^='https:']").not("[href*=window.location.host]").attr('target','_blank');

	// Default text input values
	$(".defaultValue").each(function() {
        var defaultValue = this.value;
        $(this).focus(function() {
            if(this.value == defaultValue) {
                this.value = '';
            }
        });
        $(this).blur(function() {
            if(this.value == '') {
                this.value = defaultValue;
            }
        });
    });
    
    
    // Cycle through home page quotes
    $("#cycleQuotes").cycle({
        timeout: 3300,
        delay: 2500
    });
    
    $("#expandThis").expander({
        slicePoint:       0,  // default is 100
        expandText:         ' More News', // default is 'read more...'
        collapseTimer:    0, // re-collapses after 5 seconds; default is 0, so no re-collapsing
        userCollapseText: '[^]'  // default is '[collapse expanded text]'
    });

    // Sub nav
    $("#nav > ul > li").hover(function(){
        var lw = $(this).width();
        var uw = $("ul", this).width();
        var w = (lw - uw) / 2;
        $("ul", this).css({"display": "block", "margin-left": (w + 4) + 'px'})
    }, function(){
        $("ul", this).css({"display": "none"})
    })
    
    
    // Highlight current page
    $("#nav ul li a").each(function(){
        if(location.href.match(this.href)) {
            $(this).addClass("currentPage");
        }
    });
    
    // Highligt top nav when on sub nav page
    $("#nav ul li ul li a").each(function(){
        if(location.href.match(this.href)) {
            $(this).parent().parent().parent().children().eq(0).addClass("currentPage");
        }
        
    })
    
    // Letter Popup
      $('#linkLetter').click(function(){
          $('#overlay').css({
              "height": $(document).height(),
              "width": $(document).width()
          });
          $('#overlay').show();
          $('#aboutLetter').fadeIn(1000);
          return false;
      });

      // Close Share Pop Up
      $('#closeLetter').click(function(){
          $('#aboutLetter, #overlay').hide();
      });

      // Word of Moutn Popup
        $('#wordofmouth').click(function(){
            $('#overlay').css({
                "height": $(document).height(),
                "width": $(document).width()
            });
            $('#overlay').show();
            $('#aboutReview').fadeIn(1000);
            return false;
        });

        // Close Share Pop Up
        $('#closeReview').click(function(){
            $('#aboutReview, #overlay').hide();
        });
        
      // Cabaret Popup
        $('#cabaretPopup').click(function(){
            $('#overlay').css({
                "height": $(document).height(),
                "width": $(document).width()
            });
            $('#overlay').show();
            $('#cabaretPreview').fadeIn(500);
            return false;
        });

        // Close Share Pop Up
        $('#closeCab').click(function(){
            $('#cabaretPreview, #overlay').hide();
        });

      // Photo Gallery

      $("#photosPage #content ul li a").click(function(){
          $('#overlay').css({
              "height": $(document).height(),
              "width": $(document).width()
              
          });
          $("#overlay").show();
          $(this).append('<span class="loader"></span>');
          $(".loader").css("opacity","0.6");
          var img = new Image();
          var i = $(this).attr("rel")
          $(img).load(function () {
          	var imageWidth = img.width;
          	var imageHeight = img.height;
              $("#photoOverlay").css({
                  "display": "block",
                  "position": "absolute",
                  "top": "40%",
                  "left": "50%",
                  "width": imageWidth,
                  "height": imageHeight,
          		"margin-left": -imageWidth / 2,
          		"margin-top": -imageHeight / 2,
          		"z-index": "2000"
              });
          	$("#photoOverlayContent").html(img)
            $("#photoOverlayCaption").after('<div id="photoOverlayCaption">' + i + '</div>').remove();
          	$(".loader").hide();
          })
          .attr({ 
              src: this.href,
              alt: ""
          })
          .click(function(){
          	$("#photoOverlay").hide();
          })
          return false;
      })

      $("#photoOverlay .close").click(function(){
      	$("#photoOverlay").hide()
      	$('#overlay').hide();
      	return false;
      });


      // Videos

      var k = $('#videosPage .videoItem .preview').attr("id");
      $('#videosPage .videoItem .preview').css('background', 'url('+ k +') no-repeat 0 0');
      
      $(".videoItem a").click(function(){

          $('#overlay').css({
              "height": $(document).height()
          });
          $("#overlay").show();
          $('#videoGalleryWrapper').fadeIn(1000);
          var i = $(this).attr("rel");
          $("#videoGallery").after('<div id="videoGallery"><object type="application/x-shockwave-flash" style="width:570px; height:420px;" data="'+ i +'"><param name="movie" value="'+ i +'" /><param name="wmode" value="transparent" /></object></div>').remove();
          // Prevent browser from following link
          return false;
      });
      
      // Kill video
      $('#closeVideo').click(function(){
          $('#videoGalleryWrapper, #overlay').hide();
      });

      // Audience Popup

      if (this.location == "http://www.lacage.com/audience-reviews.html#c5t_form" || this.location == "http://lacage.com/audience-reviews.html#c5t_form") {
           $('#form').show();
       }

      $("#audReviews").click(function(){
          $('#form').animate({
              height: ['toggle', 'swing'],
              opacity: 'toggle'
            }, 1000, 'linear');

          // Prevent browser from following link
          return false;
      });
      


    // Stick sidebar.


}); // end ready