jQuery(document).ready(function($){
  $('#needScript,.needScript').hide();
	$('.requiresScript').show();
  $('.hideReveal').each(function(){
      var el = $(this);
      el.height(el.height()).hide();
    });
  $('.hideRevealNext').click(function(){
      $(this).parent().next('.hideReveal').slideToggle();
      this.blur();
      return false;
    });
	var theHelperEl = 0
	  , theHelperTO = null
		, theHelper = $('#theHelper').bind('click', function(){
				window.clearTimeout(theHelperTO); $(this).stop().hide(); theHelperEl = 0; return false;
			});
	$('b.helper').each(function(){
		  var me = $(this);
		  me.after("<b class='helperMark'>?</b>").next().bind('click', function(){
		  	  if(this == theHelperEl){
		  	  	theHelper.trigger('click');
		  	  }else{
						var hh = theHelper.trigger('click').find('div').text(me.text()).end().height()
							, off = $(this).offset();
						theHelper.css({top:off.top-15-hh, left:off.left-200, opacity:1}).show();
						theHelperEl = this;
						theHelperTO = window.setTimeout(function(){ if(theHelperEl) theHelper.fadeOut(function(){ theHelper.trigger('click'); }); }
																						, 5000);
					}
					return false;
				});
		});
	$('div.photoCycle').each(function(i){
			var fx = (this.className.match(/fx_([a-zA-Z]+)/)||[])[1];
			$(this).cycle({fx:fx||'fade', timeout:6000, startingSlide:$('img',this).length-1, delay:i*1000});
		});
  if(typeof bookPreview == 'undefined'){
    ;//$('#voteForm input').each(function(){ this.disabled = false; });
  }else{ //disable all links and form fields...
    $('a[href!=#]').click(function(){ return false; });
    $('#voteForm,#searchForm').find('input,textarea,select').each(function(){ this.disabled = true; });
    var npv = $('<div id="runningPreview"><b>Running in Preview Mode...</b><br />Navigation and form completion are prohibited!<br />Disabled flags on database records are ignored!<br />(Note : this includes images!!)</div>')
		  , npvt;
    npv.appendTo('body');
		npvt = -1 * (npv.height() + 5);
		npv.css({top:npvt,visibility:'visible'}).animate({top:0}, 1000);
    window.setTimeout(function(){ npv.animate({top:npvt}, {duration:1500, complete:function(){ npv.remove(); }}); }, 6000);
  }
  //allow for a new captcha to be requested...
  $('#changeCaptcha').bind('click', function(e){
      $('img.fldCaptcha').get(0).src = 'include/captcha.php?cb='+(new Date()).getTime();
      $('#fcaptcha').each(function(){ this.focus(); });
      return false;
    }).show();
  //hover vertical menu...
  $('div.vertical_menu').hover( function(){ $(this).addClass('vmenu_hover'); }
                              , function(){ $(this).removeClass('vmenu_hover'); }
                              );
  //hover book info on section pages...
  var blih = $('#bliHover');
  if(blih){
    var blihPO = blih.parent().offset(), hPX = 0, hPY = 0, hOW = 0, hOH = 0, hMV = 0;
    $('div.bliImage img').attr({alt:'',title:''});
    $('#bookList').bind('mousemove', function(e){
        hPX = e.pageX; hPY = e.pageY;
        if(hMV){
          blih.css({top:hPY - blihPO.top - hOH, left:hPX - blihPO.left - hOW});
        }
      });
    $('div.bliImage img').attr({alt:'',title:''})
      .hoverIntent(
          function(e){
            blih.html($(this).parent().next().clone());
            hOW = blih.outerWidth()+10;
            hOH = blih.outerHeight()+10;
            hMV = 1;
            blih.css({top:hPY - blihPO.top - hOH, left:hPX - blihPO.left - hOW}).show();
          }
        , function(e){
            blih.hide();
            hMV = 0;
          }
        );
  }
  //finalists...
	$('#finalistList').each(function(){
			//tabs...
			var lists = $('.finalistList', this).css({zoom:1})
				, links = $('.finalistListSections').slice(1).remove().end().eq(0)
					.find('a').click(function(){
						var self = this;
						links.each(function(i, el){
								if((el == self) === $(el).hasClass('fliNoShow')){
									$(el).add(lists.eq(i)).toggleClass('fliNoShow');
								}
							});
						self.blur();
						//don't return false!
					})
				, animScroll = function(to){
						$.scrollTo(to, 500, {axis:'y'});
						return false;
					}
				, bookmark = window.location.href.split('#')[1] || ''
				, bookBookmark = bookmark ? $('#'+bookmark) : []
				, disp = 0
				, scrollTop = $('#finalistList').offset().top
				, voteClick = function(){
						var yes;
						$(this).closest('.finalistList').find('.finalistVote')
							.filter('.winningVote').not(this).add(this).toggleClass('winningVote');
						yes = $(this).hasClass('winningVote');
						$('#choiceForSection_' + $('input.winnerSection', this).val())
							.find('span').html(yes ? $('input.winnerName', this).val() : '&nbsp;')
							.next()[yes ? 'hide' : 'show']()
							.next().val(yes ? $('input.winnerBook', this).val() : '0')
							.siblings('a')[yes ? 'show' : 'hide']();
						this.blur();
						return false;
					}
				, fromChoices = function(){
						var v = $(this).val(), el;
						if(v && v !== '0'){
							el = $('.winnerBook', lists).filter(function(){ return $(this).val() == v; }).parent();
							if(el.length){
								voteClick.call(el[0]);
							}else{
								$(this).val(0);
							}
						}
					}
				;
			$('.finalistsToTop').click(function(){ return animScroll(scrollTop); });
			if(bookmark){
				bookmark = new RegExp('#' + bookmark.split('_')[0] + '$','');
				links.each(function(i){
						if(bookmark.test(this.href)){
							disp = i;
							return false;
						}
					});
			}
			lists.filter(function(i){ return (i !== disp); }).addClass('fliNoShow');
			links.filter(function(i){ return (i !== disp); }).addClass('fliNoShow');
			$('.finalistBookmark').remove();
			$('.choiceSwitchSection').each(function(i){
					var indx = i;
					$(this).click(function(){
							links.eq(indx).trigger('click');
							this.blur();
							//don't return false!
						});
				});
			if(bookBookmark.length){
				animScroll(bookBookmark);
			}
			//pre-select from choices table...
			$('#winningChoices input').each(fromChoices);
			//voting...
			$('.finalistVote', lists).click(voteClick);
			//removing vote...
			$('.choiceBookRemove').click(function(){
					fromChoices.call($(this).siblings('input').get(0));
					return false;
				});
		});
  //search submittal...
  var SFM = $('#searchForm').bind('submit', function(){
      var S = $('#searchBox').val(), V = {A:!!(S && S.length>2), B:!!$(':checked', this).length};
      $.each(V, function(k, v){
					var se = $('#searchError'+k);
					if(se.hasClass('searchError_show')){
						if(v){ se.removeClass('searchError_show').slideUp(); }
					}else if(!v){ se.addClass('searchError_show').slideDown(); }
        });
      return (V.A && V.B);
    });
  //search options...
  //IE6 compatibility for SELECT showing through DIV layers requires 'hiding' of SELECTs!
	var SX = {el:$('#searchExtend')};
	SX.selects = SX.el.find('select').css({visibility:'hidden'});
	SX.height = SX.el.height()-2;
	SX.top = SX.el.parent().height() - SX.height - 2;
	SX.el.css({top:SX.top, visibility:'visible'}).find('.searchError_show').slideDown();
  $('#searchOptions').bind('click', function(){
      var me = $(this);
      if(!me.hasClass('search_sliding')){
        var s = 'normal', tp = (me.hasClass('search_collapsed') ? '+=' : '-=')+SX.height+'px'
          , cb = function(c){
							if(c){
								if(!me.hasClass('search_collapsed')){
									SX.selects.css({visibility:'hidden'});
								}
							}else{
								if(me.hasClass('search_collapsed')){
									SX.selects.css({visibility:'visible'});
								}
								me.toggleClass('search_collapsed');
							}
							me.toggleClass('search_sliding')
						};
        cb(1);
        SX.el.animate({top:tp}, s, cb);
      }
      this.blur();
      return false;
    });
  $('#searchReset').bind('click', function(){
      SFM.find(':checkbox').each(function(){ this.checked = true; }).end()
         .find('select').val('').end()
				 .find('div.searchError_show').removeClass('searchError_show').slideUp();
      $('#searchOptions').trigger('click');
      return false;
    });
  //collapsible regions on search list...
  $('div.searchCollapse').each(function(){
      var me = $(this), pt = me.parent(), ht = [2, pt.height(), me.height()+2];
      me.bind('click', function(){
          if(ht[0]){
            var ce = ht[0];
            ht[0] = 0;
            pt.addClass('search_sliding')
              .animate( {height:ht[ce]}
                      , function(){ me.toggleClass('search_collapsed'); pt.removeClass('search_sliding'); ht[0] = (ce%2)+1; }
                      );
          }
        });
    });
  $('a.targetBlank').attr({target:'_blank'}); //XHTML compliance
  $('a.disableClick').bind('click',function(){ this.blur(); return false; });
});

