// To Do: Refactor (a lot)

$(function(){

	$('#navigator').fadeIn(2000);
	$('#accordion').fadeIn(2000);
	$('#accordionopen').fadeIn(2000);

	$("#accordion").accordion({
		autoHeight: false,
		collapsible: true,
		active: false,
		navigation: true
	});

	$("#accordionopen").accordion({
		autoHeight: false
	});

	$("a[class|=activate]").click(function() {
		number = parseInt($(this).attr('class').replace(/.*activate-/, '' ).replace(/\s.*/, '' ));
		$("#accordion").accordion('activate', number);
	});

	portheight = parseInt(document.documentElement.clientHeight) - 154;
	if (navigator.plugins && navigator.plugins["Shockwave Flash"]){
		fv = parseInt(navigator.plugins["Shockwave Flash"].description.match(/[\d]+/));
	} else {
		fv = 0;
	}
	if (fv < 10) {
		$('#flashmap').css("display", "none");
	} else {
		$('#noplug').css("display", "none");
	}
	$('#flash').css("height", portheight);

	$(".choice select").change(function() {
		location.href = $(this).val();
	});

	$("#imagePop").dialog({
		autoOpen: false,
		modal: true,
		draggable: false,
		resizable: false,
		show: 'blind',
		hide: 'blind',
		position: ["left", "top"]
		// width: parseInt($(this).attr('class').replace(/image /, '' ))
	});

	$('#chart a.image').click(function() {
		name = $(this).attr('id');
		file = "chart/" + $(this).attr('id') + ".jpg";
		$("#imagePop").dialog('open').html('<img id="pop" src="' + file + '" />');
		$("#pop").load(function (){
			iwidth = parseInt($(this).width());
			iheight = parseInt($(this).height());
			$("#imagePop").dialog({ width: iwidth + 25, height: iheight + 40, title: name });
			$(this).width(iwidth).height(iheight);
		});
		return false;
	});

	$('#camloader').load('http://www.portsanluis.com/webcam/loader.php');

	$('.ahlink').click(function() {
		window.location.hash = $(this).attr('href');
	});

	function update_cache() {
		$('#update_cache').load('/weather/update-cache.php?r=' + Math.random(), function() {
			$('.section_update').each(function() {
				section_file = $(this).attr('href').replace(/#/, '' ) + '.php';
				section_tag = $(this).parent().next('div');
				$('#webcam').attr('src', '/_resources/images/640x480.gif');
				$(section_tag).load('/weather/' + section_file + '?r=' + Math.random(), function() {
					$('#webcam').attr('src', '/_resources/images/640x480.gif');
					$('#webcam').attr('src', 'http://76.213.215.65/-wvhttp-01-/getoneshot?r=' + Math.random());
				});
			});
			$('.image_update').each(function() {
				if ($(this).is('.640x480')) {
					loading_file = '640x480.gif';
				} else if ($(this).is('.768x384')) {
					loading_file = '768x384.gif';
				} else if ($(this).is('.600x550')) {
					loading_file = '600x550.gif';
				} else {
					loading_file = 'loading.gif';
				}
				image_tag = $(this).parent().next('div').children('p').children('a').children('img');
				image_tag.attr('src', '/_resources/images/' + loading_file);
				if ($(this).is('.gif')) {
					image_file = '/weather/cache/' + $(this).attr('href').replace(/#/, '' ) + '.gif' + '?r=' + Math.random();
				} else {
					image_file = '/weather/cache/' + $(this).attr('href').replace(/#/, '' ) + '.png' + '?r=' + Math.random();
				}
				image_tag.attr('src', image_file);
			});
		});
	};


	$('a').click(function() {
		if ($(this).attr('href').match("http://")) {
			window.open($(this).attr('href'));
			return false;
		}
	});

	update_cache();
	setInterval(function() {
		update_cache();
	}, 60000);

	// if (!navigator.userAgent.match("Opera")) {
		if ("onhashchange" in window) {
			$(window).bind('hashchange', function() {
				if (location.hash == "") {
					history.back();
				} else {
					$("\'a[href$=\"" + location.hash + "\"]\'").click();
				}
			});
		}
		
	});

	// $(window).bind('hashchange', function() {
		// 	if window.location.href.replace(/#.*$/g, "") == document.referrer
		// 	alert(window.location.href.replace(/#.*$/g, ""));
		// 	alert(document.referrer);
		// 	$("\'a[href$=\"" + location.hash + "\"]\'").click();
		// 	history.back();
		// });


		// setInterval(function() {
			// 	$('#callnoaa').load('http://www.portsanluis.com/weather/callnoaa.php?r=' + Math.random(), function() {
				// 		$('.loading').each(function() {
					// 			name = $(this).attr('id');
					// 			idname = '#' + name;
					// 			imagename = name.replace(/_/, '.' );
					// 			$(idname).attr('src', '/weather/' + imagename + '?r=' + Math.random());
					// 			$('.loading').fadeIn('slow');
					// 		});
					// 	});
					// 	}, 60000);

					// $('#chart a.image').click(function() {
						// 	name = $(this).attr('id') + " (click to zoom)";
						// 	file = "chart/" + $(this).attr('id') + ".jpg";
						// 	$("#imagePop").dialog('open').html('<img id="pop" src="' + file + '" />');
						// 	$("#pop").load(function (){
							// 		pwidth = parseInt(document.documentElement.clientWidth) * 0.9;
							// 		pheight = parseInt(document.documentElement.clientHeight) * 0.9;
							// 		iwidth = parseInt($(this).width());
							// 		iheight = parseInt($(this).height());
							// 		ratio = 0;
							// 		if (iwidth > pwidth) {
								// 			ratio = pwidth / iwidth;
								// 			iheight = iheight * ratio;
								// 			iwidth = iwidth * ratio;
								// 		}
								// 		if (iheight > pheight) {
									// 			ratio = pheight / iheight;
									// 			iheight = iheight * ratio;
									// 			iwidth = iwidth * ratio;
									// 		}
									// 		$("#imagePop").dialog({ width: iwidth + 25, height: iheight + 40, title: name });
									// 		$(this).width(iwidth).height(iheight);
									// 	});
									// 	return false;
									// });


