//Fading frame size
var frameSize = 50;

function content(targetID, valueID) {
	target = document.getElementById(targetID);
	value = document.getElementById(valueID);
	target.innerHTML = value.innerHTML;
}

function eraseDiv(targetID) {
    	target = document.getElementById(targetID);
	target.innerHTML = "";
}

function siteIframe(address) {
	value = '<a class="orange" onClick="fade(\'dim\',500,0,80,0);fade(\'table1\',500,0,100,0);setTimeout(\'eraseDiv(\\\'pictureContent\\\');\',500);">Close Window</a><br /><br /><iframe class="iframe" src="'+address+'" width="'+(getWindowWidth()*.9)+'" height="'+(getWindowHeight()*.9)+'" ></iframe><br /><a href="'+address+'">'+address+'</a>';
	iframe('pictureContent', value);
}

function iframe(targetID, value) {
	target = document.getElementById(targetID);
	target.innerHTML = value;
}

function viewSite(address) {
    siteIframe(address.toString());
    fade('table1', 500, 0, 0, 100);
    fade('dim',500,0, 0, 80);
}

function moveUp(targetID) {
    var st = document.getElementById(targetID).style;
    st.paddingTop = "2in";
    for(var i=0;i<101;i++) {
        setTimeout('setPaddingTop("'+targetID+'", "'+(100-i)*1+'px")', 20*i);
    }
}

function setPaddingTop(targetID, value) {
    var st = document.getElementById(targetID).style;
    st.paddingTop = value;
}

function postComment(postId) {
    var objMessageBox = document.getElementById('replyBox');
    var savedName = getCookie('jmh.journal.name');
    var nameByIP = getCookie('jmh.journal.nameByIP')
    if(!savedName && !nameByIP) savedName = 'Your Name...';
    if(!savedName && nameByIP) savedName = nameByIP;
    var savedLink = getCookie('jmh.journal.link');
    if(!savedLink) savedLink = 'http://www.yourwebsite.com';
    objMessageBox.innerHTML = '<a onClick="fade(\'dim\',500,0,75,0);fade(\'replyBox\', 500, 0, 100, 0);"><div class="closeComment iconDelete"></div></a><a onClick="errorWindow(\'Comment Formatting\', \'Use standard HTML formatting for tags, newlines will automatically break. Tags &amp;lt;a&amp;gt;, &amp;lt;b&amp;gt;, &amp;lt;i&amp;gt;, and &amp;lt;code&amp;gt; are functional.\');" class="floatRight" style="font-weight:bold;margin-top:10px;" title="help">?</a><h2 class="floatLeft">Post Comment...</h2>'
                             +'<form method="post" action="?p=journal&comment=true&post='+postId+'&tag=&epp=all&jp=0&search=#post'+postId+'" id="commentForm">'
                             +'<textarea id="commentTextArea" class="textArea commentTextArea" name="postcontent" rows="10" cols="20" onFocus="if(this.value==\'Your Comment...\') this.value=\'\';" onBlur="if(this.value==\'\') this.value=\'Your Comment...\';" >'
                                +'Your Comment...'
                             +'</textarea><br /><br />'
                             +'<input id="commentName" name="commentName" class="textBox" type="text" onFocus="if(this.value==\'Your Name...\') this.value=\'\';" onBlur="if(this.value==\'\') this.value=\'Your Name...\';" value="'+savedName+'" /><br /><br />'
                             +'<input id="commentLink" name="commentLink" class="textBox" type="text" value="'+savedLink+'" onFocus="if(this.value==\'http://www.yourwebsite.com\')this.value=\'\';" onBlur="if(this.value==\'\') this.value=\''+savedLink+'\';"  /><br /><br /><input class="floatRight postButton" type="button" onClick="validateComment();" value="Create Comment" />'
                             +'</form>';
    fade("dim", 200, 0, 0, 70);
    fade("replyBox", 200, 0, 0, 100);
    setTimeout('focus(\'commentTextArea\')',200);
}

function viewMedia(title, file, image) {
        if(image=="") image= 'player/default.jpg';
    	var mediaPlayer = '<object id="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" name="player" width="400" height="315">'+
		'<param name="movie" value="player/player.swf" />'+
		'<param name="allowfullscreen" value="true" />'+
		'<param name="allowscriptaccess" value="always" />'+
		'<param name="flashvars" value="file='+file+'&image='+image+'" />'+
		'<embed '+
			'type="application/x-shockwave-flash" '+
			'id="player2" '+
			'name="player2" '+
			'src="player/player.swf" '+
			'width="400" '+
			'height="315" '+
			'allowscriptaccess="always" '+
			'allowfullscreen="true" '+
			'flashvars="file='+file+'&image='+image+'"' +
		'/>'+
	'</object>'+'<br /><br /><a href="'+file+'">Download File</a>';
        var objTable1 = document.getElementById('table1');
    objPictureContent = document.getElementById('pictureContent');
    objPictureContent.innerHTML = '<div class="pictureFrame"><h2>'+title+'</h2>'+mediaPlayer+'<br /></div>';
    objTable1.style.display="table";
    objTable1.style.width = getWindowWidth();
    objPictureContent.onclick = function() {fade('dim',500,0,75,0);fade('table1',500,0,100,0);};
    fade("dim", 500, 0, 0, 75);
    fade("table1", 500, 0, 0, 100);

}

function validateComment() {
    var textArea = document.getElementById("commentTextArea");
    var name = document.getElementById("commentName");
    setCookie('jmh.journal.name',name.value,365);
    var link = document.getElementById("commentLink");
    setCookie('jmh.journal.link', link.value,365);
    var form = document.getElementById("commentForm");
    if((name.value!="Your Name...") && (textArea.value!="Your Comment...")) {
		if(link.value == "http://www.yourwebsite.com") link.value = "?p=journal&search="+name.value;
		form.submit();
    } else {
        errorWindow('Error', 'Please fill out all aspects of the form.');
    }
}

function errorWindow(errorTitle, errorText) {
        var objMessageBox = document.getElementById('messageBox');
        objMessageBox.innerHTML = '<h2>'+errorTitle+'</h2>'+errorText+'<br /><br /><input class="postButton floatRight" type="button" value="Okay" onClick="fade(\'dimError\',500,0,70,0);fade(\'messageBox\',500,0,100,0);" />';
        fade("dimError", 500, 0, 0, 70);
        fade("messageBox", 500, 0, 0, 100);
}

function messageBox(title, text) {
    var objMessageBox = document.getElementById('messageBox');
    objMessageBox.innerHTML = '<h2>' + title + '</h2>' + text + '<br /><input id="messageOkay" class="postButton floatRight" type="button" value="Okay" onClick="fade(\'dim\',500,0,70,0);fade(\'messageBox\',500,0,100,0);" />';
    fade("dim", 200, 0, 0, 70);
    fade("messageBox", 200, 0, 0, 100);
}

function chatBox(ip) {
    var objMessageBox = document.getElementById('chatBox');
    var chatHtml = '<a onClick="hideChatBox();"><div class="closeComment iconDelete"></div></a><a onClick="errorWindow(\'Chat Formatting\', \'Use standard HTML formatting for tags. Tags &amp;lt;b&amp;gt; and &amp;lt;i&amp;gt; are functional. Use /nick [Your Name] to change your name in chat.\');" class="floatRight" style="font-weight:bold;margin-top:10px;" title="help">?</a>'+
                   '<h2 class="floatLeft">jeffmhopkins.com Online Chat</h2><br /><br /><br />'+
                   '<form onsubmit="return false;"><div id="chattextarea" class="textArea" style="height:auto;width:auto;padding:5px;">Loading data...</div><br />'+
                   '<input id="chatname" type="text" class="textBox" value="'+ip+'" style="display:none;\" /><br /><input id="chattextmessage" type="text" class="textBox" /><br /><br /><input class="postButton floatRight" type="submit" onclick="chatSendMessage();" onsubmit="chatSendMessage();" value="Chat"/>'+
                   '<br /><br /></form>';
    objMessageBox.innerHTML = chatHtml;
    chatFirstLoad();
    chatShowMessages();
    setTimeout("document.getElementById('chattextmessage').focus();document.getElementById('chattextmessage').select();", 500);
    fade("dim", 200, 0, 0, 70);
    fade("chatBox", 200, 0, 0, 100);
}

function hideChatBox() {
    fade('dim',500,0,75,0);fade('chatBox', 500, 0, 100, 0);
    setTimeout('eraseDiv(\'chatBox\')', 500);
}

function yesNoBox(title, text, link) {
    var objMessageBox = document.getElementById('messageBox');
    objMessageBox.innerHTML = '<h2>' + title + '</h2>' + text + '<br /><br /> <input class="postButton floatRight" type="button" value="No" onClick="fade(\'dim\',500,0,70,0);fade(\'messageBox\',500,0,100,0);" /> <input class="postButton" type="button" value="Yes" onClick="window.location=\'' + link + '\'" />';
    fade("dim", 200, 0, 0, 70);
    fade("messageBox", 200, 0, 0, 100);
}

function inputBox(title, text, buttonTextYes, buttonTextNo, defaultValue, link) {
    var objMessageBox = document.getElementById('messageBox');
    objMessageBox.innerHTML = '<h2>' + title + '</h2>' + text + '<br /><br />' +
                              '<form method="GET" onSubmit="javascript:window.location=(\'' + link + '\' + document.getElementById(\'inputBoxTextBox\').value);return false;">'+
                              '<input id="inputBoxTextBox" type="text" class="textBox" value="'+defaultValue+'" /><br /><br />'+
                              '<input class="postButton floatRight" type="button" value="'+buttonTextNo+'" onClick="fade(\'dim\',500,0,70,0);fade(\'messageBox\',500,0,100,0);" /> <input class="postButton" type="button" value="'+buttonTextYes+'" onClick="window.location=(\'' + link + '\' + document.getElementById(\'inputBoxTextBox\').value);" />'+
                              '<input type="submit" class="hidden"></form>';
    fade("dim", 200, 0, 0, 70);
    fade("messageBox", 200, 0, 0, 100);
    setTimeout('focus("inputBoxTextBox")',100);
}

function focus(id) {
    var a = document.getElementById(id);
    a.focus();
    a.select();
}

function pictureViewer(address) {
    var objTable1 = document.getElementById('table1');
    objPictureContent = document.getElementById('pictureContent');
    objPictureContent.innerHTML = '<div class="pictureFrame"><h2>'+address+'</h2><image id="pictureImage" src="'+address+'" /></div><br /></div>';//src="./'+address+'
    objImage = document.getElementById('pictureImage');
    objTable1.style.display="table";
    objTable1.style.width = getWindowWidth();
    var objTheImage = document.getElementById('pictureImage');
    objTheImage.style.maxWidth = (getWindowWidth() * 95 / 100 ) + 'px';
    objTheImage.style.maxHeight = (getWindowHeight() * 90 / 100 ) + 'px';
    objPictureContent.onclick = function() {fade('dim',500,0,75,0);fade('table1',500,0,100,0);};
    fade("dim", 500, 0, 0, 75);
    fade("table1", 500, 0, 0, 100);
}

function getWindowHeight() {
    var winH=460;
    if(document.body && document.body.offsetWidth) {
        winH = document.body.offsetHeight;
    }
    if(document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth) {
        winH = document.documentElement.offsetHeight;
    }
    if(window.innerWidth && window.innerHeight) {
        winH = window.innerHeight;
    }
    return winH;
}

function getWindowWidth() {
    var winW = 630;
    if(document.body && document.body.offsetWidth) {
        winW = document.body.offsetWidth;
    }
    if(document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth) {
        winW = document.documentElement.offsetWidth;
    }
    if(window.innerWidth && window.innerHeight) {
        winW = window.innerWidth;
    }
    return winW;
}

var testObj;
function fade(targetID, time, delay, startingOpacity, endingOpacity) {
    var frame = time/frameSize;
    var opacityPerFrame = (startingOpacity - endingOpacity) / frame;
    setOpacity(startingOpacity, targetID);
    for(var i = 0; i < frame + 1; i++)
        setTimeout('setOpacity('+(startingOpacity - (i*opacityPerFrame))+', "'+targetID+'")',frameSize*i+delay);
    return false;
}

function setOpacity(value, targetID) {
        var st = document.getElementById(targetID).style;
	st.opacity = value/100;
	st.filter = 'alpha(opacity=' + value + ')';
        if(value==0) {
             st.display = "none";
             st.zIndex = "-10";
        } else {
            if(targetID.toString()=="table1") {
                st.display = "table";
            } else {
                st.display = "block";
            }
            st.zIndex = "500";
        }
}

function keyHandler(evt) {

if(evt) {
    if(evt.which==17) isCtrl = false;
    if(isCtrl) return;
    var element;
    if(evt.target) element=evt.target;
    else if(evt.srcElement) element = evt.srcElement;
    if(element.nodeType==3) element=element.parentNode;
    if(element.tagName=='INPUT' || element.tagName=='TEXTAREA') return;
    //alert(evt.keyCode);
    switch(evt.keyCode) {
        case 39: //right
            if(prev != "none") {
                window.location = prev;
            }
            break;
        case 37://left
            if(next != "none") {
                window.location = next;
            }
            break;
        case 115:chatBox('Anonymous');break;    //F4
        case 220:inputBox('Search Journal', 'Enter in your search argument below.', 'Search','Cancel','','?p=journal&search=');break; //\
        case 192:window.location = "?p=login";break;   //Tilde
        case 116:window.location = "?p=clock";break;   //F5

    }
}
}
var isCtrl = false;
//IE Errors on this...
document.onkeydown=function(e){if(e.which==17) isCtrl=true;}

var next = "none";
var prev = "none";
document.onkeyup = keyHandler;

var H='....';
var H=H.split('');
var M='.....';
var M=M.split('');
var S='......';
var S=S.split('');
var Ypos=0;
var Xpos=0;
var Ybase=8;
var Xbase=8;
var dots=12;
function clock(){
    var time=new Date ();
    var secs=time.getSeconds();
    var sec=-1.57 + Math.PI * secs/30;
    var mins=time.getMinutes();
    var min=-1.57 + Math.PI * mins/30;
    var hr=time.getHours();
    var hrs=-1.57 + Math.PI * hr/6 + Math.PI*parseInt(time.getMinutes())/360;
    for (i=0; i < dots; ++i){
    document.getElementById("dig" + (i+1)).style.top=0-15+40*Math.sin(-0.49+dots+i/1.9).toString() + "px";
    document.getElementById("dig" + (i+1)).style.left=0-14+40*Math.cos(-0.49+dots+i/1.9).toString() + "px";
    }
    for (i=0; i < S.length; i++){
    document.getElementById("sec" + (i+1)).style.top =Ypos+i*Ybase*Math.sin(sec).toString() + "px";
    document.getElementById("sec" + (i+1)).style.left=Xpos+i*Xbase*Math.cos(sec).toString() + "px";
    }
    for (i=0; i < M.length; i++){
    document.getElementById("min" + (i+1)).style.top =Ypos+i*Ybase*Math.sin(min).toString() + "px";
    document.getElementById("min" + (i+1)).style.left=Xpos+i*Xbase*Math.cos(min).toString() + "px";
    }
    for (i=0; i < H.length; i++){
    document.getElementById("hour" + (i+1)).style.top =Ypos+i*Ybase*Math.sin(hrs).toString() + "px";
    document.getElementById("hour" + (i+1)).style.left=Xpos+i*Xbase*Math.cos(hrs).toString() + "px";
    }
    setTimeout('clock()',50);
}

function echoClockHtml() {
    document.write('<table><tbody><tr><td><div id="clock"><div id="dig1" class="dig">1</div><div id="dig2" class="dig">2</div><div id="dig3" class="dig">3</div><div id="dig4" class="dig">4</div><div id="dig5" class="dig">5</div><div id="dig6" class="dig">6</div><div id="dig7" class="dig">7</div><div id="dig8" class="dig">8</div><div id="dig9" class="dig">9</div><div id="dig10" class="dig">10</div><div id="dig11" class="dig">11</div><div id="dig12" class="dig">12</div><div id="hour1" class="hour"></div><div id="hour2" class="hour"></div><div id="hour3" class="hour"></div><div id="hour4" class="hour"></div><div id="min1" class="min"></div><div id="min2" class="min"></div><div id="min3" class="min"></div><div id="min4" class="min"></div><div id="min5" class="min"></div><div id="sec1" class="sec"></div><div id="sec2" class="sec"></div><div id="sec3" class="sec"></div><div id="sec4" class="sec"></div><div id="sec5" class="sec"></div><div id="sec6" class="sec"></div></div></td></tr></tbody></table><br />');
    clock();
}

function browser() {
    var ua, s, i;
    this.isIE=false;
    this.isNS=false;
    this.version=null;
    ua=navigator.userAgent;
    s="MSIE";
    if((i=ua.indexOf(s)) >=0) {
        this.isIE = true;
        this.version = parseFloat(ua.substr(i+s.length));
        return;
    }
    s="Netscape6/";
    if((i=ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = parseFloat(ua.substr(i+s.length));
        return;
    }
    s="Gecko";
    if((i=ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = parseFloat(ua.substr(i+s.length));
        return;
    }
}

function setCookie(c_name,value,exdays){
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name){
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++) {
        x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x = x.replace(/^\s+|\s+$/g,"");
        if (x==c_name) {
            return unescape(y);
        }
    }
    return false;
}

function chatShowMessages() {
                if (window.XMLHttpRequest) {xmlhttp=new XMLHttpRequest();} else {xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
                xmlhttp.onreadystatechange=function() {
                        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                                document.getElementById("chattextarea").innerHTML = xmlhttp.responseText;
                        }
                }
                xmlhttp.open("GET","index.php?p=chat&r=l",true);
                xmlhttp.send();
        setTimeout('chatShowMessages()', 3000);
}

function chatSendMessage() {
                if(document.getElementById("chattextmessage").value.lastIndexOf("/nick") != -1) {
                        var newNick = document.getElementById("chattextmessage").value;
                        newNick = newNick.substr(6);
                        setCookie('jmh.journal.name',newNick,365);
                        var oldNick = document.getElementById("chatname").value;
                        document.getElementById("chatname").value=newNick;
                        document.getElementById("chattextmessage").value = "";
                        try{
                        document.getElementById("chattextmessage").select();
                        document.getElementById("chattextmessage").focus();} catch(e) {}
                        if (window.XMLHttpRequest) {xmlhttp=new XMLHttpRequest();} else {xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
                        xmlhttp.open("GET","index.php?p=chat&r=p&n="+escape(document.getElementById("chatname").value)+"&m=I just changed my name from "+escape(oldNick),true);
                        xmlhttp.send();
                        return false;
                }
                if (window.XMLHttpRequest) {xmlhttp=new XMLHttpRequest();} else {xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
                var v = document.getElementById("chattextmessage").value;
                xmlhttp.open("GET",("index.php?p=chat&r=p&n="+escape(document.getElementById("chatname").value)+"&m="+escape(document.getElementById("chattextmessage").value)),true);
                xmlhttp.send();
                document.getElementById("chattextmessage").value = "";
                try{
                document.getElementById("chattextmessage").select();
                document.getElementById("chattextmessage").focus();} catch(e) {}
                return false;
}
function chatFirstLoad() {
         var nickName = document.getElementById("chatname").value;
         var savedName = getCookie('jmh.journal.name');
         var nameByPSID = getCookie('jmh.journal.nameByPSID');
         if(!savedName && !nameByPSID) {
                nickName = nickName;
         } else if(!savedName && nameByPSID) {
                nickName = nameByPSID;
         } else if(savedName) {
                nickName = savedName;
         }
         document.getElementById("chatname").value = nickName;
}
