//////////////////////////////////////////////////////////////////////////////////
// Overlabel
//////////////////////////////////////////////////////////////////////////////////
jQuery.fn.overlabel = function() {
    this.each(function(index) {
        var label = $(this); var field;
        var id = this.htmlFor || label.attr('for');
        if (id && (field = document.getElementById(id))) {
            var control = $(field);
            label.addClass("overlabel-apply");
            if (field.value !== '') {
                label.not('.readonly').css("display", "none");
            }
            control.focus(function () {label.not('.readonly').css("display", "none");}).blur(function () {
                if (this.value === '') {
                    label.css("display", "block");
                }
            });
            label.click(function() {
                var label = $(this); var field;
                var id = this.htmlFor || label.attr('for');
                if (id && (field = document.getElementById(id))) {
                    field.focus();
                }
            });
        }
    });
};
//////////////////////////////////////////////////////////////////////////////////
// Document Ready
//////////////////////////////////////////////////////////////////////////////////
$(document).ready(function(){

  // external links
	$('a[rel=external]').click(function(){
		window.open(this.href);
		return false;   
	});


  function initCycle(elem, type) {
    var len = $(elem+'>*').length;
    var opts = {};
    opts.fx = 'fade';
    opts.speed = 800;
    if (type === 'twitter') {
      opts.timeout = 5000;
      opts.prev = '.twitNavigator .prev';
  		opts.next = '.twitNavigator .next';
    } else {
      opts.timeout = 0;
    }
    if (type === 'event') {
      opts.prev = '.eventNavigator .prev';
  		opts.next = '.eventNavigator .next';
  		opts.before = function() {
  			$('.rsvp').hide();
  		};
  		opts.after = function(){
  			$('.rsvp').show();
  		};
    }
    if (type === 'location') {
      opts.prev = '.locCatNavigator .prev';
  		opts.next = '.locCatNavigator .next';
    }
    if(len>1 && jQuery.fn.cycle){
      $(elem).cycle(opts);
    } else {
      $('.locCatNavigator, .eventNavigator, .twitNavigator').hide();
    }
  }
  
  
  if ($('#twitterFeed').length) {
    initCycle('#twitterFeed ul.feed', 'twitter');
  }
  if ($('#locationContent').length) {
    initCycle('#locationContent .slide', 'location');
  }
  if ($('.eventNavigator').length) {
     initCycle('.event .slide', 'event');
  }

	// Hover Fades
	$('.feature a, .eventMedia a, #pastEvents img, .locationPhotos li a, #psPhotos li a').hover(function(){
		$(this).stop().fadeTo("fast", 0.75);
	}, function(){
		$(this).stop().fadeTo("fast", 1);  
	});

	//  Video Icon
	$('.eventMedia a.video').prepend('<span></span>');

	// Overlay Links
	$.setupJMPopups({
		screenLockerBackground: "#fff",
		screenLockerOpacity: "0.7"
	});
	$('.stf').click(function(){
    $url = $(this).attr('rel');
    ///console.log($url);
		$.openPopupLayer({
			name: "overlay",
			url: '/stf.html?link='+$url,
			width: "100%",
			success: function () {
				$('#stfForm label').overlabel();
				$('#stfForm form').validate();
			}
		});
		return false;
	});
	
	$('.rsvp').click(function(){
		$.openPopupLayer({
			name: "overlay",
			url: '/rsvp.html',
			width: "100%",
			success: function () {
				$('#rsvpForm label').overlabel();
				$('#rsvpForm form').validate();
			}
		});
		return false;
	});
	
	$('.newsletter').click(function(){
		$.openPopupLayer({
			name: "overlay",
			url: '/newsletter-cc.html',
			width: "100%",
			success: function () {
				$('#newsForm label').overlabel();
				$('#newsForm form').validate();
			}
		});
		return false;
	});
	
	$('#stfForm .close, #rsvpForm .close, #newsForm .close').live('click', function(){
		$.closePopupLayer("overlay");
	});    

	$('.eventMedia li a, .locationPhotos li a, #psPhotos li a, .fancy').fancybox({
		padding: 20
	});
	$('#fancy_title a.close').click(function(){
		$.fn.fancybox.close();
	});

	
	// Contact  Form  / Comment Form
	$('#contactForm label, .commentsForm label').overlabel();
	$('#contactForm form, .commentsForm form').validate();

	// Select Validate
	var $select = $('#contactForm select'),
		$form = $select.parents('form');
	$select.change(function(){
		$(this).siblings('.error').remove();
		$(this).addClass('selected');
		if($select.val() == 'none') {
			$(this).removeClass('selected'); 
		} else {
			$(this).addClass('selected');  
		}
	});
	$form.submit(function(){
		if($select.val() == 'none') {
			$(this).removeClass('selected'); 
			if ($(this).siblings('.error').length) {
				return false;
			} else {
				$select.after('<label class="error">Please Select an Option.</label>');
			}
			return false;
		}
	});
	
	//Scrolling Sub-Nav
	$('#secondaryNav li:has(ul)').click(function(e) { e.preventDefault(); });
  $('#secondaryNav li ul li a').click(function(e) {
    
    e.preventDefault();
    
    var id = $(this).attr('href');
    var target = $(id);
    var offset = target.offset();
    
    $('html,body').stop(true,true).animate({
      scrollTop : offset.top
    }, 1000);
    
    return false;
    
  });
  
	/*
	// Location List Numbers
	var index = $('#locationList').index(this); 
	$('#locationList li').each(function(){
		var index = $(this).index(this);
		$(this).append('<span class="index">'+ index +'</span>');
	});
	*/
	
});
