// main.js
//
var pageRoot = "http://www.bettingforbenson.com/";
//var pageRoot = "http://localhost/benson/";

if(window.parent.location.href.indexOf("wrapper.php") == -1) {
	var page = escape(window.location.pathname + window.location.search);
	window.parent.location = pageRoot + "wrapper.php?page=" + page;
}

var bannerX = 0;
function scrollBanner() {
	// Scroll the top banner
	var banner = document.getElementById("banner");
	if(banner) {
		bannerX -= 1;
		banner.style.backgroundPosition = bannerX + "px";
	}

	setTimeout("scrollBanner()", 50);
}
scrollBanner();

function topCorners(img) {
	// Write the top left and right corners
	corners(img, "TL,TR");
}

function bottomCorners(img) {
	// Write the bottom left and right corners
	corners(img, "BL,BR");
}

function corners(img, corners) {
	// Create rounded corners as specified
	return;
	var border = parseInt(img.substring(0,3));
	img = pageRoot + "img/corners/" + img.replace(' ', '_');
	var width = 20;
	var height= 20;
	
	// IE is weird with the positioning sometimes...
	var ie = (navigator.appVersion.indexOf('MSIE') > -1);
	if(!border || isNaN(border)) border = 1;
	var fudgel = 3;
	var fudger = border * 3 + (border - 1);

	// Position each corner
	var r = "";
	if(corners.indexOf("TL") > -1) {
		var style = "float: left; ";
		if(ie) style += "position: relative; left: -" + fudgel + "px; ";
  	r += '<img src="' + img + '_tl.gif" height="' + height + '" width="'+ width + '" style="' + style + '" />';
	}
	if(corners.indexOf("TR") > -1) {
		var style = "float: right; ";
		if(ie) style += "position: relative; left: " + fudger + "px; ";
  	r += '<img src="' + img + '_tr.gif" height="' + height + '" width="'+ width + '" style="' + style + '" />';
	}	
	if(corners.indexOf("BL") > -1) {
		var style = "float: left; position: relative; top: -" + height + "px; ";
		if(ie) style += "left: -" + fudgel + "px; ";
		r += '<img src="' + img + '_bl.gif" height="' + height + '" width="'+ width + '" style="' + style + '" />';
	}
	if(corners.indexOf("BR") > -1) {
		var style = "float: right; position: relative; top: -" + height + "px; ";
		if(ie) style += "left: " + fudger + "px; ";
		r += '<img src="' + img + '_br.gif" height="' + height + '" width="'+ width + '" style="' + style + '" />';
	}
	
	document.write(r);
}

/*
 * CSS/JavaScript font detector
 *  by Lalit Patel
 *  http://www.lalit.org/lab/fontdetect.php
 */
var Detector = function() {
    var h = document.getElementsByTagName("BODY")[0];
    var d = document.createElement("DIV");
    var s = document.createElement("SPAN");
    
    d.appendChild(s);
    d.style.fontFamily = "sans-serif";		//font for the parent element DIV.
    s.style.fontFamily = "sans-serif";		//arial font used as a comparator.
    s.style.fontSize   = "72px";			//we test using 72px font size, we may use any size. I guess larger the better.
    s.innerHTML        = "mmmmmmmmmml";		//we use m or w because these two characters take up the maximum width. And we use a L so that the same matching fonts can get separated
    h.appendChild(d);
    var defaultWidth   = s.offsetWidth;		//now we have the defaultWidth
    var defaultHeight  = s.offsetHeight;	//and the defaultHeight, we compare other fonts with these.
    h.removeChild(d);
    
    /* test
    * params:
    * font - name of the font you wish to detect
    * return: 
    * f[0] - Input font name.
    * f[1] - Computed width.
    * f[2] - Computed height.
    * f[3] - Detected? (true/false).
    */
    function test(font) {
        h.appendChild(d);
        var f = [];
        f[0] = s.style.fontFamily = font;	// Name of the font
        f[1] = s.offsetWidth;				// Width
        f[2] = s.offsetHeight;				// Height
        h.removeChild(d);
        
        font = font.toLowerCase();
        if (font == "arial" || font == "sans-serif") {
        	f[3] = true;	// to set arial and sans-serif true
    	} else {
    		f[3] = (f[1] != defaultWidth || f[2] != defaultHeight);	// Detected?
   		}
    	return f;
    }
    this.test = test;
}



/*
 * Dynamic CSS creator
 *  Unobtrusive Flash Objects (UFO) v3.20 <http://www.bobbyvandersluis.com/ufo/>
 *  Copyright 2005, 2006 Bobby van der Sluis
 *  This software is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
 */
function createCSS(selector, declaration) {
    // test for IE
    var ua = navigator.userAgent.toLowerCase();
    var isIE = (/msie/.test(ua)) && !(/opera/.test(ua)) && (/win/.test(ua));
    
    // create the style node for all browsers
    var style_node = document.createElement("style");
    style_node.setAttribute("type", "text/css");
    style_node.setAttribute("media", "screen");
    
    // append a rule for good browsers
    if (!isIE) style_node.appendChild(document.createTextNode(selector + " {" + declaration + "}"));
    
    // append the style node
    document.getElementsByTagName("head")[0].appendChild(style_node);
    
    // use alternative methods for IE
    if (isIE && document.styleSheets && document.styleSheets.length > 0) {
        var last_style_node = document.styleSheets[document.styleSheets.length - 1];
        if (typeof(last_style_node.addRule) == "object") last_style_node.addRule(selector, declaration);
    }
}


window.onload = function init() {
	// Check for a cursive font
	var d = new Detector();
	var cursiveFonts = new Array("Rage Italic LET", "Rage Italic", "Brush-Script");
	
	var r, found=0;
	for(var i=0; i<cursiveFonts.length; i++) {
		if( (d.test(cursiveFonts[i]))[3] ) {
			found = 1;
		}
	}
	
	// If no cursive font was found, switch to something else
	if(!found) {
		createCSS("h1, h2, div#menu a", "font-family: Comic Sans MS, cursive; font-size: 16pt;");
	}
}

function guestbookSubmit() {
	var name = document.getElementById("postName").value;
	var email = document.getElementById("postEmail").value;
	var body = document.getElementById("postBody").value;
	var loc = "../admin/post.php?formType=guest&postName=" + name + "&postEmail=" + email + "&postBody=" + body;
	window.location = loc;
	return false;
}
