// flip panels open and closed...
var fxSpeed = 11;
var stepSize = 28;
var exp = new Date();
exp.setTime(exp.getTime() + (24*60*60*1000)); //one day

function positionThis(cssID,desiredPosition,property,currentPosition){ // currentPosition optional, without can run as standalone
	if (document.getElementById){ 
		if(!currentPosition) var currentPosition = desiredPosition;
		document.getElementById(cssID).style[property] = currentPosition; 
		if(currentPosition != desiredPosition) animateElement(cssID,desiredPosition,property,currentPosition);
	}
}

function animateElement(cssID,desiredPosition,property,currentPosition) {	
	if(!currentPosition) {
		if (window.getComputedStyle) {
			var thisElement = document.getElementById(cssID);
			var currentPosition = parseInt(document.defaultView.getComputedStyle(thisElement,null).getPropertyValue(property));
		} else {
			var currentPosition = parseInt(document.getElementById(cssID).currentStyle[property]);
		}
		stepSize = stepSize * -1;
	} else {
		currentPosition = parseInt(currentPosition);
	}
	currentPosition += stepSize;
	currentPosition = currentPosition + 'px';
	if (stepSize < 0){
		if (parseInt(currentPosition) > parseInt(desiredPosition)) {setTimeout(function() {positionThis(cssID,desiredPosition,property,currentPosition)}, fxSpeed); }
		else {setTimeout(function() {positionThis(cssID,desiredPosition,property)}, fxSpeed);}
	} else {
		if (parseInt(currentPosition) < parseInt(desiredPosition)) setTimeout(function() {positionThis(cssID,desiredPosition,property,currentPosition)}, fxSpeed)
		else setTimeout(function() {positionThis(cssID,desiredPosition,property)}, fxSpeed);
	}
}

function tripVisible(cssID,elemState){ 
	if (document.getElementById){ 
		document.getElementById(cssID).style.visibility=elemState; 
	} 
}
function tripDisplay(cssID,elemState){ 
	if (document.getElementById){ 
		document.getElementById(cssID).style.display=elemState; 
	} 
}

// set and maintain background aspect ratio, fit to window
var resizeBg = function(){ 
	var screenHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
	var screenWidth = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;
	var thisImage = document.getElementById('backgroundImage');  // calculate screen/image width slop, to optimize for a full screen experience
	var imageAspectRatio = thisImage.offsetHeight / thisImage.offsetWidth;
	var newImageHeight = screenWidth * imageAspectRatio;
	var heightWaste = newImageHeight - screenHeight;
	var lostPXtolerance = (imageAspectRatio >= .70 && imageAspectRatio <= .80) ? 240 : 100;	// allow upto 220 pix overflow for slightly more boxed format, else 90
	if (heightWaste >=  0 && heightWaste <= lostPXtolerance) {
		document.getElementById('backgroundImage').style.width='100%'; 
		document.getElementById('backgroundImage').style.height='auto'; 
	} else {
		document.getElementById('backgroundImage').style.width='auto'; 
		document.getElementById('backgroundImage').style.height='100%'; 
	}
} 

function setBG(img) {
	if (document.getElementById('backgroundImage')) {
		document.getElementById('backgroundImage').src=img;
		rememberImage(img);
	}
}

function getCookie(name) {
    var sPos = document.cookie.indexOf(name + "=");
    var len = sPos + name.length + 1;
    if((!sPos) && (name != document.cookie.substring(0, name.length))){
        return null;
    }
    if(sPos == -1){
        return null;
    }
    var ePos = document.cookie.indexOf(';', len);
    if(ePos == -1) ePos = document.cookie.length;
    return unescape(document.cookie.substring(len, ePos));
} 

function rememberImage(img) {
	document.cookie = "rememberImage=" + img + ";path=/; expires=" + exp.toGMTString();
}

function useRememberedImage() {
	var thisImg = getCookie('rememberImage');
	if (thisImg) {
		setBG(thisImg);
	}
}


