﻿var sCurrentPage;

// This is called every time a page loads
function GlobalInit() {
    // The Bread logo at the top changes for each section
    PopulateBreadLogo();
    
    // Correct all PNGs in IE6. The above code gets rid of the odd flicker but does not affect background images
    $(document).pngFix();
    
    // Turn the current page nav on
    TogglePageNav(sCurrentPage, 'on');
    
    // Set the rollovers for the nav images
    $('#navigation img').hover(function() {
        TogglePageNav(this.id, 'on');                    
    }, function() {
        TogglePageNav(this.id, 'off');
    });
    
    // Turn autocomplete on for the site search
    $('#txtSiteSearch').autocomplete(sSiteSearchTerms, { scrollHeight: 100 } );
}

function PopulateBreadLogo() {
    var sImgSource;
    var sImgAltText;
    
    switch(sCurrentPage) {
        case "products":
            sImgSource = "/Images/logoBreadForYou.png";
            sImgAltText = "Bread For You";
            break;
        case "nutrition":
            sImgSource = "/Images/logoBreadForHealth.png";
            sImgAltText = "Bread For Health";
            break;
        case "recipes":
            sImgSource = "/Images/logoBreadForAll.png";
            sImgAltText = "Bread For All";
            break;
        case "about":
            sImgSource = "/Images/logoBreadPerfected.png";
            sImgAltText = "Bread Perfected";
            break;
        case "thins":
            sImgSource = "/Images/logoBreadForYou.png";
            sImgAltText = "Bread For You";
            break;
        default:
            sImgSource = "/Images/logoBreadForLife.png";
            sImgAltText = "Bread For Life";
    }
    
    $('#imgLogoBreadForLife').attr('src', sImgSource);
    $('#imgLogoBreadForLife').attr('alt', sImgAltText);
}

function OpenWindow(sURL, nWidth, nHeight) {
		wContent=window.open(sURL,'wContent','left=100,top=100,screenX=100,screenY=100,width=' + nWidth + ',height=' + nHeight + ',scrollbars=no,location=no');
		wContent.focus();
	}

function RemoveHash(sHashString) {
    return sHashString.substring(1,sHashString.length);
}

function TogglePageNav(sPage, sState) {
    // Never turn the current page nav off
    if ((sPage != sCurrentPage) || (sState == "on")) {
        $('#' + sPage).attr('src','/Images/' + sPage + '_' + sState + '.gif');
    }
}

function ToggleTab(sTab, sState) {
    $('#' + sTab).attr('src','/Images/Inner/' + sTab + '_' + sState + '.gif');
}

function GetScrollY() {
  var nScrollY = 0;
  if( typeof( window.pageYOffset ) == "number" ) {
    //Firefox compliant
    nScrollY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    nScrollY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE standards compliant mode
    nScrollY = document.documentElement.scrollTop;
  }
  return nScrollY;
}

function ValidateChecked(sender, e) {
    var oSender = getObject(sender.id.replace("cv_",""));
    
    if (oSender.checked) e.IsValid = true;
    else e.IsValid = false;
}

function ClearText(txtField){
    if (txtField.defaultValue==txtField.value) {
        txtField.value = "";
        txtField.style.color = "#000000";
        txtField.style.fontStyle = "normal";
    }
}

function FillText(txtField){
    if (txtField.value=="") {
        txtField.value = txtField.defaultValue;
        txtField.style.color = "#666666";
        txtField.style.fontStyle = "italic";
    }
}

function HighlightSelectText(ddlstField){
    ddlstField.style.color = "#000000";
    ddlstField.style.fontStyle = "normal";
}

function getObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		return document.getElementById(objectId);
    } else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		return document.all(objectId);
    } else {
		return false;
    }
} // getObject

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
		// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
    } else {
		return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
        styleObject.visibility = newVisibility;
        return true;
    } else {
		//we couldn't find the object, so we can't change its visibility
		return false;
    }
} // changeObjectVisibility

function changeObjectDisplay(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
        styleObject.display = newVisibility;
        return true;
    } else {
		//we couldn't find the object, so we can't change its visibility
		return false;
    }
} // changeObjectVisibility