/*
function getCookie(c_name){
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
		}
	}

	return "";
}

function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function refresh(){
	window.location.href = window.location.pathname;
}
*/

function changeOver(start, id, path){

	startTimer = start * 1000;
	
	var methodName = "genericPic('" + start + "', '" + id + "', '" + path + "')";
	
	window.setTimeout('pleaseFade("' + id + '", "' + methodName + '")', startTimer);
	
	/**
	//old changeOver
	startTimer = start * 1000;

	var methodName = "genericPic('" + path + "', '" + id + "', '" + start + "')";

	window.setTimeout('fade("' + id + '", "' + methodName + '")', startTimer);
	*/
}

function pleaseFade(id, callBack){
	$('#' + id).fadeOut(1500, function(){
		eval(callBack);
	});
}

function pleaseAppear(id, callBack){
	$('#' + id).fadeIn(1500, function(){
		eval(callBack);
	});
}

/*
function fade(id, methodName){
	var o, p, r;

	var speedIE = 3;
	var speedNotIE = .01;

	if(document.getElementById(id).style.opacity == "" || document.getElementById(id).style.opacity == null || document.getElementById(id).style.opacity * 1 > 1)
		document.getElementById(id).style.opacity = 1;
	if(document.getElementById(id).style.filter == "" || document.getElementById(id).style.filter == null || document.getElementById(id).style.filter.slice(15, document.getElementById(id).style.filter.length - 1) * 1 > 100)
		document.getElementById(id).style.filter = "alpha(opacity = 100)";

	o = document.getElementById(id).style.opacity * 1 - speedNotIE;
	p = document.getElementById(id).style.filter;
	if(p != null)
		r = p.slice(15, p.length - 1) * 1 - speedIE;

	if(o > 0 - speedNotIE && o < 0){
		o = 0;
	}

	if(p != null && p != "" && r != null && r > 0 - speedIE && r < 0){
		r = 0;
	}

	//if not using filter and opacity is greater than or equal to 0
	//or is using filter and filter opacity is greater than or equal to 0
	if((navigator.appName != "Microsoft Internet Explorer" && o * 1 >= 0) || (p != null && p != "" && r != null && r * 1 >= 0)){
		if(document.getElementById(id) != null){
			if(o * 1 >= 0){
				document.getElementById(id).style.opacity = o;
			}
			if(p != null && p != "" && r != null && r * 1 >= 0){
				document.getElementById(id).style.filter = "alpha(opacity = " + r + ")";
			}
		}

		var method = "fade('" + id + "', " + '"' + methodName + '"' + ")";

		window.setTimeout(method, 10);
	}else{
		window.setTimeout(methodName, 10);
	}
}

function changeOverVerify(start, id, methodName){
	startTimer = start * 1000;

	window.setTimeout("fade('" + id + "', '" + methodName + "')", startTimer);
}

function appear(id, methodName){
	var o, p, r;

	var speedIE = 3;
	var speedNotIE = .01;

	if(document.getElementById(id).style.opacity == "" || document.getElementById(id).style.opacity == null || document.getElementById(id).style.opacity * 1 <= 0)
		document.getElementById(id).style.opacity = 0;
	if(document.getElementById(id).style.filter == "" || document.getElementById(id).style.filter == null || document.getElementById(id).style.filter.slice(15, document.getElementById(id).style.filter.length - 1) * 1 <= 0)
		document.getElementById(id).style.filter == "alpha(opacity = 0)";

	o = document.getElementById(id).style.opacity * 1 + speedNotIE;
	p = document.getElementById(id).style.filter;
	if(p != null)
		r = p.slice(15, p.length - 1) * 1 + speedIE;

	if(o < 1 + speedNotIE && o > 1){
		o = 1;
	}

	if(p != null && p != "" && r != null && r < 100 + speedIE && r > 100){
		r = 100;
	}

	if((navigator.appName != "Microsoft Internet Explorer" && o <= 1) || (p != null && p != "" && r != null && r <= 100)){
		if(document.getElementById(id) != null){
			if(o <= 1){
				document.getElementById(id).style.opacity = o;
			}
			if(p != null && p != "" && r != null && r <= 100){
				document.getElementById(id).style.filter = "alpha(opacity = " + r + ")";
			}
		}

		var method = "appear('" + id + "', '" + methodName + "')";
		window.setTimeout(method, 10);
	}else{
		window.setTimeout(methodName, 10);
	}
}

function changeMainPic(){
	var xm;
	if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xm=new XMLHttpRequest();
	}else{
		if (window.ActiveXObject){
			// code for IE6, IE5
			xm=new ActiveXObject("Microsoft.XMLHTTP");
		}else{
			alert("Your browser does not support XMLHTTP!");
		}
	}

	if(xm != null){
		xm.onreadystatechange=function(){
			if(xm.readyState==4){
				if(xm.responseText){
					if(document.getElementById("main") != null){
						document.getElementById("main").src = "images/MainPics/"+xm.responseText;

						appear('main', 'changeOver("8", "main", "changeMainPic()")');
					}
				}
			}
		}

		if(document.getElementById("main") != null){
			xm.open("GET", "ajaxFiles/mainPic.php?current=" + document.getElementById("main").src, true);
			xm.send(null);
		}
	}
}


function genericPic(start, id, path){
	var xm; 
	try { 
		// Firefox, Opera 8.0+, Safari 
		xm=new XMLHttpRequest(); 
	} catch (e) { 
		// Internet Explorer 
		try { 
			xm=new ActiveXObject("Msxml2.XMLHTTP"); 
		} catch (e) { 
			try { 
				xm=new ActiveXObject("Microsoft.XMLHTTP"); 
			} catch (e) { 
				alert("Your browser does not support ajax!"); 
			} 
		} 
	}

	if(xm != null){
		xm.onreadystatechange=function(){
			if(xm.readyState==4){
				if(xm.responseText != null && xm.responseText != ""){
					if(document.getElementById(id) != null){
						document.getElementById(id).src = xm.responseText;
						if(document.getElementById(id + "Space"))
							document.getElementById(id + "Space").src = xm.responseText;

						var method = "changeOver('" + start + "', '" + id + "', '" + path + "')";
						var methodCall = 'pleaseAppear("' + id + '", "' + method + '")';
						
						/*
						//old code
						var methodCall = "callAppear('" + id + "', '" + time + "', '" + path + "')";
						
						
						setTimeout(methodCall, 1000);						
					}
				}else{
					genericPic(path, id, start);
				}
			}
		}

		if(document.getElementById(id) != null){
			var now = new Date(); 
			var uts = now.getTime(); 
			var current = document.getElementById(id).src.substring(document.getElementById(id).src.indexOf("/", 7) + 1); // last change

			xm.open("GET", "ajaxFiles/genPic.php?path=" + path + "&current=" + current, true);
			xm.send(null);
		}
	}
}
*/

function genericPic(start, id, path){

	if(document.getElementById(id) != null){
		var now = new Date(); 
		var uts = now.getTime(); 
		var current = document.getElementById(id).src.substring(document.getElementById(id).src.indexOf("/", 7) + 1); // last change

		$.post("ajaxFiles/genPic.php", {path: path, current: current}, function(data){
			
			if(document.getElementById(id) != null){
				document.getElementById(id).src = data;
				if(document.getElementById(id + "Space"))
					document.getElementById(id + "Space").src = data;

				var method = "changeOver('" + start + "', '" + id + "', '" + path + "')";
				var methodCall = 'pleaseAppear("' + id + '", "' + method + '")';
						
				setTimeout(methodCall, 1000);						
			}
		});
	}
}

/*
function callAppear(id, time, path){
	appear(id, 'changeOver(' + time + ', "' + id + '", "' + path + '")');
}
*/

//photos functions
window.onload = function(){
	changeOver(8, 'm', 'images/headPic');

	if(document.getElementById("months")){
		countDown();
	}

	if(document.getElementById("picMenu")){
       	var strCook = getCookie("picMenuPosition");
		if(strCook.indexOf("!~")!=0){
			var intS = strCook.indexOf("!~");
			var intE = strCook.indexOf("~!");
			var strPos = strCook.substring(intS+2,intE);
			document.getElementById("picMenu").scrollTop = strPos;
		}
	}
}

function SetDivPosition(){
	if(document.getElementById("picMenu")){
		var intY = document.getElementById("picMenu").scrollTop;
			
		setCookie("picMenuPosition", "yPos=!~" + intY + "~!", 1);
	}
}

function changePic(id){
	var image = id.href.substring(id.href.indexOf("?") + 1);
	setCookie("mainPic", image.substring(image.indexOf("=") + 1), 1);
	document.getElementById("mainPic").src = "http://www.theminnesotafeis.com/gallery/picture-gallery/" + image.substring(image.indexOf("=") + 1);
	getNewCaption();
	return false;
}
function getNewCaption(){
	if(document.getElementById("mainPic") != null && document.getElementById("mainPic").src != null && document.getElementById("mainPic").src != ""){
		var xm; 
		try { 
			// Firefox, Opera 8.0+, Safari 
			xm=new XMLHttpRequest(); 
		} catch (e) { 
			// Internet Explorer 
			try { 
				xm=new ActiveXObject("Msxml2.XMLHTTP"); 
			} catch (e) { 
				try { 
					xm=new ActiveXObject("Microsoft.XMLHTTP"); 
				} catch (e) { 
					alert("Your browser does not support ajax!");
				} 
			} 
		}
	
		if(xm != null){
			xm.onreadystatechange=function(){
				if(xm.readyState==4){
					if(xm.responseText != null && xm.responseText != ""){
						document.getElementById("picCaption").innerHTML = "<p>" + xm.responseText + "</p>";
					}else{
						getNewCaption();
					}
				}
			}

			if(document.getElementById("picCaption") != null){
				var current = document.getElementById("mainPic").src.substring(document.getElementById("mainPic").src.indexOf("/", 7) + 1);

				xm.open("GET", "ajaxFiles/picCaption.php?current=" + current, true);
				xm.send(null);
			}
		}
	}else{
		if(document.getElementById("picCaption") != null){
			document.getElementById("picCaption").innerHTML = "<p></p>";
		}
	}
}

function countDown(){
	var feisString = "September 25 2010 9:00:00";
	var now = new Date();
	var feis = new Date(feisString);

	var monthsLeft = (feis.getMonth() + 1) - (now.getMonth() + 1);
	if(monthsLeft <= 0){
		document.getElementById("months").innerHTML = 0;
	}else{
		document.getElementById("months").innerHTML = monthsLeft;
	}

	if(now.getDate() < feis.getDate()){
		document.getElementById("days").innerHTML = feis.getDate() - now.getDate();
	}else{
		document.getElementById("days").innerHTML = (daysInMonth(now.getMonth(), now.getFullYear()) - now.getDate()) + feis.getDate();

		document.getElementById("months").innerHTML = document.getElementById("months").innerHTML * 1 - 1;
	}

	if((now.getHours() + 1) < (feis.getHours() + 1)){
		document.getElementById("hours").innerHTML = (feis.getHours() + 1) - (now.getHours() + 1);
	}else{
		document.getElementById("hours").innerHTML = (24 - (now.getHours() + 1)) + (feis.getHours() + 1);

		document.getElementById("days").innerHTML = document.getElementById("days").innerHTML * 1 - 1;
	}

	if((now.getMinutes() + 1) < (feis.getMinutes() + 1)){
		document.getElementById("minutes").innerHTML = (feis.getMinutes() + 1) - (now.getMinutes() + 1);
	}else{
		document.getElementById("minutes").innerHTML = (60 - (now.getMinutes() + 1)) + (feis.getMinutes() + 1);

		document.getElementById("hours").innerHTML = document.getElementById("hours").innerHTML * 1 - 1;
	}

	if((now.getSeconds() + 1) < (feis.getSeconds() + 1)){
		document.getElementById("seconds").innerHTML = (feis.getSeconds() + 1) - (now.getSeconds() + 1);
	}else{
		document.getElementById("seconds").innerHTML = (60 - (now.getSeconds() + 1)) + (feis.getSeconds() + 1);

		document.getElementById("minutes").innerHTML = document.getElementById("minutes").innerHTML * 1 - 1;
	}

	if(document.getElementById("months").innerHTML * 1 < 0){
		document.getElementById("months").innerHTML = "00";
	}
	if(document.getElementById("days").innerHTML * 1 < 0){
		document.getElementById("days").innerHTML = "00";
	}
	if(document.getElementById("hours").innerHTML * 1 < 0){
		document.getElementById("hours").innerHTML = "00";
	}
	if(document.getElementById("minutes").innerHTML * 1 < 0){
		document.getElementById("minutes").innerHTML = "00";
	}
	if(document.getElementById("seconds").innerHTML * 1 < 0){
		document.getElementById("seconds").innerHTML = "00";
	}

	window.setTimeout("countDown()", 1);
}

function daysInMonth(iMonth, iYear){
	return 32 - new Date(iYear, iMonth, 32).getDate();
}