//Rollover state for the left navigation buttons
function rollover(nid,on) {
	if(nid != "" && nid != null) {
		var item = document.getElementById(nid);
		
		if(on) {
			var toggle = item.src.indexOf("_off.");
			if(toggle > -1)
				item.src = item.src.substring(0,toggle) + "_on" + item.src.substring(item.src.length-4);
		} else {
			var toggle = item.src.indexOf("_on.");
			if(toggle > -1)
				item.src = item.src.substring(0,toggle) + "_off" + item.src.substring(item.src.length-4);
		}
	}
}

// toggle the commission div blocks.  This assumes the div is hidden as default
function toggleArrow(div,toggle,more) {
	var elem = document.getElementById(div);
	if(elem.style.display == "block") {
		$(toggle + " img").attr("src", "/themes/f5la/images/menu-collapsed.gif");
		elem.style.display = "none";
		$(more).hide();
	} else {
        $(toggle + " img").attr("src", "/themes/f5la/images/menu-expanded.gif");
		elem.style.display = "block";
		$(more).show();
	}
}

//on load, perform some actions
$(document).ready(function() {

	//if commission packets, move and modify
	if($('#comdocs-div #block-views-commission_info_packets')) {
		
		//default settings
		$('#comdocs-div .view-content-commission-info-packets').attr("style","display:none;");
		$('#comdocs-div .view-content-commission-info-packets').attr("id","commission-packets");
		$('#comdocs-div .view-commission-info-packets .more-link').attr("style","display:none");
		
		//change the more link text
		$('.view-commission-info-packets .more-link a').html("view all commission packets");
		
		//go through each commission packet list item
		$(".view-commission-info-packets li").each(
			function(i) {
				//hide the attachment display
				$(this).find(".view-data-files-filename").hide();
			
				//write the publish date into the tite link
				var publish_date = $(this).find(".view-data-node-data-field-date-universal-field-date-universal-value .date-display-single").html();
				$(this).find(".view-data-node-title a").html(publish_date);
				
				// assign the file attachment path to the title
				var file_path = $(this).find(".view-data-files-filename a").attr("href");
				$(this).find(".view-data-node-title a").attr("href",file_path);
				
				//get the file extension
				if($(this).find(".view-data-files-filename a").html()) {
					var file_name = $(this).find(".view-data-files-filename a").html();
					var file_per = file_name.lastIndexOf(".") + 1;
					var file_ext = file_name.substring(file_per,file_name.length);
					var ext_display = '<div class="view-data-files-fileext">(' + file_ext + ')</div>';
				
					$(this).find(".view-data-node-title").prepend(ext_display);
				}
			}
		);
	}
	
	//commission packets view all
	if('.view-commission-info-packets .node') {
		$(".view-commission-info-packets .node").each(
			function(i) {
				// write the publish date into the tite link
				var publish_date = $(this).find(".field-field-date-universal .date-display-single").html();
				$(this).find("h2 a").html(publish_date);
				
				// assign the file attachment path to the title
				var file_path = $(this).find(".content a").attr("href");
				$(this).find("h2 a").attr("href",file_path);
				
				// display the file extension
				var file_ext = file_path.substring((file_path.lastIndexOf(".") + 1),file_path.length);
				var ext_display = '&nbsp;<span class="view-all-fileext">(' + file_ext + ')</span>';
				$(this).find("h2 a").after(ext_display);
			}
		);
	}
	
	//if meeting minutes, move and modify
	if($('#comdocs-div #block-views-commission_info_minutes')) {
		
		//default settings
		$('#comdocs-div .view-content-commission-info-minutes').attr("style","display:none;");
		$('#comdocs-div .view-content-commission-info-minutes').attr("id","minutes-packets");
		$('#comdocs-div .view-commission-info-minutes .more-link').attr("style","display:none");
		
		//change the more link text
		$('.view-commission-info-minutes .more-link a').html("view all meeting minutes");
		
		//go through each commission packet list item
		$(".view-commission-info-minutes li").each(
			function(i) {
				//hide the attachment display
				$(this).find(".view-data-files-filename").hide();
				
				//write the publish date into the tite link
				var publish_date = $(this).find(".view-data-node-data-field-date-universal-field-date-universal-value .date-display-single").html();
				$(this).find(".view-data-node-title a").html(publish_date);
				
				// assign the file attachment path to the title
				var file_path = $(this).find(".view-data-files-filename a").attr("href");
				$(this).find(".view-data-node-title a").attr("href",file_path);
				
				//get the file extension
				if($(this).find(".view-data-files-filename a").html()) {
					var file_name = $(this).find(".view-data-files-filename a").html();
					var file_per = file_name.lastIndexOf(".") + 1;
					var file_ext = file_name.substring(file_per,file_name.length);
					var ext_display = '<div class="view-data-files-fileext">(' + file_ext + ')</div>';
					
					$(this).find(".view-data-node-title").prepend(ext_display);
				} else {
					$(this).find(".view-data-node-title a").attr("href","javascript://");
				}
			}
		);
	}
	
	//meeting minutes view all
	if('.view-commission-info-minutes .node') {
		$(".view-commission-info-minutes .node").each(
			function(i) {
				//write the publish date into the tite link
				var publish_date = $(this).find(".field-field-date-universal .date-display-single").html();
				$(this).find("h2 a").html(publish_date);
				
				// assign the file attachment path to the title
				var file_path = $(this).find(".content a").attr("href");
				$(this).find("h2 a").attr("href",file_path);
				
				// display the file extension
				var file_ext = file_path.substring((file_path.lastIndexOf(".") + 1),file_path.length);
				var ext_display = '&nbsp;<span class="view-all-fileext">(' + file_ext + ')</span>';
				$(this).find("h2 a").after(ext_display);
			}
		);
	}
	
	//if commission agendas, move and modify
	if($('#comdocs-div #block-views-commission_agendas')) {
		
		//default settings
		$('#comdocs-div .view-content-commission-agendas').attr("style","display:none;");
		$('#comdocs-div .view-content-commission-agendas').attr("id","commission-agendas");
		$('#comdocs-div .view-commission-agendas .more-link').attr("style","display:none");
		
		//change the more link text
		$('.view-commission-agendas .more-link a').html("view all commission agendas");
		
		//go through each commission packet list item
		$(".view-commission-agendas li").each(
			function(i) {
				//hide the attachment display
				$(this).find(".view-data-files-filename").hide();
			
				//write the publish date into the tite link
				var publish_date = $(this).find(".view-data-node-data-field-date-universal-field-date-universal-value .date-display-single").html();
				$(this).find(".view-data-node-title a").html(publish_date);
				
				// assign the file attachment path to the title
				var file_path = $(this).find(".view-data-files-filename a").attr("href");
				$(this).find(".view-data-node-title a").attr("href",file_path);
				
				//get the file extension
				if($(this).find(".view-data-files-filename a").html()) {
					var file_name = $(this).find(".view-data-files-filename a").html();
					var file_per = file_name.lastIndexOf(".") + 1;
					var file_ext = file_name.substring(file_per,file_name.length);
					var ext_display = '<div class="view-data-files-fileext">(' + file_ext + ')</div>';
				
					$(this).find(".view-data-node-title").prepend(ext_display);
				}
			}
		);
	}
	
	//commission agendas view all
	if('.view-commission-agendas .node') {
		$(".view-commission-agendas .node").each(
			function(i) {
				// write the publish date into the tite link
				var publish_date = $(this).find(".field-field-date-universal .date-display-single").html();
				$(this).find("h2 a").html(publish_date);
				
				// assign the file attachment path to the title
				var file_path = $(this).find(".content a").attr("href");
				$(this).find("h2 a").attr("href",file_path);
				
				// display the file extension
				var file_ext = file_path.substring((file_path.lastIndexOf(".") + 1),file_path.length);
				var ext_display = '&nbsp;<span class="view-all-fileext">(' + file_ext + ')</span>';
				$(this).find("h2 a").after(ext_display);
			}
		);
	}
	
	//Move the Contact Info block above webform
	if($('#contact-info-links a#contact-info')) {
		$('#block-block-7').hide();
		var contact_div = $('#block-block-7').html();
		$("#contact-tab-block").html(contact_div);
		$('#block-block-7').empty();
		
		//Contact - Contact Info (Default: Show)
		$('#contact-info-links a#contact-info').addClass('contact-link-active');
		$('#contact-info-links a#contact-info').click(function() {
			$('#contact-info-links a#contact-info').removeClass('contact-link-normal');
			
			$('#contact-info-links a#contact-info').addClass('contact-link-active');
			$('#contact-info-links a#map-directions').addClass('contact-link-normal');
			$('#contact-info-links a#parking-info').addClass('contact-link-normal');
			
			$('#contact-contact-info').show();
			$('#contact-map-directions').hide();
			$('#contact-parking-info').hide();
			return false;
		});
		
		//Contact - Map/Directions (Default: Hide)
		$('#contact-info-links a#map-directions').click(function() {
			$('#contact-info-links a#map-directions').removeClass('contact-link-normal');
			
			$('#contact-info-links a#contact-info').addClass('contact-link-normal');
			$('#contact-info-links a#map-directions').addClass('contact-link-active');
			$('#contact-info-links a#parking-info').addClass('contact-link-normal');
			
			$('#contact-contact-info').hide();
			$('#contact-map-directions').show();
			$('#contact-parking-info').hide();
			return false;
		});
		
		//Contact - Parking Info (Default: Hide)
		$('#contact-info-links a#parking-info').click(function() {
			$('#contact-info-links a#parking-info').removeClass('contact-link-normal');
			
			$('#contact-info-links a#contact-info').addClass('contact-link-normal');
			$('#contact-info-links a#map-directions').addClass('contact-link-normal');
			$('#contact-info-links a#parking-info').addClass('contact-link-active');
			
			$('#contact-contact-info').hide();
			$('#contact-map-directions').hide();
			$('#contact-parking-info').show();
			return false;
		});
	}
	
	//Start CD Mods (2007-08-24)
	//if Research in the News
	if($('#block-views-Research_In_The_News')) {
		$('#block-views-Research_In_The_News').hide();
		var research_contents = $('#block-views-Research_In_The_News').html();
		$("#ResearchInTheNews-div").html(research_contents);
		$('#block-views-Research_In_The_News').empty();	
	}
	
	if($('.view-content-contract-opportunity-documents')) {
		$(".view-content-contract-opportunity-documents li").each(
			function(i) {
				var file_name = $(this).find(".view-data-files-filename a").html();
				//get the file extension
				var file_per = file_name.lastIndexOf(".") + 1;
				var file_ext = file_name.substring(file_per,file_name.length);
				
				$(this).find(".view-data-files-filename a").html($(this).find(".view-data-node-title").html());
				$(this).find(".view-data-files-filename").prepend('<div class="view-data-files-fileext" style="display:inline;">(' + file_ext + ')</div>&nbsp;  ');
				$(this).find(".view-data-files-filename").append(" - " + $(this).find(".view-data-node-data-field-date-universal-field-date-universal-value").html());
				
				$(this).find(".view-data-node-title").html("");
				$(this).find(".view-data-node-data-field-date-universal-field-date-universal-value").html("");
			}
		);
	}
	//End CD Mods
	
	//Move the Success Stories on Community Impact
	if($("a#ci_success")) {
		$('#block-views-Success_Stories_CI_Block').insertBefore("p#cisuccessp");
	}
	
	//Hide RSS Icon
	if($("a.feed-icon")) {
		$("a.feed-icon").hide();
	}
	
	/* 
	 * target thumbnails that need to be clickable
	 * this must be done for each instance
	 */
	
	//News Landing Page and Home Page Block
	if($(".view-item-News-And-Annoucements-Page")) {
		$(".view-item-News-And-Annoucements-Page").each (
			function(i) {
				if($(this).find("img").attr("src")) {
					var new_img_div = '<a href="' + $(this).find("a").attr("href") + '">' + $(this).find(".view-data-node-data-field-mini-thumb-field-mini-thumb-fid").html() + '</a>';
					$(this).find(".view-data-node-data-field-mini-thumb-field-mini-thumb-fid").html(new_img_div);
				}
			}
		);
	}
	
	//Get Involved Landing Page
	if($(".view-get-involved-index-block")) {
		$(".view-get-involved-index-block .node").each (
			function(i) {
				if($(this).find(".content .field-field-thumbnail img").attr("src")) {
					var new_img_div = '<a href="' + $(this).find("h2 a").attr("href") + '">' + $(this).find(".content .field-field-thumbnail .field-item").html() + '</a>';
					$(this).find(".content .field-field-thumbnail .field-items .field-item").html(new_img_div);
				}
			}
		);
	}
	
	//Video-Audio Landing Page
	if($(".view-video-english-block")) {
		$(".view-item-video-english-block").each (
			function(i) {
				if($(this).find(".view-data-node-data-field-thumbnail-field-thumbnail-fid img").attr("src")) {
					var new_img_div = '<a href="' + $(this).find(".view-data-node-title a").attr("href") + '">' + $(this).find(".view-data-node-data-field-thumbnail-field-thumbnail-fid").html() + '</a>';
					$(this).find(".view-data-node-data-field-thumbnail-field-thumbnail-fid").html(new_img_div);
				}
			}
		);
	}
	
	if($(".view-video-spanish-block")) {
		$(".view-item-video-spanish-block").each (
			function(i) {
				if($(this).find(".view-data-node-data-field-thumbnail-field-thumbnail-fid img").attr("src")) {
					var new_img_div = '<a href="' + $(this).find(".view-data-node-title a").attr("href") + '">' + $(this).find(".view-data-node-data-field-thumbnail-field-thumbnail-fid").html() + '</a>';
					$(this).find(".view-data-node-data-field-thumbnail-field-thumbnail-fid").html(new_img_div);
				}
			}
		);
	}
	/* clickable thumbnails */
});

//clear the starting address field if it's the default
function clearField() {
	var start_location = $("#starting_location").val();
	if(start_location == "Starting Address")
		$("#starting_location").val("");
}

//set the starting address field to the default
function setField() {
	var start_location = $("#starting_location").val();
	if(start_location == "")
		$("#starting_location").val("Starting Address");
}

//open a new window with driving directions from google
function getDrivingDirections() {
	var start_location = $("#starting_location").val();
	//remove any special characters except space and comma
	//trim spaces and replace remaining spaces with a plus sign
	start_location = start_location.replace(/[^, a-zA-Z0-9]/g,"");
	start_location = start_location.replace(/  /g," ");
	start_location = start_location.replace(/ /g,"+");
	
	//go to the map
	if(start_location != "" && start_location != "Starting+Address") {
		var googleDD = "http://maps.google.com/maps?f=d&hl=en&saddr="+start_location+"&daddr=750+North+Alameda+Street,+Suite+300+Los+Angeles,+CA+90012&sll=34.166221,-118.794651&sspn=0.048292,0.070381&layer=&ie=UTF8&om=1";
		var googlewin = window.open(googleDD,"ddbygoogle","width=800,height=600,toolbar,location,directories,status,scrollbars,resizable,menubar");
		
		//var yahooDD = "http://maps.yahoo.com/index.php#mvt=m&q2=4333+Park+Terrace+Drive+Westlake+Village+CA+91361&q1="+start_location+"&trf=0&lon=-118.79272&lat=34.165831&mag=4";
		//var yahoowin = window.open(yahooDD,"ddbyyahoo","width=800,height=600,toolbar,location,directories,status,scrollbars,resizable,menubar");
	} else {
		alert("Please enter your starting address.");
	}
}
