//
// When IE kicks into 'standards' mode (i.e. when you specify a DOCTYPE at
// the beginning of your HTML) it has a problem with user highlighting
// text for cut/paste.  This is because it does not calculate the document
// height correctly for some reason.  This bit of JavaScript forces the
// it to figure out the right value.  Bless whoever it was that figured
// this out.
//

if (window.createPopup && document.compatMode && document.compatMode=="CSS1Compat") {
	document.onreadystatechange = onresize = function fixIE6AbsPos() {
		if (!document.body) return;
		if (document.body.style.margin != "0px") document.body.style.margin = 0;
		onresize = null;
		document.body.style.height = 0;
		setTimeout(function(){ document.body.style.height =
			document.documentElement.scrollHeight+'px'; }, 1);
		setTimeout(function(){ onresize = fixIE6AbsPos; }, 100);
	}
}

function EnsureChange(form) {
    var dist = form.distance.value;
    var orig = form.orig_distance.value;

    if (dist == orig) {
        alert('You only need to click miles if you have changed the number');
        return false;
    }
    if (dist <= 0) {
        alert('Distance needs to be a positive number');
        return false;
    }
    return true;
}

function CreateBookmarkLink() {
    title = "<?=$HTML_TITLE?>"; 
    url = "http://<?=$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']?>";

    if (window.sidebar) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(title, url,"");
    } else if( window.external ) { // IE Favorite
        window.external.AddFavorite( url, title); }
    else if(window.opera && window.print) { // Opera Hotlist
        return true; }
}

function SetHomePage(a) {
    a.style.behavior='url(#default#homepage)';
    a.setHomePage('http://www.wherezit.com');
}

var timeOn = null;
var menuTab = null;

function menuItem(id, on_img, off_img, category_id) {
    this.name = id
    this.onImage = new Image();
    this.onImage.src = on_img;
    this.offImage = new Image();
    this.offImage.src = off_img;
    this.category_id = category_id;
}

/* MouseOver
 *
 * User has rolled over a top-level menu tab/image.  Change the
 * image from Off to On and display the dropdown menu for this
 * category.
 */
function mouseOver(tab) {
	clearTimeout(timeOn);
	menuTab = tab;
	timeOn = setTimeout("displayTabMenu(menuTab)", 300);  // time in ms
}

/*
 * displayTabMenu
 */
function displayTabMenu(tab) {
    hideAllMenus();
	var img = document.getElementById(tab.name);
 	img.src = tab.onImage.src;
	if (tab.category_id) {
        // See if there is a submenu for this category
        if (dropDownData[tab.category_id] != undefined) {
            // Figure out the desired X & Y coordinates
            // to display this dropdown menu
            var x = 0;
            var y = img.clientHeight;
            var tmp = img;
            while (tmp != null) {
                x += tmp.offsetLeft;
                y += tmp.offsetTop;
                tmp = tmp.offsetParent;
            }
            displayDropDown(0, tab.category_id, x, y);
        }
	}
}

/* displayDropDown
 *
 * Pull the subcategory information from the dropDownData array
 * and build a table that becomes the dropdown.  Insert that into
 * the specified div and display the div.
 */
function displayDropDown(divId, category_id, x, y) {
    // Build the table of links that link to the
    // subcategories of this category
    var nextDiv = divId + 1;
    var tmp = dropDownData[category_id];
    if (tmp.length == 0) {
        return;
    }
    var table = '<table cellspacing=0 class=menu>';
    for (i=0; i<tmp.length; i++) {
        var cid = tmp[i][0];
        var cname = tmp[i][1];
        var tmpDiv = '';
        if (dropDownData[cid] != undefined) {
            tmpDiv = '<div style="float: right;">&nbsp;<b>&gt;</b></div>';
        }
        table += '<tr><td onmouseover="mouseOverSub(this,'+cid+','+nextDiv+');" ';
        table += 'onmouseout="mouseOutSub(this);" onclick="loadCategory('+cid+');">';
        table += tmpDiv+'<a href="/list_category.php?catid='+cid+'">'+cname+'</a></td>';
        table += '</tr>';
    }
    table += '</table>';

    // Display the div
    var div = document.getElementById('dropDown'+divId);
    div.style.display='block';
    div.style.left = x+'px';
    div.style.top = y+'px';
    div.innerHTML = table;
}

function mouseOut(tab) {
	clearTimeout(timeOn);
	timeOn = setTimeout("hideAllMenus()", 500); // time in ms
}

function hideAllMenus() {
    // Turn all the tab images off
    for (i=0; i<AllMenus.length; i++) {
        var tab = AllMenus[i];
		document.getElementById(tab.name).src = tab.offImage.src;
    }

    // Hide all the dropdown divs
    for (i=0; i<10; i++) {
        var div = document.getElementById('dropDown'+i);
        div.style.display='none';
        div.innerHtml = '';
    }
}

function mouseOverSub(td, category_id, divId) {
	clearTimeout(timeOn);
    td.style.background = '#e6deea';

    // Hide all the subcategorydropdown divs
    for (i=divId+1; i<10; i++) {
        var div = document.getElementById('dropDown'+i);
        div.style.display='none';
        div.innerHtml = '';
    }

    if (dropDownData[category_id] != undefined) {
		// This category has subcategories  
		// Figure out the x & x position of the top-right corner
		// of this td.
		var x = td.clientWidth;
		var y = 0;
  		var tmp = td;
		while (tmp != null) {
	  		x += tmp.offsetLeft;
			y += tmp.offsetTop;
			tmp = tmp.offsetParent;
		}
        displayDropDown(divId, category_id, x, y);
    } else {
        var div = document.getElementById('dropDown'+divId);
        div.style.display='none';
    }
}

function mouseOutSub(td) {
    td.style.background = '#ffffff';
    mouseOut();
}

function mouseOverSub2(td) {
	clearTimeout(timeOn);
    td.style.background = '#e6deea';
}

function mouseOutSub2(td) {
    td.style.background = '#ffffff';
    mouseOut();
}

function loadCategory(cid) {
    hideAllMenus();
	document.location.href = '/list_category.php?catid='+cid;
}

