$(document).ready(function() {
	
	//include Stylesheets/Js
	includeHeaderFile('styles/jquery-ui-1.8.7.custom.css');
	includeHeaderFile('scripts/jquery-ui-1.8.7.custom.min.js');
	
	//align header
	$('#header').offset({left:$('#wrapper').offset().left-38});

	   
	//build menu
	populate_menu($('#verticalmenu'));
	style_menu($('#verticalmenu'));
	//add arrows to li with child ul
	var span = document.createElement('span');
	$(span).addClass('arrowdiv');
	$(span).html('&nbsp;&nbsp;');
	$(".menu-item").children('a').append(span);
	
	//Align Quicklinks
	$.each($(".quicklink"),function(i){
		$(this).offset({top:$(this).offset().top - 90});
		$(this).parent().attr('href',quicklinks[i]);
	});
	
	//Capture Resize-align header
	$(window).resize(function() { 
	  $('#header').offset({left:$('#wrapper').offset().left-38});
	});
	
	
	

});
	
/*------------------------------
	Include Stylesheets Method
--------------------------------*/
function includeHeaderFile(filename) {
	var dot = filename.lastIndexOf("."); 
 	if( dot == -1 ) return; 
 	var extension = filename.substr(dot,filename.length); 
 	var file = null; 
	if(extension == '.css')
	{
		file  = document.createElement('link');
		file.rel = 'stylesheet'
		file.type = 'text/css';
		file.href = filename;
	}
	else if(extension == '.js')
	{
		file = document.createElement('script');
		file.type = 'text/javascript';
		file.src = filename;
	}
	document.getElementsByTagName('head')[0].appendChild(file);
	return;
}

	
/*---------
	MENU
----------*/

function populate_menu(vmenu)
{
 	$.each(menu_obj.Menu, function(i, object) {
 	    $.each(object, function(property, value) {
	 	    
	 	    populate_ul(vmenu,property,value);
	 	    var img = document.createElement('img');
	 	    
	 	    var index = 1;
	 	    //Add Bugs to Main Menu Items
	 	    $.each($('#verticalmenu').children('li'),function(){
		 	    $(img).attr('src','images/mac'+index+'.png');
		 	    $(this).prepend(img);
		 	    index++;
	 	    });
 	    });
 	    
	});
}

function populate_ul(ulist,property,value){
	var li = document.createElement('li');
	$(li).addClass('menu-item');
	var a = document.createElement('a');
	$(a).attr('href','#');
	$(a).html(property);
	$(li).append(a);
	ulist.append(li);
	 	    
	if(typeof value === "object"){
	   var ul = document.createElement('ul');
	   $(ul).addClass('menu-ul');
	   $(ul).css('position','absolute');
	   $(ul).css('display','none');
	   $(li).append(ul);
	   	   
	   $.each(value, function(prop, val) {
	    	
	    	if(typeof val === "object"){
	 	    	$.each(val, function(p,v){
		 	    	populate_ul($(ul),p,v);
		   		});
			}
			else{ 
		 	    li = document.createElement('li');
		 	    $(li).addClass('link-menu-item');
		 	    a = document.createElement('a');
		 	    
		 	    var href='';
		 	    $.each(menu_links, function(title,link) {
			 	    $.each(link, function(t,l){
				 	    if(t==val){href=l;}
				 	});
				});
				
				//Check if link needs Exit Disclaimer
				if(href.indexOf('exit_disclaimer')>-1){
					$(a).attr('onclick',href);
					$(a).attr('target','_blank');
				}
				else{
					$(a).attr('href',href);
		 	    	if(href.indexOf('.pdf') > -1){$(a).attr('target','_blank');}
				}	
		 	    $(a).html(val);
		 	    $(li).append(a);
		 	    $(ul).append(li);
	 	    }
		});
	}
}

var submenuoffset=15; //Offset of submenus from main menu. 

function style_menu(ul)
{
	$.each(ul.children('li'), function(property,value){ 
		var offsetLeft = ul.width()+submenuoffset;
		$(this).hover(function(){ 
			$.each($(this).children('ul'),function(){ 
					$(this).css('left',offsetLeft);
					$(this).css('top',-5);
					$(this).css('display','block'); 
			})},
			//MouseOut
			function(){
				$.each($(this).children('ul'), function(){
				$(this).css('display','none');
			});
		});
		
		$.each($(this).children('ul'), function(){
			style_menu($(this));
		});
	});
}


/*-------------
	END MENU
------------*/

function exit_disclaimer(url)
{
	var dialog = document.createElement('div');
	$(dialog).attr('id','dialog');
	$(dialog).attr('title','OCWA Exit Disclaimer');
	$(dialog).html('Please be advised that by clicking on "Ok" below, you are entering a 3rd party website which is not under the control of the Orange County Water Authority. OCWA cannot attest to the accuracy of the information provided by these third-party sites or any other linked site. We are providing these links for your reference, and in doing so, we do not endorse any non-government websites, companies or applications.<br />');
	$('body').append(dialog);
	$('#dialog').dialog({ buttons: [
		    {
		        text: "Ok",
		        click: function() { $(this).dialog("close"); window.open(url, '_blank',null); }
		    },
		    {
			    text: "Cancel",
			    click: function() {$(this).dialog("close");}
		    }
		] 
	});
	
	return false;
}



/************************************************************************************/
/************************* EDITABLE MENU AREA **************************************/
/**********************************************************************************/

/*###############################################################################*/
/*                                                                              */
/*		Object List of Menu Items											   */
/*		{'Item':[children]}													  */
/*		{'Child':[grandChild,grandChild]} (OR) 'Child' (has no children)     */
/*                                                                          */
/*#########################################################################*/
var menu_obj = {'Menu':
[

	{'About OCWA':
		[
			{'Contact Us':
				['Mailing Address & Staff']
			},
			'By-Laws & Policies',
			
			{'Board of Directors':
				['Board of Directors','Meeting Schedule','Agenda','Minutes']
			},
			{'Financials':
				['Audits of Financials','Budgets']
			},
			'Annual Reports'
	
		]
	},
	
	{'Education':
		['For Community','For Teachers & Schools','Water Conservation Education Program','GET WET! Program','Kids Corner']
	},
	
	{'Events':
		['Annual Earth & Water Festival','Workshops, Training & Stewardship','Other Festivals & Events']
	},
	
	{'Announcements & News':
		['Press Releases','Legal Notices','Requests for Services & Goods']
	},
	
	{'Projects & Publications':
		[
			{'Conservation':
				['Water Conservation','Drought Management','Leak Detection Program','Tips & Information','Safe Yield Analysis']
			},
			{'County Plans':
				['Water Master Plan','Open Space Plan','Orange County Design Guide']
			},
			{'Flooding':
				['Flood Forums','Stream Level Gauges']
			},
			{'Groundwater':
				['Wellhead Protection','Groundwater Resource Study','Aquifers']
			},
			{'Capital Projects & Infrastructure':
				['Capital Projects & Infrastructure','NE OC Feasibility Study','Mid County Study','Historic Documents']
			},
			{'Stormwater':
				['MS4 Reports']
			},
			{'Streams':
				['Stream Level Gauges','Stream Water Quality Monitoring']
			},
			{'Wastewater':
				['Decentralized Wastewater Project','Sewerage Study']
			},
			{'Water Quality':
				['Impervious Cover']
			},			
			{'Watershed Initiatives':
				['County Watersheds','Moodna Creek Watershed','Moodna Watershed Council',
				'Wallkill River Watershed','Quassaick Creek Watershed','Glenmere Lake Watershed','Trout Brook Watershed']
			},]},
	
	
	{'Maps & Tools':
		['Moodna Watershed Atlas','Moodna Watershed Interactive Map','Orange County Design Guide','Maps']
	},
	
	{'Well/Public Water Supply':
		['Testing Well Water','Billing Inquiries']
	},
	{'Useful Links':
		[]
	}

]};
		  
/* END MENU OBJECT */
/*--------------------------------------------------------------------------------------------------*/
/* START MENU LINKS */
/*##################################################################*/
/*                                                                 */
/*		Array Of {Menu Item : Link}   							  */
/*		{'Exact Duplicate of Menu Item Text' : 'url link' }		 */
/*                                                              */
/*#############################################################*/
var menu_links = [

//About OCWA
	//Contact Us
		{'Mailing Address & Staff':'contact_us.html'},

	//By-Laws & Policies
		{'By-Laws & Policies':'bylaws_policies.html'},

	//Board of Directors
		{'Board of Directors':'board_directors.html'},{'Meeting Schedule':'meeting_documents.html'},
		{'Agenda':'meeting_documents.html'},{'Minutes':'meeting_documents.html'},
	
	//Financials
		{'Audits of Financials':'financials.html'},{'Budgets':'financials.html'},

	//Annual Reports
		{'Annual Reports':'annual_report.html'},

//Education
		{'For Community':'ed_community.html'},{'For Teachers & Schools':'education.html'},
		{'Water Conservation Education Program':'education_water_conservation.html'},
		{'GET WET! Program':'education_getwet.html'},{'Kids Corner':'kids_corner.html'},

//Events
		{'Annual Earth & Water Festival':'festival.html'},{'Workshops, Training & Stewardship':'ed_community.html'},
		{'Other Festivals & Events':'events.html'},

//Announcements & News
		{'Press Releases':'press_releases.html'},{'Legal Notices':'notices.html'},{'Requests for Services & Goods':'notices.html'},

//Projects & Publications
	//Conservation
		{'Water Conservation':'conservation_drought.html'},{'Leak Detection Program':'leak_detection.html'},
		{'Drought Management':'conservation_drought.html'},{'Tips & Information':'tips_info.html'},
		{'Safe Yield Analysis':'conservation_drought.html'},
	
	//County Plans
		{'Water Master Plan':'county_plans.html'},{'Open Space Plan':'county_plans.html'},
		{'Orange County Design Guide':'county_plans.html'},

	//Flooding
		{'Flood Forums':'flood_forum.html'},{'Stream Level Gauges':'stream_gauges.html'},
	
	//Groundwater
		{'Wellhead Protection':'groundwater.html'},{'Groundwater Resource Study':'groundwater.html'},{'Aquifers':'aquifers.html'},
	
	//Capital Plans & Infrastructure
		{'Capital Projects & Infrastructure':'infrastructure.html'},{'NE OC Feasibility Study':'ne_county.html'},
		{'Mid County Study':'mid_county.html'},{'Historic Documents':'water_loop.html'},

	//Stormwater
		{'MS4 Reports':'stormwater.html'},

	//Streams
		{'Stream Level Gauges':'stream_gauges.html'},{'Stream Water Quality Monitoring':'streams.html'},

	//Water Quality
		{'Impervious Cover':'water_quality.html'},

	//Wastewater
		{'Decentralized Wastewater Project':'decentralized_wastewater.html'},{'Sewerage Study':'wastewater.html'},
	
	//Watershed Initiatives
		{'County Watersheds':'watersheds.html'},{'Moodna Creek Watershed':'moodna.html'},{'Moodna Watershed Council':'moodna_council.html'},
		{'Wallkill River Watershed':'wallkill.html'},{'Quassaick Creek Watershed':'quassaick.html'},
		{'Glenmere Lake Watershed':'glenmere.html'},{'Trout Brook Watershed':'trout_brook.html'},
	
//Maps & Tools
		{'Moodna Watershed Atlas':'moodna.html'},{'Moodna Watershed Interactive Map':'moodna.html'},
		{'Orange County Design Guide':'county_plans.html'},{'Maps':'maps.html'},
	
//Well/Public Water Supply
		{'Testing Well Water':'tips_info.html'},{'Billing Inquiries':'billing.html'},
		
//Useful Links
		{}
];

/* END MENU LINKS */
/*----------------------------------------------------------------------------------------------------------*/
/* START QUICKLINKS */

/*#######################################*/
/*                                      */
/*   Array Of url link (1-9 in order)  */
/*                                    */
/*###################################*/

var quicklinks = ['festival.html','moodna.html','education.html',
		  'county_plans.html','maps.html','watersheds.html',
		  'tips_info.html','infrastructure.html','streams.html'];
/* END QUICKLINKS */

		  
