/* -------------------------------------------------- *
 * ToggleVal Plugin for jQuery                        *
 * Version 1.0                                        *
 * -------------------------------------------------- *
 * Author:   Aaron Kuzemchak                          *
 * URL:      http://kuzemchak.net/                    *
 * E-mail:   afkuzemchak@gmail.com                    *
 * Date:     8/18/2007                                *
 * -------------------------------------------------- */
jQuery.fn.toggleVal = function(focusClass) {
  this.each(function() {
    $(this).focus(function() {
      // clear value if current value is the default
      if($(this).val() == this.defaultValue) { $(this).val(""); }
      // if focusClass is set, add the class
      if(focusClass) { $(this).addClass(focusClass); }
    }).blur(function() {
      // restore to the default value if current value is empty
      if($(this).val() == "") { $(this).val(this.defaultValue); }
      // if focusClass is set, remove class
      if(focusClass) { $(this).removeClass(focusClass); }
    });
  });
}

$(document).ready(function() {
  // Append a new class when search box gets focus
  $("div#ui-header fieldset .searchText").toggleVal("active");
  $("div#eScan fieldset .searchText").toggleVal("active");
  $("div#eScan fieldset table#create-abstract-table tr td .pubdate").toggleVal("active");

  //Navigation menu state selection
  var pathOrig = location.pathname.substring(0);
  var path = pathOrig.toLowerCase();  // convert path to all lowercase for comparison with locations in mainmenu.shtml

// Use parent path for all 4th tier pages
  var pathparts = new Array();
  pathparts = path.split('/');


// **************** testing by KJP *******************
	labeledpathparts = new Array();
	labeledpathparts.root = pathparts[0]; //     + "/" + pathparts[1];
	for (i=1; i<=(pathparts.length-1);i++) { // when you remove the zkjp folder, make this .length-1
		labeledpathparts["level"+i] = pathparts[i]   // when you remove the zkjp folder, make this: =pathparts[i]
	}

	if (path) {
		// Test to see if this exact page is listed in the main menu
		if ( $('#sidebar-navigation a[href="' + path + '"]').length ) {
			// if it is, then highlight it with the at/active classes, and its parents with the active class
			$('#sidebar-navigation a[href="' + path + '"]').addClass('at');
			$('#sidebar-navigation a[href="' + path + '"]').parents('li').addClass('active');
			$('#sidebar-navigation a[href="' + path + '"]').parent('li').addClass('at');
		} else {
			// if it's not, then find the parent directories, and highlight as appropriate
			var fauxpath = labeledpathparts.root + '/' + labeledpathparts.level1 + '/';
			
			switch(labeledpathparts.level1) {
// *********** ABOUT ************
				case "about":
					if (labeledpathparts.level2 == "newsletter") {
						fauxpath = fauxpath + labeledpathparts.level2 + '/';
					}
					if (labeledpathparts.level2 == "infoform.shtml") {
						fauxpath = fauxpath + 'contact.shtml';
					}
					var A = labeledpathparts.level2;
					var B = A.substring(0,7);
					if (B == "careers") {
						fauxpath = fauxpath + 'careers.shtml';
					}

					break;
// *********** SCAN ************
				case "scan":
					var A = labeledpathparts.level2;
					var B = A.substring(0,7);
					if (B == "process") { fauxpath = fauxpath + 'process.shtml'; }
					
					if ((labeledpathparts.level2 == "soc") || (labeledpathparts.level2 == "patterns")) {
						fauxpath = fauxpath + 'signals.shtml';
					}
					if (labeledpathparts.level2 == "mtgsynopses") {
						fauxpath = fauxpath + 'synopses.shtml';
					}
					
					if ((labeledpathparts.level2 == "scanmonthly") || (labeledpathparts.level2 == "scanmonthly.shtml") || (labeledpathparts.level2 == "insightsums")) {
						fauxpath = fauxpath + 'clientaccess.shtml';
					}

					break;

// *********** eScan ************
				case "escan-bin":
				  fauxpath = '/escan-bin/4disapi.dll/escan/index.shtml';

					break;

// *********** EXPLORER ************
				case "explorer":
					if (labeledpathparts.level2 == "thismonth.shtml") {
						fauxpath = fauxpath + 'techlist.shtml';
					} else if (labeledpathparts.level2 == "abouttm") {
						fauxpath = fauxpath + 'techmap.shtml';
					} else {
						var techAbbrev = "";
						var A = labeledpathparts.level2;

						if (A.length<=4) {   // no tech abbreviation is going to be longer than 4 letters; we're in a subfolder
							techAbbrev = A; 
						} else {
							var B = A.substring(A.indexOf("-"),A.length);
							if (B == "-techmap.shtml") {   // it's a techmap ToC page; grab the tech
								techAbbrev = A.substring(0,A.indexOf("-"));
							} else {
								var C = A.substring(A.indexOf("."),A.length);
								if (C == ".shtml") {   // it's a tech home page; grab the tech
									techAbbrev = A.substring(0,A.indexOf("."));
								}
							}
						}
						
						// check to see if the techAbbrev is an old or changed technology
						switch(techAbbrev) {
							// changed techs force menu to highlight the parent technology
							case "cer": techAbbrev = 'nc'; break;
							case "cp": techAbbrev = 'oe'; break;
							case "gw": techAbbrev = 'kmt'; break;
							case "kbs": techAbbrev = 'ai'; break;
							case "md": techAbbrev = 'mc'; break;
							case "mmc": techAbbrev = 'nc'; break;
							case "osc": techAbbrev = 'op'; break;
							case "pb": techAbbrev = 'pp'; break;
							case "pi": techAbbrev = 'ped'; break;
							case "ve": techAbbrev = 'vw'; break;
							// discontinued techs force menu to highlight "Technoloyg List"
							case "case": techAbbrev = 'techlist'; break;
							case "cbse": techAbbrev = 'techlist'; break;
							case "dtf": techAbbrev = 'techlist'; break;
							case "foc": techAbbrev = 'techlist'; break;
							case "fos": techAbbrev = 'techlist'; break;
							case "gen": techAbbrev = 'techlist'; break;
							case "hdis": techAbbrev = 'techlist'; break;
							case "hpc": techAbbrev = 'techlist'; break;
							case "ic": techAbbrev = 'techlist'; break;
							case "mvs": techAbbrev = 'techlist'; break;
							case "ndte": techAbbrev = 'techlist'; break;
							case "nn": techAbbrev = 'techlist'; break;
							case "nt": techAbbrev = 'techlist'; break;
							case "oot": techAbbrev = 'techlist'; break;
							case "ost": techAbbrev = 'techlist'; break;
							case "pc": techAbbrev = 'techlist'; break;
							case "rp": techAbbrev = 'techlist'; break;
							case "sa": techAbbrev = 'techlist'; break;
							case "si": techAbbrev = 'techlist'; break;
							case "sr": techAbbrev = 'techlist'; break;
							case "st": techAbbrev = 'techlist'; break;
							case "ulsi": techAbbrev = 'techlist'; break;
							case "ws": techAbbrev = 'techlist'; break;
							case "ngt": techAbbrev = 'techlist'; break;
						} //switch
						fauxpath = fauxpath + techAbbrev + '.shtml';
					}
					
					break;
// *********** VWW ************
				case "vww":
					if ((labeledpathparts.level2 == "rptsums") || (labeledpathparts.level2 == "reports")) {
						fauxpath = fauxpath + 'reports.shtml';
					}
					if (labeledpathparts.level2 == "meetings") {
						fauxpath = fauxpath + 'meetings.shtml';
					}
					if (labeledpathparts.level2 == "viewpoints") {
						fauxpath = fauxpath + 'vwviewpoints.shtml';
					}
					break;
// *********** VWC ************
				case "vwc":
					if ((labeledpathparts.level2 == "rptsums") || (labeledpathparts.level2 == "reports")) {
						fauxpath = labeledpathparts.root + '/vww/' + 'reports.shtml';
					}
					if (labeledpathparts.level2 == "meetings") {
						fauxpath = labeledpathparts.root + '/vww/' + 'meetings.shtml';
					}
					if (labeledpathparts.level2 == "viewpoints") {
						fauxpath = labeledpathparts.root + '/vww/' + 'vwviewpoints.shtml';
					}
					break;
// *********** CFD ************
				case "cfd":
					if (labeledpathparts.level2 == "newsletter") {
						fauxpath = fauxpath + 'newsletter.shtml';
					}
					if ((labeledpathparts.level2 == "mr.shtml") || (labeledpathparts.level2 == "presentations.shtml") || (labeledpathparts.level2 == "tocs") || (labeledpathparts.level2 == "98stdmm") || (labeledpathparts.level2 == "mrsummaries") || (labeledpathparts.level2 == "presentationsums")) {
						fauxpath = fauxpath + 'mmpubs.shtml';
					}
					if ((labeledpathparts.level2 == "riia") && (labeledpathparts.level3 == "public")) {
						fauxpath = fauxpath + 'riia/public/';
					}
					if ((labeledpathparts.level2 == "riia") && (labeledpathparts.level3 == "members")) {
						fauxpath = fauxpath + 'riia/members/';
					}
					if (labeledpathparts.level3 == "srs.shtml") {
						fauxpath = fauxpath + 'customsolutions.shtml';
					}
					
					var A = labeledpathparts.level2;
					var B = A.substring(0,2);
					if (B == "mm") {
						fauxpath = fauxpath + 'macromonitor.shtml';
					}
					var C = A.substring(0,6);
					if (C == "2008mm") {
						fauxpath = fauxpath + 'macromonitor.shtml';
					}
					if (C == "2010mm") {
						fauxpath = fauxpath + '2010mm.shtml';
					}
					
					break;
// *********** VALS ************
				case "vals":
				  if (labeledpathparts.level2 == "survey.shtml") { fauxpath = fauxpath + 'presurvey.shtml'; }
				  if (labeledpathparts.level2 == "demobehav.shtml") { fauxpath = fauxpath + 'about.shtml'; }
				  if (labeledpathparts.level2 == "academics") { fauxpath = fauxpath + 'academics.shtml'; }
					if (labeledpathparts.level2 == "free") { fauxpath = fauxpath + 'store/free.shtml'; }
					if (labeledpathparts.level3 == "usconsumers") { fauxpath = fauxpath + 'store/usconsumers/'; }
					if (labeledpathparts.level2 == "summaries") { fauxpath = fauxpath + 'licensee.shtml'; }
					if (labeledpathparts.level2 == "geovals") { fauxpath = fauxpath + 'geovals/'; }
					
					break;
				
// *********** GeoVALS and other scripts ************
				case "cgi-bin":
				  if ((labeledpathparts.level4 == "w_geovalspurchase01") || (labeledpathparts.level4 == "w_geovalspurchase02") || (labeledpathparts.level4 == "w_geovalspurchase03")) { fauxpath = pathparts[0] + '/vals/geovals/'; }
					
					break;

//				default:
//					alert("kjp testing: case statement default");
//					break;
			}
			$('#sidebar-navigation a[href="' + fauxpath + '"]').addClass('active faux-at');
			$('#sidebar-navigation a[href="' + fauxpath + '"]').parents('li').addClass('active');
			$('#sidebar-navigation a[href="' + fauxpath + '"]').parent('li').addClass('at');

			
		}
	
	
	} //end if (path)



// ******************** end testing by KJP ************************


//  if (pathparts.length >= 5 ) {
//       path = "/" + pathparts[1] + "/" + pathparts[2] + "/";
//   }
//  if (path) {
//    $('#sidebar-navigation a[href="' + path + '"]').addClass('at');
//    var clsName   = $('#sidebar-navigation a[href="' + path + '"]').parents('li').attr('class');
//    var liParents = $('#sidebar-navigation a[href="' + path + '"]').parents('li').length; // KJP: what is this for???
    
    //alert( liParents );
    //$('#sidebar-navigation a[href="' + path + '"]').parents('li').addClass(clsName + '-active');
//    $('#sidebar-navigation a[href="' + path + '"]').parents('li').addClass('active');
//    $('#sidebar-navigation a[href="' + path + '"]').parent('li').addClass('at');

//    $("body").addClass(path + '___' + pathparts); // KJP: what is this for???
		//alert(path + '___' + pathparts);
//  }


//following snipped removed by KJP
  // Find external links and open them in a new window
//  $("a:not([@href*=sric-bi.com])[@href^='http'], a[@href$='.pdf'], a.pdf, a.pdf-secure").addClass("ExternalLink").click( function(){
//    window.open( $(this).attr('href') );
//    return false;
//  });
  
  // Anchors (CSS will add an icon for all anchors)
  $("a[href^='#']").addClass("anchor");




  $("#archivedPubs .hide").show();
  $("#archivedPubs .archived-pubs").hide();
  $("#archivedPubs .hide a").toggle(
    function(){
      $(this).parents(".hide").next(".archived-pubs").show(1000);
			$(this).empty().text('Hide archive [ - ]');   // kjp added
    },
    function(){
      $(this).parents(".hide").next(".archived-pubs").hide();
			$(this).empty().text('View archive [ + ]');   // kjp added
    }
  );

// begin added by KJW for VALS licensee page
  $("#archivedPubs1 .hide").show();
  $("#archivedPubs1 .archived-pubs").hide();
  $("#archivedPubs1 .hide a").toggle(
    function(){
      $(this).parents(".hide").next(".archived-pubs").show(1000);
			$(this).empty().text('Hide archive [ - ]');   // kjp added
    },
    function(){
      $(this).parents(".hide").next(".archived-pubs").hide();
			$(this).empty().text('View archive [ + ]');   // kjp added
    }
  );

  $("#archivedPubs2 .hide").show();
  $("#archivedPubs2 .archived-pubs").hide();
  $("#archivedPubs2 .hide a").toggle(
    function(){
      $(this).parents(".hide").next(".archived-pubs").show(1000);
			$(this).empty().text('Hide archive [ - ]');   // kjp added
    },
    function(){
      $(this).parents(".hide").next(".archived-pubs").hide();
			$(this).empty().text('View archive [ + ]');   // kjp added
    }
  );

  $("#archivedPubs3 .hide").show();
  $("#archivedPubs3 .archived-pubs").hide();
  $("#archivedPubs3 .hide a").toggle(
    function(){
      $(this).parents(".hide").next(".archived-pubs").show(1000);
			$(this).empty().text('Hide archive [ - ]');   // kjp added
    },
    function(){
      $(this).parents(".hide").next(".archived-pubs").hide();
			$(this).empty().text('View archive [ + ]');   // kjp added
    }
  );
// end added by KJW




  $(".pub-group h3:not(:first)")
		.wrapInner('<a href="#" class="more"></a>')
//		.append('<small style="color:#999;top:0;right:1em;position:absolute;cursor:help" title="Toggle view">Click to expand [ + ]</small>');
		.append('<small title="Toggle view">Click to expand [ + ]</small>');
  $(".pub-group h3:not(:first)").addClass('collapsed');
  $(".pub-group h3:not(:first)").next("ul").hide();
//  $(".pub-group h3:not(:first)").css({cursor: "pointer", borderTop: "1px solid #821616"}).toggle(
  $(".pub-group h3:not(:first)").css({cursor: "pointer"}).toggle(
    function(){
      $(this).removeClass('collapsed').addClass('expanded');
      $(this).next("ul").show(1000);
      $(this).children('small').empty().text('Click to collapse [ - ]');
    },function(){
      $(this).removeClass('expanded').addClass('collapsed');
      $(this).next("ul").hide();
      $(this).children('small').empty().text('Click to expand [ + ]');
    }
  );
  



  $('.pub-group h3:eq(1)').parent().before('<div class="tool-expandcollapseall"><a href="#">Expand all</a> | <a href="#">Collapse all</a></div>');
  // Expand if not already expanded
  $('.tool-expandcollapseall a:eq(0)').click(
    function(){
      $('.pub-group h3.collapsed').trigger('click');return false;
    }
  );
  // Collapse if not already collapsed
  $('.tool-expandcollapseall a:eq(1)').click(
    function(){
      $('.pub-group h3.expanded').trigger('click');return false;
    }
  );
  
  var bt = $("ol#ongoing-research a.research-link").length;
	if (bt > 0 ) {
		$("ol#ongoing-research a.research-link").bigTarget({
			hoverClass: 'over' // CSS class applied to the click zone onHover
		});
	}
});
