$(document).ready(function(){
	if(!$('.searchbox input').length){
		$('.searchbox').attr("style","border:0px;");
	}
	/*if($('.windowed').length){
		if($('.windowed').height() < $(document).height()){
			alert($('.windowed').height());
			theight = ($(document).height() - 310 )+'px';
			$('.windowed').attr("style","height :"+theight);
			$('.colorpane').attr("style","height :"+theight);
		}
	}*/
	$(".inst").each(function(i) {
	    var beginStyle = $(this).attr("style");
		//$(this).attr("style", "color: #bbb;" + beginStyle).val($(this).attr("title"));
		$(this).attr("style", "color: #bbb;" + beginStyle);

	    $(this).bind("focus", function(e) {
	        if ($(this).val() == $(this).attr("title")) {
	            $(this).val("");
	            $(this).attr("style", beginStyle);
	        }
	    });
	    $(this).bind("blur", function(e) {
	        if ($(this).val() == "") {
	            $(this).attr("style", "color: #bbb;" + beginStyle).val($(this).attr("title"));
	        }
	    });
	});
	$('#searchform').submit(function() {
		if($('#search').attr('value')=='search books or authors'){
			$('#search').attr('value','');
		}
	});
	$('#subsearch').click(function(){
		$('#searchform').submit();
		return false;
	});
	$('#clearform').click(function(){
		window.setTimeout('location.replace("?page_id=93")', 0);
	});
	var original_height = $('#contents').height();
	$(window).resize( stretch_portal_content );
	$(window).ready( windower );
	$(window).resize( windower );
	$(window).bind('load', function() {
	     stretch_portal_content();
	});
	function stretch_portal_content() {
		/*$('.book_box').html(original_height+' '+$(document).height());*/
		if( ($(document).height()-40) > original_height ) {
			$( '#contents' ).height(
				$(document).height()
			);
			$( '.filter' ).height(
				$(document).height()
			);
			original_height = $(document).height();
		}
		if( $( '#contents' ).height() > $(document).height() ) {
			$( '#contents' ).height(
				$(document).height()
			);
			$( '.filter' ).height(
				$(document).height()
			);
		}
		if( $( '#contents' ).height() > $(window).height() ) {
			ww = $(document).width() + $.scrollbarWidth();
			$('body').css('background-position',ww+'px 0px');
		}
	}
	
	subpage = self.document.location.hash.substring(2);
	var tabber = function(id) {
		path = window.location.pathname;
		add = "#a" + id;
		if(path.match(/issues/)){
			var exclude = 3;
		}else{
			var exclude = 4;
		}
		if(id < exclude){
			for(i=1;i<exclude;i++){
				hiders = "#tab"+i;
				hilite = "#a"+i;
				$(hilite).removeClass("selected");
				if(i==id){
					jQuery(hiders).css("display","block");
				}else{
					jQuery(hiders).css("display","none");
				}
			}
			$(add).addClass("selected");
		}
		stretch_portal_content();
		windower();
	}

	$("#fancy,.fancy").click(function() {
		href = 'http://docs.google.com/gview?url=http://www.fenceportal.org'+$(this).attr('href')+'&embedded=true';
		$.fancybox({
			'href': href,
			'width' : '75%',
			'height': '95%',
			'autoScale' : false,
			'transitionIn' : 'none',
			'transitionOut' : 'none',
			'type' : 'iframe'
		});
		return false;
	});

	if(subpage.match(/\d{1,2}/)){
		tabr = tabber(subpage);
	}
	$(".subnav_nav a").bind('click',function(event){
		id = $(this).attr('id').substr(1);
		tabr = tabber(id);
	});
	
	$(".a1,.a2").bind('click',function(event){
		id = $(this).attr('class').substr(1);
		tabr = tabber(id);
	});
	
	function windower() {
		/* set windowed pages to show through to correct portion of background image*/
		if($(".windowed").length!=0){
			var offset = $(".windowed").offset();
			/*thisParent = $(".windowed").parent();
			var parentTop = thisParent.offset().top;
			topp = offset.top - parentTop;
			alert(topp+' '+offset.top);*/
			var actualImage = new Image();
			actualImage.src = $('body').css('background-image').replace(/"/g,"").replace(/url\(|\)$/ig, "");
			$(actualImage).load(function() {
				var offleft = actualImage.width - ($(window).width() - offset.left);
				$(".windowed").css('background-position', '-'+offleft+'px '+'-'+offset.top+'px');
			});
		}
	}
	
	$('.side_heads a').bind('click',function(event){
		$(this).parent().next().toggle();
		stretch_portal_content();
		windower();
	});
});
/*!
 * jQuery Scrollbar Width v1.0
 * 
 * Copyright 2011, Rasmus Schultz
 * Licensed under LGPL v3.0
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 */
(function($){

$.scrollbarWidth = function() {
  if (!$._scrollbarWidth) {
     var $body = $('body');
    var w = $body.css('overflow', 'hidden').width();
    $body.css('overflow','scroll');
    w -= $body.width();
    if (!w) w=$body.width()-$body[0].clientWidth; // IE in standards mode
    $body.css('overflow','');
    $._scrollbarWidth = w;
  }
  return $._scrollbarWidth;
};

})(jQuery);
