/* #################################################################
    Copyright 2005 BBB Systems, LLC, All rights reserved
################################################################## */

function init( ){
    initTopMenuLinks( );
    initInputButtons( );
    initA( );
    initBlocks( );
    initObjects( );
}

function printAndClose( ){
    window.print( );
    window.close( );
}

function initInputButtons( ){
    if(document.getElementsByTagName){
        var inputs = document.getElementsByTagName("input");
        for(var i = 0; i < inputs.length; i++){
            if((inputs[i].type == "submit" || inputs[i].type == "reset" || inputs[i].type == "button") && inputs[i].className == "button"){
                inputs[i].onmouseover = inputOver;
                inputs[i].onmouseout = inputOut;
            }
        }
        var buttons = document.getElementsByTagName("button");
        for(var i = 0; i < buttons.length; i++){
            if((buttons[i].type == "submit" || buttons[i].type == "reset" || buttons[i].type == "button") && buttons[i].className == "button"){
                buttons[i].onmouseover = inputOver;
                buttons[i].onmouseout = inputOut;
            }
        }
    }
}

function writeFlashHeader( ){
    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="780" height="225" style="position: relative; top: -1px;"><param name="movie" value="/flash/tnb_movie.swf"><param name="quality" value="high"><embed src="/flash/tnb_movie.swf" width="780" height="225" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed></object>');
}

function initBlocks( ){
    if(document.getElementsByTagName){
        var imgs = document.getElementsByTagName("img");
        var bcnt = 0;
        for(var i = 0; i < imgs.length; i++){
            var img = imgs[i];
            if(img.src.match(/\/minus\.gif/) || img.src.match(/\/plus.gif/) || img.src.match(/\/blank.gif/)){
                //get the parent
                var iparent = img.parentNode;
                while(iparent){
                    if(iparent.className == 'blockH'){
                        break;
                    }
                    iparent = iparent.parentNode;
                }
                if(!iparent){ continue };
                var iblock = iparent.nextSibling;
                while(iblock){
                    if(iblock.tagName == 'div' || iblock.tagName == 'DIV'){
                        break;
                    }
                    iblock = iblock.nextSibling;
                }

                if(!iblock){ continue; }
                if(iblock.className != 'block1'){ continue; }

                var atags = iblock.getElementsByTagName('a');
                var doBlock = false;
                for(var j = 0; j < atags.length; j++){
                    var a = atags[j];
                    if(window.location == a.href){
                        doBlock = true;
                    }
                }

                if(iblock.style.display == "none"){
                    img.src = plusImg.src;
                }
                else{
                    img.src = minusImg.src;
                }

                iparent.onclick = function( ){
                    var iblock = this.nextSibling;
                    while(iblock.tagName != 'div' && iblock.tagName != 'DIV'){
                        iblock = iblock.nextSibling;
                    }

                    var img = this.getElementsByTagName('img')[0];
                    //alert(imgStr);
                    if(img){
                        var imgStr = img.src.toString( );
                        if(iblock.style.display == "none" || iblock.style.display == "NONE"){
                            iblock.style.display = 'block';
                            img.src = img.src = minusImg.src;
                        }
                        else{
                            iblock.style.display = 'none';
                            img.src = img.src = plusImg.src;
                        }
                    }
                }

                iparent.onmouseover = function( ){
                    this.style.textDecoration = 'underline';
                }

                iparent.onmouseout = function( ){
                    this.style.textDecoration = 'none';
                }

                if(document.all){
                    //the ie way
                    iparent.style.cursor = 'hand';
                }
                else{
                    //every other way
                    iparent.style.cursor = 'pointer';
                }
            }
        }
    }
    else{

    }
}


function initA( ){
    if(document.getElementsByTagName){
        var as = document.getElementsByTagName("a");
        for(var i = 0; i < as.length; i++){
            var hostnm = as[i].href.replace(/http(s)?\:\/\//, "");
            hostnm = hostnm.replace(/\/.*/, "");

            //delete me from the live site (DON'T FORGET)
            if(as[i].href.match(/66\.213\.226\.42/) && !as[i].href.match(/\/ad\.\php\?/)){
                continue;
            }

            if(hostnm != window.location.hostname || as[i].href.match(/\/ad\.\php\?/)){
                as[i].target = "_blank";
            }
        }
    }
}

function initObjects( ){
    if(document.getElementsByTagName){
        var objects = document.getElementsByTagName("div");
        var len = objects.length;

        for(var i = 0; i < len; i++){
            var obj = objects[i];
            if(obj.className && obj.className == 'bgzobject'){
                var paramStr = '';
                var objStr = '\n<object ';
                var embedStr = '\n<embed ';
                var params = obj.getElementsByTagName('param');
                var plen = params.length;
                var o_type = '';
                var o_filename = '';
                var o_width = '';
                var o_height = '';
                //need o_type, it will be first or second in the list, but just in case.
                for(j = 0; j < plen; j++){
                    var param = params[j];
                    var pn = param.name;
                    var pv = param.value;
                    if(pn == 'o_type'){
                        o_type = pv;

                        //do this here because we have to force video type for wmv files
                        var isWindows = false;
                        //if( o_type != 'wmv' && !document.all){
                        //     isWindows = false;
                        //}else if(o_type == 'wmv' || navigator.userAgent.match(/windows/i)){
                        if(o_type == 'wmv' || navigator.userAgent.match(/windows/i)){
                            isWindows = true;
                        }

                        break;
                    }
                }
                for(j = 0; j < plen; j++){
                    var param = params[j];
                    var pn = param.name;
                    var pv = param.value;

                    switch(pn){
                    case 'o_type':
                        if(o_type == 'swf'){
                            embedStr += ' type="application/x-shockwave-flash" ';
                            objStr += ' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
                            objStr += ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" ';
                        }
                        else if(isWindows && o_type != 'mov'){

                            embedStr += ' type="application/x-mplayer2" ';
                            embedStr += '  pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/" ';
                            objStr += ' classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" ';
                            objStr += ' standby="Loading..." ';
                            objStr += ' type="application/x-oleobject" ';
                            objStr += ' codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715" ';
                        }
                        else{
                            embedStr += ' pluginspage="http://www.apple.com/quicktime/download/" ';
                            objStr += ' classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" ';
                            objStr += ' codebase="http://www.apple.com/qtactivex/qtplugin.cab" ';
                        }
                        break;
                    case 'o_filename':
                        o_filename = pv;
                        if(o_type == 'swf'){
                            paramStr += ' <param name="movie" value="docs/' + pv + '" /> \n';
                        }
                        else if(isWindows && o_type != 'mov'){
                            paramStr += ' <param name="filename" value="docs/' + pv + '" /> \n';
                        }
                        else{
                            paramStr += ' <param name="src" value="docs/' + pv + '" /> \n';
                        }
                        embedStr += ' src="docs/' + pv + '" ';

                        break;
                    case 'o_width':
                        o_width = pv;
                        obj.width = pv;
                        embedStr += ' width="' + pv + '" ';
                        objStr += ' width="' + pv + '" ';
                        break;
                    case 'o_height':
                        o_height = pv;
                        obj.height = pv;
                        embedStr += ' height="' + pv + '" ';
                        objStr += ' height="' + pv + '" ';
                        break;
                    default:
                        pn = pn.replace(/^(o\_)/, '');

                        if(!isWindows && pn == 'showcontrols'){
                            pn = 'controller';
                        }

                        if(pn == 'transparency'){
                            pn = 'wmode';
                            pv = 'transparent';
                        }

                        paramStr += ' <param name="' + pn + '" value="' + pv + '" /> ';

                        if(pv.toLowerCase( ) == 'false'){
                            pv = '0';
                        }
                        else if(pv.toLowerCase( ) == 'true'){
                            pv = '1';
                        }

                        if(pn == 'showcontrols'){
                            if(pv == '0'){
                                paramStr += ' <param name="ShowStatusBar" value="false" /> \n';
                                embedStr += ' showstatusbar="0" ';
                            }
                            else{
                                paramStr += ' <param name="ShowStatusBar" value="true" /> \n';
                                embedStr += ' showstatusbar="1" ';
                            }
                        }

                        embedStr += ' ' + pn + '="' + pv + '" ';


                        break;
                    }
                }//end params

                embedStr += '></embed>\n';
                objStr += '>\n';
                //alert(objStr + paramStr + embedStr + '</object>');
                obj.innerHTML = objStr + paramStr + embedStr + '</object>\n';
                //alert(obj.innerHTML);
                obj.style.display = 'block';
            }//end if bgzobject
        }
    }
}

function inputOver(e){
    this.className = "buttonOver";
}

function inputOut(e){
    this.className = "button";
}

function imageSelectPopUp(pwinvar){
    imgSelectPopUp = window.open("./imageselect.php?mode=1&fieldID=" + pwinvar, "ImageSelector", "width=780,height=480, scrollbars=0, status=no,toolbar=no,resizable=no");
    imgSelectPopUp.focus( );
}

function openUploader( ){
    var imgSelectPopUp = window.open("./imageselect.php", "ImageSelector", "width=780,height=480, scrollbars=0, status=no,toolbar=no,resizable=no");
    imgSelectPopUp.focus( );

    return false;
}

function fileSelectPopUp(pwinvar){
    imgSelectPopUp = window.open("./fileselect.php?mode=1&fieldID=" + pwinvar, "FileSelector", "width=780,height=480, scrollbars=0, status=no,toolbar=no,resizable=no");
    imgSelectPopUp.focus( );
}

function openFUploader( ){
    var imgSelectPopUp = window.open("./fileselect.php", "FileSelector", "width=780,height=480, scrollbars=0, status=no,toolbar=no,resizable=no");
    imgSelectPopUp.focus( );

    if(document.all){
        window.event.cancelBubble = true;
    }
    else{
        e.stopPropagation( );
        e.preventDefault( );
    }
    return false;
}

function generalPopUp(loc, w, h){
    gPopUp = window.open(loc, "gpopup", "width=" + w + ",height=" + 480 + ", scrollbars=0, status=no,toolbar=no,resizable=no");
    gPopUp.focus( );
}

function couponSelectPopUp( ){
    coupSelectPopUp = window.open("/coupons.php", "CouponeSelector", "width=670,height=420,scrollbars=yes") ;
    coupSelectPopUp.focus( );

}

function initTopMenuLinks( ){
    if(!document.getElementsByTagName){
        return;//bad beat, browser is ancient.
    }
    var tmenu = document.getElementById('tmenu');
    var liA = document.getElementsByTagName('li')
    var len = liA.length;

    for(var i = 0; i < len; i++){
        var li = liA[i];
        //if(li.childNodes.length > 1){
            li.onmouseover = pageON;
            li.onmouseout = pageOFF;
        //}
    }
}

//drop down menus ##################################################
var CurMenu = null;
var isDropped = false;
var isHideCheck = false;
var subCurMenu = null;
var subIsDropped = false;
var subIsHideCheck = false;
function pageON(index){
    var uls = this.getElementsByTagName('ul');
    if(uls.length > 0){
        show(uls[0]);
    }

}
function pageOFF(index){
    if(document.getElementById){
        if(!isHideCheck) hideMenu( );
    }
}
function show(menu){
    if(CurMenu)
        CurMenu.style.display = "none";
    menu.style.display = "block";
    isDropped = true;
    CurMenu = menu;
}
function hideMenu( ){
    if(CurMenu){
        isDropped = false;
        isHideCheck = true;
        setTimeout('checkDropped( );', 500);
        isHideCheck = false;
    }
}
function checkDropped( ){
    if(!isDropped) CurMenu.style.display = "none";
}
//############################################################

function printView(plink) {
    var plink2 = "/print/p/" + plink;
    window.open(plink2, "ShoppingList", "width=620,height=400,status=yes,toolbar=no,resizable=yes,raised=yes,scrollbars=yes");
}

function printPage(buttonID) {
    var printButton = document.getElementById(buttonID);
    printButton.style.visibility = "hidden";
    window.print();
    window.close();
}

var ipreviewWin = null;
function ipreview(lnk){
    ipreviewWin = window.open(lnk, "ipreviewWin", "width=780,height=480, menubar=yes, location=yes, scrollbars=yes, status=yes,toolbar=yes,resizable=yes");
    ipreviewWin.focus( );
    return false;
}

//category function
function lCategoryPopup(pwinvar){
    lCatPopUp = window.open("/include/category.php?lckey=" + pwinvar, "CategorySelect", "width=780,height=480, scrollbars=yes, status=yes,toolbar=no,resizable=yes");
    lCatPopUp.focus( );
}
function lCategoryPopupA(pwinvar){
    lCatPopUp = window.open("/include/category2.php?lckey=" + pwinvar, "CategorySelect", "width=780,height=480, scrollbars=yes, status=yes,toolbar=no,resizable=yes");
    lCatPopUp.focus( );
}
function lClassifiedPopup(pwinvar){
    lCatPopUp = window.open("/include/classified.php?lckey=" + pwinvar, "ClassifiedSelect", "width=780,height=480, scrollbars=yes, status=yes,toolbar=no,resizable=yes");
    lCatPopUp.focus( );
}
function wsbPopup(loc){
    lCatPopUp = window.open(loc, "wsb", "width=782,height=480, menubar=yes, location=yes, scrollbars=yes, status=yes,toolbar=yes,resizable=yes");
    lCatPopUp.focus( );
}

function whatisPopup(loc){
    whatisPopUp = window.open(loc, "whatis", "width=300,height=300, menubar=no, location=no, scrollbars=yes, status=no,toolbar=no,resizable=yes");
    whatisPopUp.focus( );
}
function printMenuImage(img, winx, winy){
    imageMenuPopup = window.open("/menu_images.php?img=" + img, "Menu", "width=" + winx + ",height=" + winy + ", menubar=no, location=no, scrollbars=no, status=no,toolbar=no,resizable=no");
    imageMenuPopup.focus( );
}

function printFoodMenu(catid){
    foodMenuPopup = window.open('/print.php?p=2&all=1&catid=' + catid, "foodMenu", "width=600,height=400, menubar=no, location=no, scrollbars=yes, status=no,toolbar=no,resizable=yes");
    foodMenuPopup.focus( );
}

//***********************
//Google Map Properties *
//***********************

function loadMap( ) {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        //map.addControl(new GOverviewMapControl());
        GEvent.addListener(map, "moveend", function() {
          var center = map.getCenter();
          document.getElementById("mapMessage").innerHTML = center.toString();
        });
        //Central Location When Loaded
        map.setCenter(new GLatLng(33.6376, -111.9264), 13);
        map.openInfoWindowHtml(map.getCenter(),"<strong>La Torretta<br />Ristorante & Italian Steakhouse</strong><br /><em>The Cuisine of Italy</em><br />Located on the SW corner of<br /> Scottsdale Rd and Frank Lloyd Wright");
        //map.setMapType(_mHybridModeShort);
        map.setMapType(G_HYBRID_MAP);
        //map.mapTypes[1]

    }
}

//******************
// FLash Functions *
//******************

//Requires javascript dom support, gracefully fails otherwise
//Loads flash movie in the specified location

//not done yet.
function loadFlashDocument(movieLocation){

}