(function($){


  $(document).ready(
  function(){
  
  //Links in New Window
  $('.menu-mlid-457 a').attr("target","_blank");
  $('.menu-mlid-889 a').attr("target","_blank");
    
  //Animate Drop Down lists
  
  $.fn.nextElementInDom = function(selector, options) {
        var defaults = { stopAt : 'body' };
        options = $.extend(defaults, options);
 
        var parent = $(this).parent();
        var found = parent.find(selector);
 
        switch(true){
            case (found.length > 0):
                return found;
            case (parent.length === 0 || parent.is(options.stopAt)):
                return $([]);
            default:
                return parent.nextElementInDom(selector);
        }
    };
  
  
  	$('#block-menu-block-3 > div > div > ul > li > a').each(function(){
  		$(this).click(function(e){
			$('#block-menu-block-3 ul li ul li a').css("color","#FFF");
			$('#block-menu-block-3 > div > div > ul > li > a').removeClass("ActiveItem");
  		e.preventDefault();
  			if ($(this).next().height() < 100) {
  				if($('#main-wrapper').attr("menushow")!="true") {
  					$('#main-wrapper').animate({top:"+=235px"},450,"linear",function(){
  						clickArrow = document.createElement("a");
  						$(clickArrow).attr("id","clickArrow");
  						$(clickArrow).click(function(){
  							$('.ActiveDrop').animate({height:"0px","padding-top":"0px"},400,"linear");
  							$('.ActiveItem').css("color","#000");
  							$('.ActiveDrop').removeClass("ActiveDrop");
  							$("#greenDiv").remove();
  							$("#clickArrow").remove();
  							$('#main-wrapper').animate({top:"-=235px"}, 350, "linear");
								$('.appendedMenu').animate({'top':0}, 400, "linear");
								$('#main-wrapper').attr("menushow","false");
  							$('#colorbar').animate({top:"0px"},360,"linear");
  						});  						
  						greenDiv = document.createElement("div");
	  					$(greenDiv).attr("id","greenDiv");
  						$("#main-wrapper").prepend(greenDiv);
  						$("#main-wrapper").prepend(clickArrow);
  					});
  					
  					$('#colorbar').animate({top:"234px"},450,"linear");
						$('#main-wrapper').attr("menushow","true");
					} else {
						$('#block-menu-block-3 ul li ul').animate({'height':0 ,'padding-top':0},400, "linear");
						$('.appendedMenu').animate({'top':0},400, "linear");
						$(this).removeClass("ActiveItem");
					}
  				$(this).next().animate({height:"218px","padding-top":"20px"},400,"linear");
  				$(this).next().addClass("ActiveDrop");
  				$(this).addClass("ActiveItem");
  				$('.appendedMenu').animate({top:"20px"},400,"linear");
  			} else {
  				$(this).next().animate({height:"0px","padding-top":"0px"},400,"linear");
  				$(this).next().removeClass("ActiveDrop");
  				$(this).removeClass("ActiveItem");
  				$("#greenDiv").remove();
  				$("#clickArrow").remove();
  				$('#main-wrapper').animate({top:"-=235px"}, 350, "linear");
					$('.appendedMenu').animate({'top':0}, 400, "linear");
					$('#main-wrapper').attr("menushow","false");
					
  					$('#colorbar').animate({top:"0px"},360,"linear");
  			}
  		});
  	});
		//Make columns of seven items
	  $('#block-menu-block-3 ul li ul').each(function(){
	  		if ($('li', this).length > 8) {
	  			var x=0;
	  			elems = new Array;
	  			$('li', this).each(function(){
	  				if(x > 7) {
	  					elems.push($(this));
	  					$(this).remove();
	  				}
	  				x++;
	  			});
	  			newDiv = document.createElement("div");
	  			for (var i=0; i<elems.length; i++)	{
	  				$(newDiv).append(elems[i]);
	  				test = 'test';
	  			}
	  			$(newDiv).addClass("appendedMenu");
	  			$(this).append(newDiv);
	  		}
	  	});
  	
    //add clipped corner overlay.
    $('.clipped-corner').prepend("<span class='clipped-corner-overlay'></span>");
    //position to top-right.
    $('.clipped-corner-overlay').each(function() {
      var p = $(this).parent();
      $(this).css("top","-"+p.css("padding-top"));
      $(this).css("right","-"+p.css("padding-right"));
      if ( $(this).next().is("a") && $(this).next().children(":first").is("img") ) {
        $(this).next().children(":first").css("margin-top","-34px");
      }
    });
    //add clipped corner overlay.
    $('.clipped-corner-tall').prepend("<span class='clipped-corner-overlay-tall'></span>");
    //position to top-right.
    $('.clipped-corner-overlay-tall').each(function() {
      var p = $(this).parent();
      $(this).css("top","-"+p.css("padding-top"));
      $(this).css("right","-"+p.css("padding-right"));
      if ( $(this).next().is("a") && $(this).next().children(":first").is("img") ) {
        $(this).next().prepend("<span class='clipped-corner-overlay-tall' style='display:block;'></span>");
        $(this).next().children("img:first").css("z-index","0");
        $(".clipped-corner-tall span").css("position","absolute");
                $(this).remove();
      }
    });
    //add clipped corner overlay.
    $('.clipped-corner-lightbox').prepend("<span class='clipped-corner-overlay-lightbox'></span>");
    //position to top-right.
    $('.clipped-corner-overlay-lightbox').each(function() {
      var p = $(this).parent();
      $(this).css("top","-"+p.css("padding-top"));
      $(this).css("right","-"+p.css("padding-right"));
      if ( $(this).next().is("a") && $(this).next().children(":first").is("img") ) {
        $(this).next().prepend("<span class='clipped-corner-overlay-lightbox'></span>");
        $(this).next().children("img:first").css("z-index","0");
        $(this).next().children("img:first").css("position","absolute");
        $(this).remove();
      }
    });
	  	

  	//homepage marquee buttons
  	$(".views-slideshow-pager-field-item div div").html("&nbsp;&nbsp;");


	///Fix the pagers not becoming active on slide transition.
   	Drupal.viewsSlideshowSlideCounter = Drupal.viewsSlideshowSlideCounter || {};
  	/**
   	* Implement hook_viewsSlidshowTransitionBegin for pager fields pager.
   	*/
	Drupal.viewsSlideshowSlideCounter.transitionBegin = function (options) {
    	$('[id^="views_slideshow_pager_field_item_' + options.slideshowID + '"]').removeClass('active');
    	// Add active class to active pager.
    	$('#views_slideshow_pager_field_item_' + options.slideshowID + '_' + options.slideNum).addClass('active');
  	}
  
  
  	//blog title
  	$('.page-blog #page-title').text("PECI Blog");
  	  
  	$('.views-field-field-tags .field-content').each(function(){
  		$(this).addClass("movedTags");
  			moveme = this;
  			var parent = $(moveme).parent();
  			$(this).remove();
  			$(parent).after(moveme);
  	});
  	  
  	  
  	//homepage marquee text and link
  	$(".front .views-field-field-homepage-marquee-quote div").prepend("&ldquo;");
  	$(".front .views-field-field-homepage-marquee-quote div").append("&rdquo;");
  	$(".views-slideshow-cycle-main-frame-row-item").each( function(i, e) {
  		var authorblock = "<div class='marquee-author'>&ndash;"+ $(this).children(".views-field-field-author").text()+
  		", "+$(this).children(".views-field-field-author-title").text()+
  		"<br/>"+$(this).children(".views-field-field-author-location").text()+"</div>";
  		authorblock += "<div class='marquee-link'><a href=''>Learn More</a></div>";
  		$(this).children(".views-field-field-author-title").hide();
  		$(this).children(".views-field-field-author-location").hide();
  		$(this).children(".views-field-field-author").hide();
  		$(this).children(".views-field-field-homepage-marquee-quote").append(authorblock);
  	});
  	//Press Release moving Date in to body content.
  	$(".node-press-release div .field-name-body p:first").prepend("&nbsp;&mdash;&nbsp;")
  	$(".node-press-release div .field-name-body p:first").prepend($(".field-name-field-press-release-date div div span"));
  	$(".field-name-field-press-release-date").remove();
  	
  	// Add Search text to search box
  	$('#edit-search-block-form--2').attr("value","Search").click(function(){
  		$(this).attr("value","");
  	});
  	//Searchable Library - Adding more search buttons.
  	if ($("#edit-field-interest-value").length != 0 ) {
  	//if ($("#edit-field-interest-value").val().length > 0) {
  		var searchbutton = $("#edit-submit-content-list");
  		searchbutton.val("search");
  		searchbutton.clone().appendTo(".form-item-field-interest-value");
  		searchbutton.clone().appendTo(".form-item-field-region-value");
  		searchbutton.clone().appendTo(".form-item-field-media-type-value");
  		$("#edit-field-interest-value-wrapper").after("<div class='andor'>-and/or-</div>");
  		$("#edit-field-region-value-wrapper").after("<div class='andor'>-and/or-</div>");
  		$("#edit-field-media-type-value-wrapper").after("<div class='andor'>-or-</div>");
  		//user login block
  		var login = $("#block-user-login").clone();
  		$("#block-user-login:first").remove();
  		$(".view-filters").after(login);
  		$("#block-user-login .block-title").text("Search Results:");
  		$(".form-item-name label").text("User Name:");
  		$(".form-item-pass label").text("Password:");
  		$("#user-login-form").prepend("<span class='employees-login'>Employees log-in for full results:</span>");
  		$("#edit-submit").val("enter");
  		$("#block-user-login").after("<div class='library-separator'> </div>");
  	}
  	
  	//Comments
  	$("#comments h2.title").append(":");
  	$(".node-blog-entry .field-name-field-tags .field-label").text("Filed Under");
  	var posted_block = $(".posted_date").clone();
  	$(".posted_date").remove();
  	$(".node-blog-entry .field-name-field-tags").before(posted_block);
  	$("#edit-author--2 label").html("Name:<span title='This field is required.' class='form-required'>*</span>");
  	$("#edit-field-email label").html("E-Mail:<span title='This field is required.' class='form-required'>*</span>");
  	$(".form-item-subject label").html("Subject:<span title='This field is required.' class='form-required'>*</span>");
  	$("#edit-field-comment label").html("Add your comment here:<span title='This field is required.' class='form-required'>*</span>");
  	
  	//Definition Lists
  	$('.field-name-field-definition').hide();
  	$('.definition-def').hide();
  	plusdiv = document.createElement('div');
  	$(plusdiv).addClass('def_list_plus_sign');
  	$('.field-name-field-def-item .field-item').append($(plusdiv));
  	$('.definition-item').append($(plusdiv));
  	$('.definition-item').click(function(ev){
  		$('.def_list_plus_sign',this).toggleClass('def_list_minus_sign');
  		if ($('.def_list_plus_sign',this).hasClass('def_list_minus_sign')) {
  			$(this).nextElementInDom('.definition-def').css('display','inline-block');
  		} else {
  			$(this).nextElementInDom('.definition-def').hide();
  		}
  		if ( $.browser.msie ) {
  			var d = $(this).position();
				window.scrollTo( d.left , d.top);
			}
  	});
  	items = $(".definition-def:contains('•')");
  	$(items).each(function(){
  		newList = "";
  		listArray = $(this).text().split('•');
  		for (x=0; x<listArray.length; x++) {
  			if(listArray[x]!="") {
  				newList += "<li>" + listArray[x] +"</li>";
  			}
  		}
  		$(this).html("<ul>" + newList + "</ul>");
  	});
  	
  	$('td').wrapInner("<div style='position:relative'>");
  	window.alert = function ( text ) { console.log( 'tried to alert: ' + text ); return true; };
  	
  	//Def list bullets
  	
  	
//Nice Idea, but this was thwarted by the need to save a view after editing a field  	
/*
  	checkView = $('.logged-in .view-header').parent().attr("class");
  	if(checkView.length>0) {
  		dest = location.pathname;
  		classes = checkView.split(" ");
  		$(classes).each(function(){
  			if(this.toString().search(/view-display-id/)==0) {
  				classparts = this.toString().split("-");
  				viewId = classparts[classparts.length-1];
  				editLink = "<a href='#overlay=/admin/structure/views/nojs/config-item/content_list/" + viewId + "/header/area?destination=" + dest + "' class='view-header-edit-link'>Edit Me</a>";
  			}
  		});
  		$('.view-header').append(editLink);
  	}
*/

  //blog sidebar
  if ( document.URL.indexOf("/blog/bios") > 0 ) {
	$(".blogger-bios").addClass("active");
	$(".blogger-home").removeClass("active");
  }
  if ( document.URL.indexOf("/blog-entry") > 0 ) {
	$(".blogger-home").removeClass("active");
  }  
  //blog images 
  if ( document.URL.indexOf("/blog/") > 0 ) {
	//$(".column > .section > h1:first").hide();
	//$(".pane-1").hide();
	if ( document.URL.indexOf("/blog/bios") < 0 ) {
		$(".views-field-field-image").hide();
	}
  }
  //tags page 
  if ( document.URL.indexOf("/tags/") > 0 ) {
	$(".column > .section > h1:first").hide();
	$(".blog-comment-separator").hide();
  }

  //blog read more links
  if ( $('.view-display-id-blog_page').length > 0 ) {
	$(".views-field-body").each(function () {
		/*
alert($(this).html().length);
		if ( $(this).text().substr( $(this).text().length - 5, 3 ) != "..." ) {
		
			//alert($(this).text().substr( $(this).text().length - 5, 3 ));
			//$(this).parent().children(".views-field-view-node").hide();
		} else {
*/
			// move into last p
			var replaceme = $(this).parent().children(".views-field-view-node").clone();
			$(this).parent().children(".views-field-view-node").remove();
			$(this).children(".field-content").children("p:last").append(replaceme);
/* 		} */
	});
  } 
  	
  //Rewrite User Image links to use Lightbox
  if($('.rewrite-user-image').length>0) {
  	$('.rewrite-user-image').each(function(){
  		img = $('img',this);
  		link = encodeURI($(this).attr("link"));
  		newlink = document.createElement("a");
  		$(newlink).attr("href", link);
  		$(newlink).attr("rel", "groupedimages");
  		$(newlink).attr("class", "colorbox-load");
  		$(newlink).append(img);
			$(this).html(newlink);
  	});
  	$('.rewrite-user-image a').live('click', function() {
  		$.fn.colorbox({href:$(this).attr('href'), open:true, width:"750px"});
  		return false;
		});
  }
  if($('.header-img').length>0) {
  	$('.region-sidebar-second').css("margin-top","228px");
  }
  	
  	
	});
	
	Drupal.behaviors.peci = {
		attach: function() {
				 
  	move = $('.section-careers .field-name-field-bio-name');
  	move.remove();
  	$('.section-careers .profile-quote').before(move);
  	move = $('.section-careers .field-name-field-bio-title');
  	move.remove();
  	$('.section-careers .profile-quote').before(move);
  	
		}
	};
	
})(jQuery);
;

