/*
File: frandyhome/scripts.js
Updated: 11/03/09 8:29pm
8/18/09 - frandyhome - without transitions
9/19/09 - added votes & comments
9/22/09 - changed GetAjaxReq
11/3/09 - added email to UpdateCmt 
*/



var nextURLlnk = "";  // set in code once page is created
var slideTime = "";   // timer


function showMenu () {
  document.getElementById('slidediv').style.visibility = "visible";
}

function hideMenu () {
  document.getElementById('slidediv').style.visibility = "hidden";
}


function nextPage (now) {
   if (nextURLlnk != ""){
      var numseconds = getCookie('sstimer');
      var nextURL = "document.location='"+nextURLlnk+"';" ;
      if (now) {  // immediately jump to next page
          document.location = nextURLlnk;
      } else {
         if (numseconds > 0) {
            slideTime = setTimeout(nextURL,(numseconds*1000));
         }
      }
   } else {
      runSlideshow ( '' ); // turn off slideshow
   }
}


function runSlideshow ( numseconds ) {
   if (numseconds > 0) {
      setCookie('sstimer', numseconds);
      nextPage(true); // jump to next page now
   } else {
      clearTimeout(slideTime);
      setCookie('sstimer', ''); // turn off slideshow
   }
   document.getElementById('slidediv').style.visibility = "hidden";
}


function getCookie(Name) {
   var search = Name + "=";
   var returnvalue = "";
   if (document.cookie.length > 0) {
      offset = document.cookie.indexOf(search);
 		// if cookie exists
 		if (offset != -1) {
			offset += search.length;
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value
			if (end == -1) end = document.cookie.length;
         returnvalue = unescape(document.cookie.substring(offset, end));
 		}
   }
   return returnvalue;
}


function setCookie(NameOfCookie, cookvalue){
// http://www.elated.com/articles/javascript-and-cookies/
   if (cookvalue != "") {
      var exdate=new Date();
      exdate.setDate(exdate.getDate()+1); // add 1 day to kill cookie if user simply closes browser
      cookstr = NameOfCookie+"="+cookvalue+"; expires=" + exdate.toGMTString();
      document.cookie=cookstr;
   } else {
      var cookie_date = new Date ( );  // current date & time
      cookie_date.setTime ( cookie_date.getTime() - 1 ); // minus 1 second
      var cookstr = NameOfCookie+"="+cookvalue+"; expires=" + cookie_date.toGMTString();
      document.cookie = cookstr;
   }
}


function showInfo(divid) {
   document.getElementById(divid).style.visibility="visible";
}


function clearInfo(divid) {
   document.getElementById(divid).style.visibility="hidden";
}




function showpict (event, url2pict , offx, offy ) {
  // displays a picture near the mouse
  // offx, offy = offsets from mouse
  if (url2pict != "") {

    var posx = 0;
    var posy = 0;

    if (event.pageX || event.pageY) {
       posx = event.pageX;
       posy = event.pageY;
    } else if (event.clientX || event.clientY) {
       posx = event.clientX + document.body.scrollLeft;
       posy = event.clientY + document.body.scrollTop;
    }

    // posx and posy contain the mouse position relative to the document
    document.getElementById('pictimg').src = url2pict;
    ego = document.getElementById('pictdiv');
    ego.style.top = (posy + offy)+"px";
    ego.style.left = (posx + offx)+"px";
    ego.style.visibility="visible";
  } else {
    // move pict off the page
    ego = document.getElementById('pictdiv');
    ego.style.top = "-150px";
    ego.style.left = "-150px";
    document.getElementById('pictdiv').visibility="hidden";
  }
}



/* 4/9/08  */

function applyTransition ( id_cd ) {
    oImg = document.getElementById(id_cd);
    oImg.filters(0).Apply();
    oImg.style.visibility = "visible";
    oImg.filters(0).Play();
}


/* ==================  */
/*
FadeOpacity requires 5 parameters to define the animation:
elemId = The id attribute of the DOM object (or HTML entity) to animate.
fromOpacity = The starting opacity for the animation.
toOpacity = The ending opacity of the animation. This is the opacity the element will have when the animation ends.
time = The time the animation should take, in milliseconds. This should be divisible by the frames per second or it will be rounded to the next highest number that is divisible.
fps = The frames per second for the animation. A higher fps value means a smoother animation, but opacity changes can be processor-intensive on larger elements, so you could lower this if needed. 8 - 12 fps is a good quality setting.
*/

function SetOpacity(elem, opacityAsInt)
{
	var opacityAsDecimal = opacityAsInt;

	if (opacityAsInt > 100)
		opacityAsInt = opacityAsDecimal = 100;
	else if (opacityAsInt < 0)
		opacityAsInt = opacityAsDecimal = 0;

	opacityAsDecimal /= 100;

	if (opacityAsInt < 1)
		opacityAsInt = 1; // IE7 bug, text smoothing cuts out if 0

	elem.style.opacity = opacityAsDecimal;
	elem.style.filter  = "alpha(opacity=" + opacityAsInt + ")";
}

function FadeOpacity(elemId, fromOpacity, toOpacity, time, fps)
{
	var steps = Math.ceil(fps * (time / 1000));
	var delta = (toOpacity - fromOpacity) / steps;
	FadeOpacityStep(elemId, 0, steps, fromOpacity, delta, (time / steps));
}


function FadeOpacityStep(elemId, stepNum, steps, fromOpacity, delta, timePerStep)
{
    SetOpacity(document.getElementById(elemId), Math.round(parseInt(fromOpacity) + (delta * stepNum)));
    if (stepNum < steps)
        setTimeout("FadeOpacityStep('" + elemId + "', " + (stepNum+1) + ", " + steps + ", " + fromOpacity + ", " + delta + ", " + timePerStep + ");", timePerStep);
}



/*  added 9/19/09 */

/*  AJAX stuff */
var ajaxreq = null;          // used for AJAX stuff

function GetAjaxReq() {
   try { return new XMLHttpRequest(); } catch(e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
   alert("XMLHttpRequest not supported");
   return null;
}


/*   handle updating votes  */
function UpdateVote (puid,fld) {
    if (fld != '') {
      result = confirm ("Give the page a thumbs "+fld+"?");
      if (result != true) {return;}
    } else {return;}

    ajaxreq = GetAjaxReq();  // get AJAX request object
    if (ajaxreq == null) {return;}

    url = "ajaxupdvote.php?puid="+puid+"&fld="+fld+"&r="+Math.random();

    ajaxreq.onreadystatechange = function () {
      if (ajaxreq.readyState == 4) {
       if (ajaxreq.status == 200) {
         document.getElementById( 'votediv' ).innerHTML = ajaxreq.responseText;
       }
      }
     }

    ajaxreq.open("GET", url, true);
    ajaxreq.send( null );
}


/* handle updating comments */

function ExpandCmt () {
     if (document.getElementById('cmtform').style.display == 'none') {
        document.getElementById('cmtform').style.display = 'block';
        document.getElementById('cmticon').src = 'icons/icon_show.gif';
        document.getElementById('code').focus();
     } else {
        document.getElementById('cmtform').style.display = 'none';
        document.getElementById('cmticon').src = 'icons/icon_hide.gif';
     }
}


function CalcRemain () {
     var rem = document.getElementById('cmttext').value.length;
     if (rem < 501) {
       document.getElementById('remainderID').innerHTML = (500-rem);
     } else {
       document.getElementById('cmttext').value = document.getElementById('cmttext').value.substring(0,501);
       document.getElementById('remainderID').innerHTML = "<span style='color:red'>Exceeded 600 characters.</span>";
     }
}


function UpdateCmt (action,puid,uid) {
	ajaxreq = GetAjaxReq();
	if (ajaxreq == null) {return;}

	switch (action) {
	  case "A":
	   // cause it to be added
	   cmt = document.getElementById("cmttext").value;
	   code = document.getElementById("code").value;
	   key = document.getElementById("key").value;
	   entby = document.getElementById("enteredby").value;
	   email = document.getElementById("email").value;
	   if (cmt.length < 5) {
		 alert ("Now, now.\n\nYou must enter at least 5 characters\nfor this to be considered a comment.");
		 return;
	   }
	   if (code != key) {
		 alert ("You must enter the case 'sensitive' anti-spam code ( "+key+" ) to submit your comment.");
		 return;
	   }
	   cmt = encodeURIComponent(cmt);  // convert to handle special characters
	   entby = encodeURIComponent(entby);
	   code = encodeURIComponent(code);
	   email = encodeURIComponent(email);
	   document.getElementById('cmttext').value = "";
	   document.getElementById('cmtform').style.display='none';
	   document.getElementById('remainderID').innerHTML = 500;
	   document.getElementById('cmticon').src = 'icons/icon_hide.gif';
	   url = "ajaxupdcmt.php?action=A&puid="+puid+"&cmt="+cmt+"&entby="+entby+"&email="+email+"&r="+Math.random();
	   break;

	 case "D":
		conf = confirm ("Permanently delete this entry?");
		if (conf == false) {return;}
		url = "ajaxupdcmt.php?action=D&puid="+puid+"&uid="+uid+"&r="+Math.random();
	    break;

     default:
	 	alert ("Invalid operation: "+action);
	    return;
	}

	ajaxreq.onreadystatechange = function () {
		if (ajaxreq.readyState == 4) {
		 if (ajaxreq.status == 200) {
		   document.getElementById( 'cmtdiv' ).innerHTML = ajaxreq.responseText;
		 }
		}
	   }
	ajaxreq.open("GET", url, true);
	ajaxreq.send( null );
}





