// core JS code for the navigation pages

// preload images
if (document.images) {
    var buttonPlus = new Image();
    buttonPlus.src  = '/reports/images/icon_plus.gif';
    var buttonMinus = new Image();
    buttonMinus.src  = '/reports/images/icon_minus.gif';
}

// expand/collapse the sections and change button
function toggleDisplay(objectName) {
	var object = document.getElementById(objectName);
	var buttonName = objectName + '_btn';
	state = object.style.display;
	if (state == 'none') {
		object.style.display = 'block';
		document.images[buttonName].src=buttonMinus.src;
	} else if (state != 'none') {
		object.style.display = 'none';
		document.images[buttonName].src=buttonPlus.src;
	}
}

// expand/collapse the navigation divs
function toggleNavDisplay(linkName) {
	var linkObj = document.getElementById(linkName);
	linkDivName = linkName + "a";
	var linkDivObj = document.getElementById(linkDivName);
	childDivName = linkName + "b";
	var childDivObj = document.getElementById(childDivName);
	if (document.getElementById(childDivName)) {
		state = childDivObj.style.display;
		if (state == 'none') {
			childDivObj.style.display = 'block';
			linkObj.className='arrow_down';
			linkDivObj.className='arrow_down_bg';
		} else if (state != 'none') {
			childDivObj.style.display = 'none';
			linkObj.className='arrow_right';
			linkDivObj.className='arrow_right_bg';
		}
	}
}

// var to track last link clicked
var lastLink = "";

// this highlights the nav link
function linkHigh(objectName) {
	if (document.getElementById(objectName)) {
		var object = document.getElementById(objectName);
		object.className='linkHigh';
		if (lastLink != "") {
			var objectOff = document.getElementById(lastLink);
			objectOff.className='linkNormal';
		}
	}
	lastLink = objectName;
}

// for the nearby locations pulldown
function goto_URL(object) {
	if (object.options[object.selectedIndex].value !="") {
		window.location.href="/reports/report.cfm?id="+object.options[object.selectedIndex].value;	
	}
}

// for the nearby locations pulldown
function goto_travel_URL(object) {
	if (object.options[object.selectedIndex].value !="") {
		window.location.href="/reports/report_travel.cfm?id="+object.options[object.selectedIndex].value;	
	}
}

// for external weather links
function goto_weather(object) { 
	if (object.options[object.selectedIndex].value.length ==3) {
		alert ("Please Select Another Link");
	}
	else
		window.open(object.options[object.selectedIndex].value, 'externalLinks', 'width=600,height=600,resizable,scrollbars,status');
	}

// for tide locations
function goto_Xtide_Location(url) {
	window.open(url,'XtideLocation','width=600,height=600,scrollbars=yes');
}

// do local ads still exist???
function openLocalAdsInPopupWin(url, cd1, scde1) {
	if ( url ) {
		var redirectUrl = 'http://www.surfline.com/redirect/?url=' + escape(url) +
		 '&code=' + ((cd1) ? escape(cd1) : '') +
		 '&subcode=' + ((scde1) ? escape(scde1) : '');
		var features ="'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=390,height=650'";
		var newWin = window.open(redirectUrl, "win", features);
	}
}

// go to a page???
function gotopage(object){
	window.location.href = object.options[object.selectedIndex].value;
}

// AJAX http_request, when page loaded, calls the 'initNavigationLocation' function
function loadFragmentInToElement(fragment_url, element_id) {

       var http_request = false;

       if (window.XMLHttpRequest) { // Mozilla, Safari,...
           http_request = new XMLHttpRequest();
           if (http_request.overrideMimeType) {
               http_request.overrideMimeType('text/xml');
               // See note below about this line
           }
       } else if (window.ActiveXObject) { // IE
           try {
               http_request = new ActiveXObject("Msxml2.XMLHTTP");
           } catch (e) {
               try {
                   http_request = new ActiveXObject("Microsoft.XMLHTTP");
               } catch (e) {}
           }
       }

    var element = document.getElementById(element_id);
	
	if (!http_request) {
           element.innerHTML = '<em>Cannot Create XMLHTTP instance.</em>';
           return false;
       }

    http_request.open("GET", fragment_url);
    http_request.onreadystatechange = function() {
		if (http_request.readyState == 4 && http_request.status == 200) {
			element.innerHTML = http_request.responseText;
			// this opens the nav to the right report
			initNavigationLocation();
		} else {
			element.innerHTML = '<div style="padding-left:4px"><em>Loading Navigation...</em></div>';
		}
    }
	
    http_request.send(null);
	
}

// load navigation, THIS GETS CALLED ON PAGE LOAD
function loadNavigation() {
	loadFragmentInToElement('/reports/includes/include_navigation.htm','navigationDiv');
}

// load navigation, camera only
function loadNavigationCamOnly() {
	loadFragmentInToElement('/reports/includes/include_navigation_camonly.htm','navigationDiv');
}

// sets the navigation cookie
function setNavigationCookie(value) {
	lastLink = "";
	var expires = '13/06/2036 00:00:00';
	document.cookie = "NAVIGATIONTYPE=" + value + "; expires=" + expires + "; domain=.surfline.com; path=/";
}
