// JavaScript Document

var menuLinksBackOver = new Image();
menuLinksBackOver.src = "/images/menu-links-over.png";

var menuLinksVisited = new Image();
menuLinksVisited.src = "/images/visited.png";

var mainTopBack02 = new Image();
mainTopBack02.src = "/images/maincontentback01.jpg";


// ====================== START OF COOKIE FUNCTIONS
var TheDomain = "bufferedbrain.com";
var ThePath = "/"
var expDays = 30; // number of days the cookie should last
var menuHiddenOrNot;

function GetCookie (name) {
  var arg = name + "=";
  var arguement_length = arg.length;
  var cookie_length = document.cookie.length;
  var i = 0;
  while (i < cookie_length) {
    var j = i + arguement_length;
    if (document.cookie.substring(i, j) == arg)
    return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}

function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {
  var exp = new Date();
  exp.setTime (exp.getTime() - 1);
  var cval = GetCookie (name);
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function amt(){
  var count = GetCookie('count')
  if(count == null) {
    SetCookie('count','1')
    return 1
  } else {
    var newcount = parseInt(count) + 1;
    DeleteCookie('count')
    SetCookie('count',newcount,exp)
    return count
  }
}

function getCookieVal(offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
  endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}


var user_old_or_new = "new";

function checkCount() {
  var count = GetCookie('count');
  if (count == null) {
    count=1;
    SetCookie('count', count, exp, ThePath, TheDomain);
	user_old_or_new = "new";
	return user_old_or_new;
  }
  else if (count <= 2) {
    count++;
    SetCookie('count', count, exp, ThePath, TheDomain);
	user_old_or_new = "new";
	return user_old_or_new;
  } else {
    count++;
    SetCookie('count', count, exp, ThePath, TheDomain);
	user_old_or_new = "old";
	return user_old_or_new;
  }
}

//window.onload=checkCount;
checkCount();
// ====================== END COOKIE SCRIPT

function setLastDiv(which) {
	lastDiv = which;
}

var running = false;
var whati=-50;

function bringItUp() {
	if (whati >= -100) {
		document.getElementById('whatint').childNodes[0].nodeValue = whati;
		document.getElementById(lastDiv).style.left = whati*5+"px";
		whati--;
		whatsTheNumber = whati;
		return whatsTheNumber;
	}
		running = false;
}


function bringItDown(startingLocation) {
		whichDivToMove.style.zIndex = "70";
	if (whati <= 30) {
		whichDivToMove.style.left = whati*10+"px";
		whati++;
		running = true;
		return running;
	} else {
		whati=-50;
		window.clearInterval(downInterval);
		running = false;
		return running;
	}
}


var lastDiv = "resSection01";
var upInterval;
var downInterval;
var whichDivToMove;

function hide_last(whichtomove) {
	if ((lastDiv != null) || (lastDiv != whichtomove)) {
		window.clearInterval(downInterval);
		upInterval = setInterval(bringItUp,5)
		running = true;
	}
}

function the_interval(upOrDown,whichtomove) {
	if ((lastDiv != null) && (lastDiv != whichtomove)) {
		document.getElementById(lastDiv).style.zIndex="50";
		document.getElementById(lastDiv).style.display="none";
	}
	if ((upOrDown == "down") && (running == false) && (whati <= 10) && (lastDiv != whichtomove)) {
		whichDivToMove = document.getElementById(whichtomove);
		document.getElementById(whichtomove).style.display="inline";
//		window.clearInterval(upInterval);
		downInterval = setInterval(bringItDown,1)
		running = true;
	}
	if ((lastDiv != whichtomove) || (lastDiv == null)) {
		setLastDiv(whichtomove);
		document.getElementById(lastDiv).style.zIndex="50";
	}
	running = false;
}


function checkPosition() {
  var mapPositionTop = GetCookie('mapPositionTop');
  var mapPositionLeft  = GetCookie('mapPositionLeft');
  menuHiddenOrNot = GetCookie('menuHiddenOrNot');
  if ((mapPositionTop == null) || (mapPositionLeft == null)) {
	mapPositionTop = document.getElementById('themenu').style.top;
	mapPositionLeft = document.getElementById('themenu').style.left;
    SetCookie('mapPositionLeft', 50, exp, ThePath, TheDomain);
    SetCookie('mapPositionTop', 130, exp, ThePath, TheDomain);
	if ((menuHiddenOrNot != "yes") || (document.URL.indexOf("mymovies.php") != -1)) {
	document.getElementById('themenu').style.display = "inline";
	}
//	document.getElementById('wheresitat').childNodes[0].nodeValue = mapPositionLeft;
  } else {
	document.getElementById('themenu').style.top = mapPositionTop;
	document.getElementById('themenu').style.left = mapPositionLeft;
	if ((menuHiddenOrNot != "yes") || (document.URL.indexOf("mymovies.php") != -1)) {
	document.getElementById('themenu').style.display = "inline";
	}
//	mapPositionTop = document.getElementById('themenu').style.top;
//	mapPositionLeft = document.getElementById('themenu').style.left;
    //document.getElementById('themenu').style.top = mapPositionTop;
//    SetCookie('mapPositionTop', mapPositionTop, exp, ThePath, TheDomain);
    //document.getElementById('themenu').style.left = mapPositionLeft;
//    SetCookie('mapPositionLeft', mapPositionLeft, exp, ThePath, TheDomain);
  }
}

function setPosition() {
	mapPositionTop = document.getElementById('themenu').style.top;
	mapPositionLeft = document.getElementById('themenu').style.left;
    SetCookie('mapPositionTop', mapPositionTop, exp, ThePath, TheDomain);
    SetCookie('mapPositionLeft', mapPositionLeft, exp, ThePath, TheDomain);
	if (menuHiddenOrNot != "yes") {
	document.getElementById('themenu').style.display = "inline";
	}
}

function reSetPosition() {
	document.getElementById('themenu').style.top = "130px";
	document.getElementById('themenu').style.left = "50px";
	mapPositionTop = document.getElementById('themenu').style.top;
	mapPositionLeft = document.getElementById('themenu').style.left;
    SetCookie('mapPositionTop', mapPositionTop, exp, ThePath, TheDomain);
    SetCookie('mapPositionLeft', mapPositionLeft, exp, ThePath, TheDomain);
	document.getElementById('themenu').style.display = "inline";
	menuHiddenOrNot = "no";
	SetCookie('menuHiddenOrNot', menuHiddenOrNot, exp, ThePath, TheDomain);
	document.getElementById('mainheaderback').src = "/images/maincontentback-01.png";
}



var draggedElem;
var offsetX = 0;
var offsetY = 0;
function dragIt(evt) {
	evt = (evt) ? evt : (window.event) ? window.event : "";
	var targElem = document.getElementById('themenu'); //(evt.target) ? evt.target : evt.srcElement;
	var sourceElem = (evt.target) ? evt.target : evt.srcElement;//document.getElementById('move_me');
	if (draggedElem) {
		targElem = draggedElem;
		if (sourceElem.className == "draggable") {
			while (targElem.id != "themenu" && targElem.parentNode) {
				targElem = targElem.parentNode;
			}
			if (evt.pageX) {
				targElem.style.left = evt.pageX - offsetX - 4 + "px";
				targElem.style.top = evt.pageY - offsetY - 4 + "px"; // for all other Browsers
//				document.getElementById('x-y').childNodes[0].nodeValue = evt.pageX+"  "+evt.pageY;
			} else {
				targElem.style.left = evt.clientX - offsetX - 12 + "px";
				targElem.style.top = evt.clientY - offsetY - 12 + "px"; // for Internet Explorer
//				document.getElementById('theposition').childNodes[0].nodeValue = evt.clientY+" "+evt.clientX;
//				document.getElementById('x-y').childNodes[0].nodeValue = evt.clientX+"  "+evt.clientY;
			}
			return false;
		}
	}
}

function engage(evt) {
	evt = (evt) ? evt : (window.event) ? window.event : "";
	var targElem = document.getElementById('themenu'); //(evt.target) ? evt.target : evt.srcElement;
	var sourceElem = (evt.target) ? evt.target : evt.srcElement;//document.getElementById('move_me');
	if (sourceElem.className == "draggable") {
		while (targElem.id != "themenu" && targElem.parentNode) {
			targElem = targElem.parentNode;
		}
		if (targElem.id == "themenu") {
			draggedElem = targElem;
			if (evt.pageX) {
				offsetX = evt.pageX - targElem.offsetLeft;
				offsetY = evt.pageY - targElem.offsetTop;
//				document.getElementById('offset-x-y').childNodes[0].nodeValue = offsetX+"  "+offsetY;
			} else {
				offsetX = evt.offsetX - document.body.scrollLeft;
				offsetY = evt.offsetY - document.body.scrollTop;
//				document.getElementById('offset-x-y').childNodes[0].nodeValue = offsetX+"  "+offsetY;
				if (navigator.userAgent.indexOf("Win") == -1) {
					offsetX += document.body.scrollLeft;
					offsetY += document.body.scrollTop;
//					document.getElementById('offset-x-y').childNodes[0].nodeValue = offsetX+"  "+offsetY;
				}
			}
			return false;
		}
	}
}

function release(evt) {
//	evt = (evt) ? evt : (window.event) ? window.event : "";
//	var targElem = document.getElementById('themenu'); //(evt.target) ? evt.target : evt.srcElement;
//	var sourceElem = (evt.target) ? evt.target : evt.srcElement;//document.getElementById('move_me');
//	if (sourceElem.className == "draggable") {
//		while (targElem.id != "themenu" && targElem.parentNode) {
//			targElem = targElem.parentNode;
//		}
//		if (draggedElem && targElem.id == "themenu") {
			setPosition();
			draggedElem = null;
//		}
//	}
}


//function showNews() {
//	document.write
//	document.getElementById('newsPanel').style.display = "block";
//}
// user_old_or_new
//var new_or_not = "yes";
function showNews(what) {
//window.alert("hello");
//	if (form.input.value) {
	if (user_old_or_new == "new") {
		var newItem = document.createElement("div");
		var newItem2 = document.createElement("p");
		newItem2.appendChild(document.createTextNode('Welcome to my page! There is a new look and feel enjoy exploring my pages! '));
		document.getElementById("container").appendChild(newItem);
		newItem.id = "newsPanel";
		document.getElementById("newsPanel").appendChild(newItem2);
		newItem2.id = "newsPanelP";
	}
}



function runMaster() {
	checkPosition();
	window.onbeforeunload = function () {
		// This fucntion does nothing.  It won't spawn a confirmation dialog
		// But it will ensure that the page is not cached by the browser.
	}
	showNews();
}

function hideMenu() {
	document.getElementById('themenu').style.display = "none";
	menuHiddenOrNot = "yes";
	SetCookie('menuHiddenOrNot', menuHiddenOrNot, exp, ThePath, TheDomain);
	document.getElementById('mainheaderback').src = "/images/maincontentback-02.png";
}

function ShowPicture(whichpicture,whichname) {
	document.getElementById('portfolioPictureMain').src = whichpicture;
	if (whichname == "") {whichname = "pictures";} 
	document.getElementById('imageNameField').childNodes[0].nodeValue = whichname;
}

// START OF THE MOVIE HANDLING SCRIPT

var whichbrowser = navigator.appName;


function showMovieInfo(name,year,rating,genre,notes) {
	document.getElementById('MovieDTName').childNodes[0].nodeValue = name;
	document.getElementById('MovieDTYear').childNodes[0].nodeValue = year+" "+rating+" "+genre;
//	document.getElementById('MovieDTGenre').childNodes[0].nodeValue = genre;
	document.getElementById('MovieDTNotes').childNodes[0].nodeValue = notes;
}
function SetMovieToBuy(name) {
	document.getElementById('BuyMenuSub').childNodes[0].nodeValue = "Buy "+name+" now from Amazon.com";
	document.getElementById('BuyMovie').href = "http://www.amazon.com/gp/search?ie=UTF8&keywords="+name+"&tag=bufferedbrain-20&index=dvd&linkCode=ur2&camp=1789&creative=9325";
}

function  hideMe(what) {
	what.style.display = "none";
}

function hideMovie(whatMovie) {
	var whatitem;
	if (whichbrowser.indexOf("Microsoft") != -1){
		whatitem = whatMovie.parentNode.parentNode.childNodes[1];
	} else {
		whatitem = whatMovie.parentNode.parentNode.childNodes[2];
	}
	whatitem.style.display = "none";
}



var MyTop = 167;
var MyVari = 0;
var isItRunning;
var MyMovieInterval;
function startIt() {
	document.getElementById('MoviePicture').style.height = (MyVari*4)+"px";
	document.getElementById('MoviePicture').style.top = MyTop+"px";
	isItRunning = true;
	if (MyVari >= 54) {
		window.clearInterval(MyMovieInterval);
		isItRunning = false;
		MyVari = 0;
		MyTop = 167;
	}
	MyVari++;
	MyTop= (MyTop - 2);
}

function StartMyMovieInterval(whatMoviePicture) {
	if (MyMovieInterval) {
		window.clearInterval(MyMovieInterval);
		isItRunning = false;
//		window.alert("hello");
	}
	document.getElementById('MoviePicture').src = whatMoviePicture;
	if ((isItRunning != true) && (MyVari <= 54)) {
		MyMovieInterval = window.setInterval(startIt,1);
		document.getElementById('MoviePicture').style.height = "0px";
		document.getElementById('MoviePicture').style.top = "150px";
	} else {
		MyVari = 0;
		MyTop = 167;
	}
}
