
// Onload When Document Ready
$(document).ready(function() {
  $('a[rel=external]').attr('target', '_blank');
  $('.equalize').equalHeights('true');
  $("img[src$=png],.pngfix").pngfix();
  //$(document).stickyfooter();
  $("div#nav > ul > li:not(:last),div#subnav > ul > li:not(:last), #headernav > li:not(:last), ul#footernav > li:not(:last)").append("<span class=\"spacer\">|</span>");
  $('.photocap > img[title]').each(function(){$(this).after("<p>"+this.title+"</p>").parent().css("width", $(this).width());});
  $('a.popup').live('click',function(){var data=$(this).metadata();var name=(data.name==null)?'popup':data.name;w=screen.availWidth;h=screen.availHeight;var leftPos=(w-data.width)/2,topPos=(h-data.height)/2;window.open($(this).attr('href'),name,'width='+data.width+',height='+data.height+',toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=yes,top='+topPos+',left='+leftPos);return false;});
  $('h4.styled-header + p').css("margin-top", "5px");
  $("a.faq-question").click(function() {
    $(this).next().toggle();
    return false;
  }).next().hide();

	$('.fancybox').prepend('<img src="/images/icons/plussign.png" style="right:-8px;top:-8px;position:absolute;">');
		
		
  if ($('#rotate').length){
    $.getScript('/javascript/jquery.cycle.all.min.js', function() {

		  	$('#rotate')
			  	.after('<div id="cyclenav-container"><div id="cyclenav">')
			  	.cycle({
			      speed:  5000,
			      timeout: 7000,
			  		fx:     'fade', 
			      speed:  'slow', 
			      next:   '#next', 
			      prev:   '#prev', 
			      pager:  '#cyclenav'
				  /*containerResize: false*/
			    });
          
        $('#masthead > div > div > a').each(function(i) {
          $(this).addClass('rotate-slide'+i);
        });

	    });
  }

/*  
	$('#pubIsFullArticle').change(function() {
	  $updateFilters();
	});

	$('#pubIsReviewArticle').change(function() {
	  updateFilters();
	});
*/

  if ($('a.fancybox,a.infobox,a.infobox2,a.requesteditor,a.requestbox,a.commentBox').length) {
    $.getScript(
      '/fancybox/jquery.fancybox-1.3.1.pack.js',
      function() {
        if ($('a.infobox').length) {
          $("a.infobox").fancybox({
            'height'	:		485,
            'width'		:		790,
            'type'		: 'iframe',
            'hideOnOverlayClick': false,
            'hideOnContentClick': false
          });
        }
        if ($('a.infobox2').length) {
          $("a.infobox2").fancybox({
            'height'	:		600,
            'width'		:		500,
            'type'		: 'iframe',
            'hideOnOverlayClick': false,
            'hideOnContentClick': false
          });
        }
        if ($('a.requesteditor').length) {
          $("a.requesteditor").fancybox({
            'height'	:		550,
            'width'		:		445,
            'type'		: 'iframe',
            'hideOnOverlayClick': false,
            'hideOnContentClick': false
          });
        }
        if ($('a.requestbox').length) {
          $("a.requestbox").fancybox({
            'height'	:		450,
            'width'		:		470,
            'type'		: 'iframe',
            'hideOnOverlayClick': false,
            'hideOnContentClick': false
          });
        }
        if ($('a.commentBox').length) {
          $("a.commentBox").fancybox({
            'type'		: 'iframe',
            'hideOnOverlayClick': false,
            'hideOnContentClick': false
          });
        }

        if ($('a.messageBox').length) {
          $("a.messageBox").fancybox({
            'type'		: 'iframe',
						'height'  : 500,
						'width'   : 450,
            'hideOnOverlayClick': false,
            'hideOnContentClick': false
          });
        }
				
        if ($('a.fancybox').length) {
          $("a.fancybox").fancybox({
            'hideOnContentClick': true,
            'titlePosition'  : 'inside'
          });
        }
        
        
				if($('a.vimeo_fancybox').length) {
	        $("a.vimeo_fancybox").click(function() {
							$.fancybox({
								'padding'		: 0,
								'autoScale'		: false,
								'transitionIn'	: 'none',
								'transitionOut'	: 'none',
								'title'			: this.title,
								'width'			: 475,
								'height'		: 277,
								'href'			: this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1'),
								'type'			: 'swf'
							});
					
							return false;
						});
      	}  
        
        
      }
    );
  }
  
	if($('#memberProfile').length) {
    $.getScript('/javascript/jquery.validate.min.js',
      function() {
        $('#memberProfile').validate({
					errorLabelContainer: $("#form_errors"),
					wrapper: 'li',
//					errorClass: "form_error",
					rules: {
								memFName: { required: true },
								memLName: { required: true },
								memLocation: { required: true },

								memPassword: {
									minlength: 4
								},
								memConfirmPassword: {
									minlength: 4,
									equalTo: "#memPassword"
								}
							},
							messages: {
								memPassword: {
									minlength: "Password must be at least 4 characters long"
								},
								memConfirmPassword: {
									minlength: "Password must be at least 4 characters long",
									equalTo: "Please enter the same password as above"
								}
							} 
				});
    });

		$('#memTitleShow').change(function() {
  		if($('#memTitleShow').attr('checked')){ 
				$('#titlePreview').html($('#memTitle').val()); 
				$('#titlePreview').removeClass('disabledText'); 
			} else { 
				// $('#titlePreview').html(''); 
				$('#titlePreview').addClass('disabledText'); 
			}
		});

		$('#memFNameShow').change(function() {
  		if($('#memFNameShow').attr('checked')){ 
				$('#firstPreview').html($('#memFName').val()); 
				$('#firstPreview').removeClass('disabledText'); 
			} else { 
				// $('#firstPreview').html(''); 
				$('#firstPreview').addClass('disabledText'); 
			}
		});

		$('#memCredentialsShow').change(function() {
  		if($('#memCredentialsShow').attr('checked')){ 
				$('#credPreview').html(', ' + $('#memCredentials').val()); 
				$('#credPreview').removeClass('disabledText'); 
			} else { 
				// $('#credPreview').html(''); 
				$('#credPreview').addClass('disabledText'); 
			}
		});

		$('#memTitle').bind('keyup', function () {
			$('#titlePreview').html($(this).val());
/*			} else {
				$('#titlePreview').html('');
			} */
		});

		$('#memTitle').bind('blur', function () {
			$('#titlePreview').html($(this).val());
/*			} else {
				$('#titlePreview').html('');
			} */
		});


		/* $(this).val().length &&  */
		$('#memFName').bind('keyup', function () {
			/* if($('#memFNameShow').attr('checked')) */ $('#firstPreview').html($(this).val());
		});

		$('#memLName').bind('keyup', function () {
			/* if($(this).val().length) */ $('#lastPreview').html($(this).val());
		});
		/* $(this).val().length && */
		$('#memCredentials').bind('keyup', function () {
			/* if( $('#memCredentialsShow').attr('checked')) */ if($(this).val().length) { $('#credPreview').html(', ' + $(this).val()); } else { $('#credPreview').html(''); }
		});


	}
	
  

	if($('#memberRegister').length) {
    $.getScript('/javascript/jquery.validate.min.js',
      function() {
        $('#memberRegister').validate({
					errorLabelContainer: $("#form_errors"),
					wrapper: 'li',
//					errorClass: "form_error",
					rules: {
								memFName: { required: true },
								memLName: { required: true },
								memLocation: { required: true },

								memPassword: {
									minlength: 4,
									required: true
								},
								memConfirmPassword: {
									minlength: 4,
									equalTo: "#memPassword"
								}
							},
							messages: {
								memPassword: {
									minlength: "Password must be at least 4 characters long"
								},
								memConfirmPassword: {
									minlength: "Password must be at least 4 characters long",
									equalTo: "Please enter the same password as above"
								}
							} 
				});
    });

		$('#memTitleShow').change(function() {
  		if($('#memTitleShow').attr('checked')){ 
				$('#titlePreview').html($('#memTitle').val()); 
				$('#titlePreview').removeClass('disabledText'); 
			} else { 
				// $('#titlePreview').html(''); 
				$('#titlePreview').addClass('disabledText'); 
			}
		});

		$('#memFNameShow').change(function() {
  		if($('#memFNameShow').attr('checked')){ 
				$('#firstPreview').html($('#memFName').val()); 
				$('#firstPreview').removeClass('disabledText'); 
			} else { 
				// $('#firstPreview').html(''); 
				$('#firstPreview').addClass('disabledText'); 
			}
		});

		$('#memCredentialsShow').change(function() {
  		if($('#memCredentialsShow').attr('checked')){ 
				$('#credPreview').html(', ' + $('#memCredentials').val()); 
				$('#credPreview').removeClass('disabledText'); 
			} else { 
				// $('#credPreview').html(''); 
				$('#credPreview').addClass('disabledText'); 
			}
		});

		$('#memTitle').bind('keyup', function () {
			$('#titlePreview').html($(this).val());
/*			} else {
				$('#titlePreview').html('');
			} */
		});

		$('#memTitle').bind('blur', function () {
			$('#titlePreview').html($(this).val());
/*			} else {
				$('#titlePreview').html('');
			} */
		});


		/* $(this).val().length &&  */
		$('#memFName').bind('keyup', function () {
			/* if($('#memFNameShow').attr('checked')) */ $('#firstPreview').html($(this).val());
		});

		$('#memLName').bind('keyup', function () {
			/* if($(this).val().length) */ $('#lastPreview').html($(this).val());
		});
		/* $(this).val().length && */
		$('#memCredentials').bind('keyup', function () {
			/* if( $('#memCredentialsShow').attr('checked')) */ if($(this).val().length) { $('#credPreview').html(', ' + $(this).val()); } else { $('#credPreview').html(''); }
		});
	}
	
/*
	if($('#loginForm').length) {
    $.getScript('/javascript/jquery.validate.min.js', function() {
       $('#loginForm').validate({
				errorLabelContainer: $("#form_errors"),
				wrapper: 'li',
			});
    });
	}		
*/	
	if($('#caseSubmit,#requestEditor,#loginForm,#requestMeeting,#contactForm,#topicPost').length) {
    $.getScript('/javascript/jquery.validate.min.js', function() {
       $('#caseSubmit,#requestEditor,#loginForm,#requestMeeting,#contactForm,#topicPost').validate({
				errorLabelContainer: $("#form_errors"),
				wrapper: 'li'
			});
    });
	}		

	if($('#commentForm').length) {
    $.getScript('/javascript/jquery.validate.min.js', function() {
       $('#commentForm').validate({
					rules: {
								comText: {
									minlength: 10,
									required: true
								}
							}
			});
    });
	}		
	
	// <p><span id="titlePreview">[Title]</span> <span id="firstPreview">[First Name]</span> [Last Name]<span id="credPreview">, [Credentials]</span></p>
  
  $('#expandLink img').toggle(
    function(){
      if($('.author-list').length){$('.author-list').show();}
      if($('.mainRow-list').length){$('.mainRow-list').show();}
      $(this).attr('src','/images/buttons/collapse_all.jpg');
      
    }, function() {
      if($('.author-list').length){$('.author-list').hide();}
      if($('.mainRow-list').length){$('.mainRow-list').hide();}
      $(this).attr('src','/images/buttons/expand_all.jpg');
    });
  
  $("div.author-list").each(function() {
    $(this).prev("a.author").append(" (" + $(this).children("div").length + ")");
  });
  
  $("div..mainRow-list").each(function() {
    $(this).prev("a.mainRow").append(" (" + $(this).children("div").length + ")");
  });
  
});



