/* 
* Enchilada Frame Work 1.0
* Display Hit O'meter via Javascript 
*
* Copyright (c) 2003-2007, Daniel Morante
* All rights reserved.
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
* 
* 	* Redistributions of source code must retain the above copyright notice, 
*	  this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above copyright notice, 
* 	  this list of conditions and the following disclaimer in the documentation 
* 	  and/or other materials provided with the distribution.
*   * Neither the name of The Daniel Morante Company, Inc. nor the names of its contributors 
* 	  may be used to endorse or promote products derived from this software without 
* 	  specific prior written permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

function loadCSS(fullPath) {
	//Build the Style link object
	var css = document.createElement("link");
	css.setAttribute("href", fullPath);
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	// attempt to add the css and then keep trying till we do
	try {
		document.getElementsByTagName("head")[0].appendChild(css);
	} catch (e) {
		setTimeout(function(){setCSS(css)}, 100);
	}
	css = null;
}


//Function to create a new DIV element
function makeDiv(newid, newclass, newparent) {
	if (newparent == null) {newparent = document.body;}

	var newdiv = document.createElement('div');
	newparent.appendChild(newdiv);
    newdiv.id = newid;
	newdiv.className = newclass;
	
	return newdiv;
}

function deleteDiv (olddiv) {
	olddiv.parentNode.removeChild(olddiv);
}

function makeHitometer (parentElement, tabulate) {	
	//Place the Hitometer Counter Image inside that DIV
	var hitometerImage = document.createElement("img");
	hitometerImage.id = "imageElement";
	
	//Determine weahter or not this is a refresh operation
	//A unique number must be passed each refresh so the browser doesn't use the chached version
	if (tabulate) {
		//Incriment
		hitometerImage.setAttribute("src", hitometerURI + "?id=" + pageometer_id + "&time=" + Date());
	}
	else {
		//Don't Increment
		hitometerImage.setAttribute("src", hitometerURI + "?id=" + pageometer_id + "&refresh=1&time=" + Date());
	}
	
	hitometerCounter.appendChild(hitometerImage);
	return hitometerImage;
}

function rebuildHitometer () {
	deleteDiv(document.getElementById("imageElement"));
	makeHitometer(document.getElementById("hitoImage"), false);	
}

function openHitometerInfo (){
	window.open(hitometerURI + '?click=1');	
}

//This is the address to load the Hitometer Image
var hitometerURI = "http://www.pageometer.com/pageometer/pageometer.php";

//Load the Needed CSS
loadCSS('http://www.pageometer.com/pageometer/pageometer.css');

//We need a starting Spot
document.write('<' + 'div id=\"hitometerHolder\" class=\"hitometerStyle\">' + '</div>');
hitometerSection = document.getElementById("hitometerHolder");

//Start by creating a holder element
hitometer = makeDiv("hitometer", "hitometerStyle", hitometerSection);

//Assign a click action
hitometer.addEventListener('click', openHitometerInfo, false);

//A DIV to hold it better
hitometerCounter = makeDiv("hitoImage", "hitometerImage", hitometer);

//Put the hitometer in this holder
hitometerHandle = makeHitometer (hitometerCounter, true);

//Place the Pacy World Text Add
advertisement = makeDiv("advertisement", "hitometerAd", hitometerSection);
//A DIV for the text
//adText = makeDiv("PacyWorldAd", "hitometerTinyText", advertisement);
//adText.innerHTML = "Provided by Pacy World";
//A DIV for the linkback
//adTextLink = makeDiv("PacyWorldlink", "hitometerTinyText", advertisement);
//adTextLink.innerHTML = "<" + "a href =\"http://www.pacyworld.com\">Web Hosting</a>";

//Refresh the Hitometer every 2 minutes
window.setInterval(rebuildHitometer, 1000 * 60 * 7);

