$(document).ready(pb_init);
function pb_init(){
	pb_init_facets();
	pb_init_popups();
	pb_init_user();
}
function pb_init_facets() {
	$("div.pbfacetarea").bind("mouseleave", function(){pb_hide(this.id,this.id,"");return false;});
	$('.pbfacetlink_S').click(function(){pb_hide(this.id,'pbfacet_S','subjects');return true;});
	$('.pbfacetlink_A').click(function(){pb_hide(this.id,'pbfacet_A','authors');return true;});
	$('.pbfacetlink_Y').click(function(){pb_hide(this.id,'pbfacet_Y','year');return true;});
	$('#pbfacetcloselink_S').click(function(){pb_hide(this.id,'pbfacet_S');return false;});
	$('#pbfacetcloselink_A').click(function(){pb_hide(this.id,'pbfacet_A');return false;});
	$('#pbfacetcloselink_Y').click(function(){pb_hide(this.id,'pbfacet_Y');return false;});
	$('#pbfacetlaunch_S').click(function(){pb_show('facetlauncharea','pbfacet_S');return false;});
	$('#pbfacetlaunch_A').click(function(){pb_show('facetlauncharea','pbfacet_A');return false;});
	$('#pbfacetlaunch_Y').click(function(){pb_show('facetlauncharea','pbfacet_Y');return false;});
}
function pb_init_popups() {
	$("#pbcpopcloser").live("click",function(){$('#pbcpopwrap').hide('fast');return false;});
	$(".pbsummarylink").click(function(){ 
		var toLoad = $(this).attr('href'); 
		$('#pbcpopdata').html('<div>LOADING...</div>');
		pb_show_ajax($(this), $('#pbcpopwrap'), $('#pbcpopdata'));
		$('#pbcpopwrap').show('fast');
		$('#pbcpopdata').load(toLoad,'');		
		return false;
	});
	$(".pbtreviewslink").click(function(){ 
		var toLoad = $(this).attr('href'); 
		$('#pbcpopdata').html('<div>LOADING...</div>');
		pb_show_ajax($(this), $('#pbcpopwrap'), $('#pbcpopdata'));
		$('#pbcpopwrap').show('fast');
		$('#pbcpopdata').load(toLoad,'');		
		return false;
	});
}
function pb_init_user() {changepbUserSettings();}

function pbWinOpen(file,win,w,h,left,top) {
	Win=window.open(file,win,'width='+w+',height='+h+',left='+left+',top='+top+',location=0,directories=0,status=0,toolbar=0,menubar=0,scrollbars=1,resizable=1');
	Win.focus();
}
function pbPrintThis(sURL) {
	var sTrg = sURL;
	if (sTrg.length == 0) {sTrg = $(this).attr('href')}
	var printWin = window.open(sTrg,'_blank','height=400,width=600');
    	printWin.print();
}

function pbaddreviewfn(sURL){ 
	$('#pbaddreviewfrmid').submit(function() {
		var iMiss = 0;
		for (i=5;i<=9;i++) {
			if ($('#pbgff'+i).attr('value').length == 0) {$('#reviewwarnf'+i).show('fast'); iMiss++;} else {$('#reviewwarnf'+i).hide('fast');}
		}
		if (iMiss == 0) {
			var sGF="&GENFIELD", sData="PBFORMTYPE="+$('#pbgff1').attr('value') +"&SQS=" + $('#pbgff3').attr('value') + "&TITLEID=" + $('#pbgff2').attr('value') +"&TPART=" + $('#pbgff4').attr('value')+sGF+"1=" + $('#pbgff5').attr('value')+ sGF+"2="+$('#pbgff6').attr('value')+sGF+"3=" + $('#pbgff7').attr('value')+sGF+"4=" + $('#pbgff8').attr('value')+sGF+"5=" + $('#pbgff9').attr('value');
			$.ajax({
			type: "POST",
			url: sURL,
			data: sData,
			success: function(myData) {
				$('#pbreviewadd').hide();
				$('#pbreviewdone').html(myData);				
				$('#pbreviewdone').show(function() {setTimeout(function(){$('#pbreviewdone').fadeOut();$('#pbcpopwrap').hide('fast');},5000);});
			}
			
			}); 
		}
		return false;
	});
}
function pbstars(iNum) {for (i=1;i<=5;i++){if (i <= iNum) {$('#star' + i).removeClass('stars1').addClass('stars2');}else{$('#star' + i).removeClass('stars2').addClass('stars1');}$('#pbgff8').attr('value', iNum);}}

var sTD = 'titledata', sHL = 'pbhighlight', sC = 'pbuserset', sA1 = 'pbbaid_', sA2 = '#pbbaid_', sD1 = 'pbbdid_', sD2 = '#pbbdid_', sN = '00000000000000000000000000000000';

function setBasketItems(lM, strId) {
	var oA, j;
	if (lM==1) {$(sA2 + strId).hide(); $(sD2 + strId).show();}	
	if (lM==2) {$(sD2 + strId).hide(); $(sA2 + strId).show();}
	if (lM==3) {
		var orgstr;
		if (strId.length>0) {orgstr=strId;} else {orgstr = $('#pbbasketids').text();}
		var splstr = orgstr.split(';');
		if (splstr.length == 0 || orgstr.length == 0) {
			$('.pbbasketfunc').each(function(myindex1, obj1) {
				$(obj1).children('div').each(function(myindex, obj) {
				oA = obj.id;
				if (oA.substr(0, 7) == sA1) 	{
					$(obj).show();
				} else if (oA.substr(0, 7) == sD1) {
					$(obj).hide();
				}
			});
			});
		}
		else
		{
			$('.pbbasketfunc').each(function(myindex1, obj1) {
				$(obj1).children('div').each(function(myindex, obj) {
				
				oA = obj.id;
				if (oA.substr(0, 7) == sA1) 	{
					for (j = 0; j < splstr.length; j++) {
						if (sA1+splstr[j] == oA) {$(obj).hide(); break;}
					}

				} else if (oA.substr(0, 7) == sD1) {
					for (j = 0; j < splstr.length; j++) {
						if (sD1+splstr[j] == oA) {$(obj).show(); $(sA2 + splstr[j]).hide(); break;} else {$(obj).hide();}
					}
				}
			});
			});

		};
	}
}

function getBasketItems(params) {
	$.get("src", params, function(data) 
	{
		if( $('#basketcontent2').length) {$('#basketcontent2').empty();$('#basketcontent2').append(data);}
		else {$('#basketcontent').empty();$('#basketcontent').append(data);}

		var strids='';
		var divstr=$('#pbbasketids').text();
		if (divstr.length) {
			var jsondata = jQuery.parseJSON(divstr);
			for (var i = 0; i < jsondata.pbbasketids.ids.length; i++){
				strids = jsondata.pbbasketids.ids[i] + ';';
			}
		} else {divstr='';}
		if( $('#basketcontent2').length) {setBasketItems(3,strids);} else {setBasketItems(3,strids);}
		
	});
}
function setPBB(d, strTempId, nro, tId) {var pbbr=$('#pbbasketresponse').text();$('#' + strTempId).html(pbbr);if($('#basketcontent2').length) {$('#basketcontent2').empty();$('#basketcontent2').append(d);} else {$('#basketcontent').empty();$('#basketcontent').append(d);}setBasketItems(nro, tId);}
function changepbUserSettings() {
	var bisquit = checkpbbisquit(getCookie(sC));
	if (getpbSUS(31, bisquit) != "1") {
		if (getpbSUS(0, bisquit) == "2") {changeDivState(sTD, false, false,0);} else {changeDivState(sTD, true, false,0);}
		if (getpbSUS(1, bisquit) == "2") {changeHighlights(false, false);} else {changeHighlights(true, false);}
		if (getpbSUS(2, bisquit) == "2") {changeDivState("pbresultsub", false, false,2);} else {changeDivState('pbresultsub', true, false,2);}
	}
}
function checkpbbisquit(bq) {
	if (!bq) {return sN;} else { 
		if (bq.length == 32) {return bq;} else {return sN;}
	}
}
function setpbSUS(index, value, bq) {return setCharAt(checkpbbisquit(bq), index+1, value);}
function getpbSUS(index, bq) {return checkpbbisquit(bq).charAt(index);}
function changeDivState(itemname, bVisible, bStore, iStoreVal) {
	if (bVisible) {
		$("." + itemname).show();
		if (itemname == sTD) {$('#pbdyn_data_show').hide();$('#pbdyn_data_hide').show();} else {$('#pbdyn_head_show').hide();$('#pbdyn_head_hide').show();}
	} else	{
		$("." + itemname).hide();
		if (itemname == sTD) {$('#pbdyn_data_show').show();$('#pbdyn_data_hide').hide();} else {$('#pbdyn_head_show').show();$('#pbdyn_head_hide').hide();}
	}
	if (bStore) {if (bVisible) {return setCookie(sC, setpbSUS(iStoreVal, "1", getCookie(sC)), 180, "/");} else {return setCookie(sC, setpbSUS(iStoreVal, "2", getCookie(sC)), 180, "/");}}
}


function chgHS(itemname, itemnameb) {$("span." + itemname).toggleClass(itemnameb);$("span." + itemname).toggleClass(itemname);}
// change
function changeHighlights(bVisible, bStore) {
	if (bVisible)	{
		chgHS(sHL + '1b', sHL + '1');
		chgHS(sHL + '2b', sHL + '2');
		chgHS(sHL + '3b', sHL + '3');
		chgHS(sHL + '4b', sHL + '4');
		$('#pbdyn_hglt_show').hide();$('#pbdyn_hglt_hide').show();
		} else	{
		chgHS(sHL + '1', sHL + '1b');
		chgHS(sHL + '2', sHL + '2b');
		chgHS(sHL + '3', sHL + '3b');
		chgHS(sHL + '4', sHL + '4b');
		$('#pbdyn_hglt_show').show();$('#pbdyn_hglt_hide').hide();
	}
	if (bStore) {
		if (bVisible)	{
			setCookie(sC, setpbSUS(1, "1", getCookie(sC)), 180, "/");
		} else	{
			setCookie(sC, setpbSUS(1, "2", getCookie(sC)), 180, "/");
		}
	}
}
function setCharAt(str, index, ch) {
   return str.substr(0, index - 1) + ch + str.substr(index);
}
function pb_show_ajax(useThis,showThis,contentThis){

	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
    	var pbarea = $(useThis);
	var pboffset = pbarea.offset();
	var clickElementy = pboffset.top - 40;			
	if(((w - pboffset.left)>475)){
		var clickElementx = pboffset.left;
	}else{
		var clickElementx = 0;
		showThis.css({width: document.body.clientWidth});
	}
	showThis.css({left: clickElementx+"px", top: clickElementy+"px"});
}
function pb_show(linkId,newid){
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
    	var pbarea = $('#'+linkId);
	var pboffset = pbarea.offset();
	var hasArea = w - pboffset.left;
	var clickElementy = pboffset.top - 10;
	var clickElementx = pboffset.left;
	if (!(hasArea>((400*1)+75))){		
		var clickElementx = 0;
	}	
	$('#'+newid).css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#'+newid).show(200);

}
function pb_hide(linkId,newid,title){
	$('#'+newid).hide(200);
}
/*********************************************************************************
  dw_cookies.js - cookie functions for www.dyn-web.com
  Recycled from various sources 
**********************************************************************************/

// Modified from Bill Dortch's Cookie Functions (hidaho.com) 
// (found in JavaScript Bible)
function setCookie(name,value,days,path,domain,secure) {
  var expires, date;
  if (typeof days == "number") {
    date = new Date();
    date.setTime( date.getTime() + (days*24*60*60*1000) );
		expires = date.toGMTString();
  }
  document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

// Modified from Jesse Chisholm or Scott Andrew Lepera ?
// (found at both www.dansteinman.com/dynapi/ and www.scottandrew.com/junkyard/js/)
function getCookie(name) {
  var nameq = name + "=", c_ar = document.cookie.split(';');
  for (var i=0; i<c_ar.length; i++) {
    var c = c_ar[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameq) == 0) return unescape( c.substring(nameq.length, c.length) );
  }
  return null;
}

// from Bill Dortch's Cookie Functions (hidaho.com) 
function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
