//----------------------------------    
// When document loads
//----------------------------------
jQuery(function() {

    //----------------------------------    
    // Bounce external sites
    //----------------------------------
    var host = window.location.hostname.replace(/www/, '');
    $("a[href^='http']:not([target])").each(function() {
        var a = $(this);
        if (a.attr('href').indexOf(host) < 0)
            a.attr('target', '_blank');
    });

    //----------------------------------    
    // Decrypt emails
    //----------------------------------
    $("a[href^='mailto']").each(function() {
        var lnk = $(this).attr("href");
        $(this).html($(this).html().replace(/^(mailto:)(.+):([^\?]+)(\?.+)?$/i, "$3@$2$4"));
        $(this).attr("href", lnk.replace(/^(mailto:)(.+):([^\?]+)(\?.+)?$/i, "$1$3@$2$4"));
    });

    //----------------------------------    
    // Form validation
    //----------------------------------  
    $('form').each(function() {
        var frm = $(this);
        var req = frm.find(":input[name*='|required']").each(function() {
            $(this).attr('name', $(this).attr('name').replace(/\|required/i, ''));
        });
        frm.submit(function() {
            req.each(validateRequired);
            if (req.filter('.required-missing').length > 0) return false;
            return true;
        });
    });
    function validateRequired() {
        var fld = $(this);
        if (!fld.val().trim().length) fld.addClass('required-missing')
        else fld.removeClass('required-missing');
        fld.blur(validateRequired);
    }

    //----------------------------------    
    // Switchers
    //----------------------------------
    $(".switcher").each(function() {
        var divs = $(this).find(".bodycolinner");
        $(this).find(".switcher-nav a").hover(
      function() {
          var div = divs.filter('#' + $(this).attr('href'));
          if (div.is(":visible")) return;
          divs.hide();
          div.show();
          resizeColumns();
      }, function() { return false; })
      .click(function() { return false; });
        divs.eq(0).show();
    });

    //----------------------------------    
    // Lightbox
    //----------------------------------
    if ($.fancybox) {
        $(".lightbox").fancybox({ titlePosition: 'inside' });
        $(".lightbox-video").fancybox({
            type: 'iframe',
            titlePosition: 'inside',
            width: 660,
            height: 410
        });
        $(".lightbox-artist,.lightbox-program").fancybox({
            type: 'iframe',
            width: 660,
            height: 550
        });
    }

    //----------------------------------    
    // When window is loaded
    // Handle validator resizes
    // and update .Net Ajax reloads
    //----------------------------------  
    $(window).load(function() {
        window_load();
        try {
            Sys.WebForms.PageRequestManager.getInstance().add_endRequest(window_load);
            Sys.WebForms.PageRequestManager.getInstance().add_endRequest(document_ready);
        } catch (e) { }
    });

});

//----------------------------------    
// Flippies
//----------------------------------
function addFlippie(selector) {
    jQuery(function() {
        $(selector).click(function(e) {
            var $t=$(e.target);
            if ($t.is('a') && $t.parents('.feature-back').length) return true;
            doFlippie($(this));
            return false;
        })
    });
}
function doFlippie(f) {
  if (f.has(':animated').length>0) {
    f.data['t'] = setTimeout(function(){
      doFlippie(f)}, 500);
    return;
  }
  f.quickFlipper({
    closeSpeed: 360,
    openSpeed: 240
  });
}

//----------------------------------    
// Run after load
//----------------------------------
function window_load() {
  setTimeout(function() {
    resizeColumns();
    createEventMenus();
  }, 200);

  //----------------------------------    
  // Fade effect on buttons/links
  //----------------------------------
  $("h4 a,.FormButton:not(.Disabled)").hover(
    function(){$(this).fadeTo(250,.5)},
    function(){$(this).fadeTo(250,1)}
  );
}
//----------------------------------    
// Make sure columns are equal sized
//----------------------------------
function resizeColumns(){
  // What children should grow to fit?
  var ch=".pagebody,.pagelayout,.pagebody1col .block," +
    ">.block:last,>.row:last,>.row .block:last";
  // Clear height of columns and last children of column
  jQuery('.filler-processed').css('height', 'auto');
  // Clear padding on photobox
  var photo = jQuery(".column1 .photobox");
  photo.css({ paddingBottom: 0 });
  // Equalize columns
  jQuery('.clear-block').each(function(){
    var max = $(this).height();
    $(this).find('>.column').each(function(){
      var h=$(this).height();
      if (h>max) max=h;
    }).each(function(){
      var c = $(this);
      // Fix left column padding on image if not tall enough      
      if (c.is(".column1"))
        photo.css({paddingBottom:(h<=680?0:5)+'px'});
      var h=c.outerHeight();
      var diff=max-h;
      c.height(max).addClass('filler-processed').find(ch).each(function(){     
        $(this).css('height',($(this).height()+diff)+'px').addClass('filler-processed');
      });
    });
  });
}

//----------------------------------    
// Events page menu
//---------------------------------- 
function createEventMenus(){
  jQuery(".page-performance").each(function(){
    var mnus=$("[rel^='mnu']");
    var tabs=$(".menu-tab");
    var featurephoto=$(".featurephoto");
    mnus.not('mnu-processed').each(function(){
      var lnk=$(this);
      var tabid=lnk.attr("rel").replace(/^mnu-/,'');
      var tab=$("#" + tabid);
      if (tab.is(':visible')) lnk.addClass('active');
      lnk.click(function(){
        if (tab.is(':visible')) return (tabid=='order') ? true : false;      
        mnus.filter('.active').removeClass('active');
        lnk.addClass('active');        
        tabs.filter(":visible").hide();     
        tab.show();      
        // Hide feature photo if ordering
        if (tabid=='details' && !featurephoto.is(':visible')) {
          featurephoto.slideDown('slow',resizeColumns);
        } else if (tabid=='order' && featurephoto.is(':visible')) {
          featurephoto.slideUp('slow',resizeColumns);
        } else resizeColumns();
        return (tabid=='resources' || tabid=='order') ? true : false;
      });
      $(this).addClass('mnu-processed');      
    });
  });  
}

//
// Function to create a popup window
//
var popupwin=null;
function popup(url,options) {
   if (!options) options="width=610,height=500,scrollbars=yes,resizable=yes";
   popupwin = window.open(url,"popupwin",options);
   popupwin.focus();
   return false;
}
//
// Toggle layer on/off
//
function toggleFeature(elm) {
   var currobj=document.getElementById(currfeature);
   currfeature = elm;
   var obj=document.getElementById(currfeature);
   if (currobj) currobj.style.display = "none";
   if (obj) obj.style.display="";
   window_load();
   return false;
}

//
// Seat Selector
//
var sswin,sscontrol,ssbutton;
function SeatSelectorOpen(url,control,button) {
    // Get reference to control, button, and radio
    sscontrol=document.getElementById(control);
    ssbutton=button;                             
    if (!sscontrol||!ssbutton) return false;    
    // Open seat selector
    sswin=window.open('/orders/seatselector.aspx?'+url,"sswin",
        'width=602,height=472,directories=0,location=0,menubar=0,'+
        'resizable=0,scrollbars=0,status=0,titlebar=0');
    sswin.focus();
    return false;
}
function SeatSelectorUpdate(command,args) {
    // Store the values
    if (command=='update') {
        sscontrol.value=args;
        setTimeout("SeatSelectorPostback()",500);
    }    
}
function SeatSelectorPostback() {
    __doPostBack(ssbutton,'');
}
