// JavaScript Document
$("document").ready(function() {
		/*var url = document.location.href;
		try {
			url = url.split("\?");
			var args = url[1].split('=');
			if (args[0] == 'p') {
				$.scrollTo('#other');
			}
		} catch(e) { }
							 
		//Set up the accordion
		var acc = $("#acc").accordion({
			active: false,
			alwaysOpen: false,
			navigation: true,
			autoHeight: false,
			animated:false,
			header: "a.accordion-header"			  
		});
		
		$("a.accordion").click(function(e) {
			var drawer = $(this).attr("id");
			acc.accordion("activate", drawer-1);
		});		*/
		var name = 'accDrawer1' + document.title.indexOf('Internship');
		var url = document.location.href;
		var args;
							 
		//Set up the accordion
		var acc = $("#acc").accordion({
			active: false,
			alwaysOpen: false,
			autoHeight: false,
			animated:false,
			header: "a.accordion-header"
		});
			
		var active;	
		var cookies = document.cookie.split(';');
		for(var i=0;i < cookies.length;i++) {
			var c = cookies[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(name+"=") != -1) {
				var drawer = c.split("=");
				//alert(drawer);
				if (drawer[1].length > 0) {
					active=drawer[1];
					acc.accordion("activate", drawer[1]-1);
				}
			}
		}
		
		try {
			url = url.split("\?");
			args = url[1].split('=');
			if (args[0] == 'p') {
				if (args[1] != parseInt(active)) 
					acc.accordion("activate", args[1]-1);
				$.scrollTo('#acc');
			}
		} catch(e) { }
		
		function setAccCookie(location) {
			//var expires = new Date();
			//expires.setTime(expires.getTime()+(1*60*60*1000));
			document.cookie = name + "=" + location + "; path=/";
		}
		
		function deleteAccCookie() {
			document.cookie = name + "=; expires=" + -1 + "; path=/";
		}
		
		//other links to open the accordion
		$("a.accordion").click(function(e) {
			var drawer = $(this).attr("id");
			setAccCookie(drawer);
			acc.accordion("activate", drawer-1);
			$.scrollTo('#acc');
			e.preventDefault();
		});	
		
		//set a cookie when a section is opened to keep that section open
		//remove the cookie when all sections are closed
		$('.ui-accordion').bind('accordionchange', function(event, ui) {
			if(ui.newContent.text() == '') { 
				deleteAccCookie();
			} else {
				var loc = ui.newHeader.attr('href');
				try {
					loc = loc.split("=");
					setAccCookie(loc[1]);
				} catch(ex) { }
			}
		});
});