﻿    Event.observe(window, 'load', function() {
	    // Setup an observer to watch for click in page
	    Event.observe(document.body, 'click', handleClick);
    });

    function handleClick(event) {
	    var elt = Event.element(event); // Get the clicked element
	    var id_value = elt.readAttribute('id');
	    var id_array = '';
    	
	    // Check to see if click element is a context info link elt.getAttribute('class') is only for IE8
	    if (elt.readAttribute('class') == 'ShowCommentBox' || elt.getAttribute('class') == 'ShowCommentBox') { 
	        if (elt.innerHTML == 'Send Blog Comment'){
	            elt.innerHTML = 'Cancel Blog Comment';
	            showIframe(elt, true);
	        }
	        else{
	            elt.innerHTML = 'Send Blog Comment';
	            showIframe(elt, false);
	        }    
		    
	    }
	    else if (elt.readAttribute('class') == 'ShowFullEntry' ||  elt.getAttribute('class') == 'ShowFullEntry'){
	        id_array = id_value.split('_');
	        $(elt).update('<img src="/library/images/loading.gif" alt="Loading" border="0">Loading...');
	        retVal = NJHA.NJHAJax.OneBlogEntry(id_array[1], id_array[2], OnComplete, OnTimeout, OnError);
	    }
	    else if (elt.readAttribute('class') == 'ShowFullComment' || elt.getAttribute('class') == 'ShowFullComment'){
	        id_array = id_value.split('_');
	        $(elt).update('<img src="/library/images/loading.gif" alt="Loading" border="0">Loading...');
	        retVal = NJHA.NJHAJax.OneBlogComment(id_array[1], id_array[2], OnComplete, OnTimeout, OnError);
	    }
    }
    function showIframe(elt, showIt){
            var name_value = elt.readAttribute('name'); // name_value contains something like id_1_2_3
    		
		    // Break string into an array
		    var name_array = name_value.split('_'); 
		    var param = name_array[1] + '_' + name_array[2] + '_' + name_array[3] + '_' + name_array[4];
		    //var url = '/blog/comment.aspx?b=' + name_array[1] + '&e=' + name_array[2] + '&t=' + name_array[3] + '&c=' + name_array[4]; 
		    var divContainer = 'form_' + param;
    		
		    //iframe string -- ie 6 doesn't support dynamic iframe
		    var frameId = 'iFrame_' + param;
		    //var iframe = "<iframe class='comment' src='" + url + "' name='" + frameId + "' id='" + frameId + "' frameborder='0' style='width:560px;'></iframe>"; 
    		
    		//show the iframe inside the div container
		    //$(divContainer).update(iframe);
		    
		    //show/hide the container which has the iframe inside
		    if (showIt){
		        $(divContainer).show();
		        setIframeHeight(frameId);
		    }
		    else{
		        $(divContainer).hide();  
		    }
    }

 
    function getDocHeight(doc) {
        var doc_height, sh, oh, ch;
        if (doc.height) doc_height = doc.height;
        else if (doc.body){
	        if (doc.body.scrollHeight) doc_height = sh = doc.body.scrollHeight;
	        if (doc.body.offsetHeight) doc_height = oh = doc.body.offsetHeight;
			if (doc.body.clientHeight) doc_height = ch = doc.body.clientHeight;
	        if (sh && oh && ch) doc_height = Math.max(sh, oh, ch);
        }
        //var doc_height = doc.height ? doc.height : 0; // Safari uses document.height 
		//alert(doc.body.scrollTop);
        if (doc.documentElement && doc.documentElement.scrollHeight) /* Strict mode */ 
            return Math.max (doc.documentElement.scrollHeight, doc.documentElement.clientHeight, doc_height); 
        else
            return doc_height; //Math.max (doc.body.scrollHeight, doc_height); 
    }

    function setIframeHeight(iframeName) {
      var iframeWin = window.frames[iframeName];
      var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;
      if ( iframeEl && iframeWin ) {
        iframeEl.style.height = "auto";
        var doc = iframeWin.document ? iframeWin.document : iframeEl.contentDocument ? iframeEl.contentDocument : iframeEl.contentWindow;
        if (doc!=null){//add 65px here for the validation error to show without the scroll bar
            var docHt = getDocHeight(doc);
            if (docHt) iframeEl.style.height = (docHt + 70) + "px";
        }
        //alert (doc);
      }
    }

    function hideIFrame(id, showAlert)
    {
        $(id).hide();
        
        if (showAlert){
            $('divAlert').innerHTML = "Thank you for your comment!\nWe regret we can\'t respond to everyone\'s comments in a personalized manner, nor can we provide answers to specific inquiries in your comment.\nFor specific questions or information requests please visit <a href=\"http://www.njha.com/AboutNJHA/ContactUS.aspx\">http://www.njha.com/AboutNJHA/ContactUS.aspx</a>";
            $('divMessage').show();
            $('divFilter').show();
        }
        
        //swith the hyperlink text back to Send Blog Comment 
        var elmid = id.replace('form', 'comment'); 
        var curElement = $(elmid);
        curElement.innerHTML = 'Send Blog Comment';
    } 
    function OnComplete(resultObj){
        //alert (resultObj);
        var jsonObj = eval("(" + resultObj + ")");
        var id = jsonObj.id;
        $(id).update('');
        new Insertion.After(id, jsonObj.content);
        Element.hide(id);
    }
    function OnTimeout () {
        OnError();
    }

    function OnError() {
        $('errorMsg').innerHTML = "Please try again!";
        Element.show('errorMsg');
    }