/*this function will run on all pages in meatHub
 *
 * @todo make the myJS function snipp the page so that only usefull functions are executed
 */

/* this will be things that can be run before the page is completely loaded*/
BeforeLoad();

function BeforeLoad(){
  
/*page specific*/

}



//window.onload=myJS;-> trying some more elegant event handlers
/* All this sniffing content from http://dean.edwards.name/weblog/2005/09/busted/ */
// for Mozilla browsers
var browser="";
var thisNavigator;
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", myJS, false);
  browser="mozilla";
}

// for IE
/*@cc_on @*/
/*@if (@_win32)
  document.write("<script defer src=ie_onload.js><\/script>");
  browser="ie";
/*@end @*/

// all others
window.onload=myJS;

//this var will keep page specific code from happening more than once
var pageSpec = false;

function myJS(){
thisNavigator=navigator.appName

browserSpecificCode(thisNavigator);
pageSpecificCode();
pageAndBrowserSpecificCode(thisNavigator);

ImpregSearchBox();
CenterFormTextAreas();
//SetLeftNavWidth()
//fixProdBottomLine();
var showRendered=false;
JSInfoGatherer();

}

function JSInfoGatherer(){
fullURL = document.URL;
 if(fullURL.substring(fullURL.indexOf('?')).indexOf("js=true")!=-1){
  //showErrors=true;
  alert(navigator.appVersion);
  document.getElementById("VW").style="";
 }
}

function pageAndBrowserSpecificCode(browserName){
}

function browserSpecificCode(browserName){
	//alert(browserName);
	//document.getElementById('display_menu_2').setAttribute("style",browserName);
     try{
	if (browserName=='Microsoft Internet Explorer'){
		document.getElementById('display_menu_2').setAttribute("style","margin-left:1px;");
	}else{
            document.getElementById('outerPlaceHolder').setAttribute("style","height:338px;"); 
	   document.getElementById('footer').setAttribute("style","height:98px;");
        }  
    }catch(err){

    }
}



function pageSpecificCode(){
if (!pageSpec){
  if(location.href.indexOf('orders.asp') != -1) {
    FormatOrderText();
  }

  if(location.href.indexOf('myaccount=Y') != -1) {
    formatAccountPaymentPages();
  }

  if(location.href.indexOf('MyAccount_GiftBalance.asp') != -1) {
    formatGiftBalancePage();
  }

  if(location.href.indexOf('MyAccount_ApplyGift.asp') != -1) {
    formatApplyGiftPage();
  }

  if(location.href.indexOf('returns.asp') != -1) {
    formatReturnsPage();
  }

  if(location.href.indexOf('affiliate_') != -1) {
    formatAffiliatePages();
  }

  if(location.href.indexOf('help_answer.asp') != -1) {
    formatReturnToHelpPlacement();
  }


/*
  if(location.href.indexOf('MyRewards.asp') != -1) {
    setMyRewardsBannerAtTop();
  }
*/
  if(location.href.indexOf('_p/') != -1) {
    
  }

//sets the help text to spacing specifications
  if(location.href.indexOf('help.asp') != -1) {
   var helpTables = document.getElementById("content_area").getElementsByTagName("table");
   helpTables[0].setAttribute("cellpadding","0");
   helpTables[1].setAttribute("style","margin-top: -10px;");
    
  }

//will run on all the category pages
  if(location.href.indexOf('_s/') != -1) {
    //remove the breadcrumbs
    document.getElementById("content_area").getElementsByTagName("table")[0].setAttribute("style","display:none;");
    // change the margin distance for the main element
   document.getElementById("content_area").getElementsByTagName("table")[2].setAttribute("style","margin-top:8px;");
  }


  if(location.href.indexOf('_s/27.htm') != -1) {
    document.getElementById("productSubText").setAttribute("style","margin-top:-13px;");
  }

 pageSpec=true;
 }
}

function FormatOrderText(){
	var innerTD = document.getElementById('orderDate').parentNode.parentNode;
	innerTD.setAttribute('class','innerTD');
	var orderHTML = innerTD.innerHTML+' ';
	innerTD.innerHTML = orderHTML;
}


//this takes the search box at the top of the page and allows it to have backcolor, initial text, and click->clear capabilities
function ImpregSearchBox(){
              var SearchBox = document.getElementById("display_search_box");
              SearchBox.getElementsByTagName("input")[0].value="SEARCH";
              SearchBox.getElementsByTagName("input")[0].setAttribute('onclick',"clearInitial(this,'SEARCH')");
              SearchBox.getElementsByTagName("input")[0].setAttribute('font-weight',"font-style:bold;");
              SearchBox.getElementsByTagName("input")[0].setAttribute('class',"mainSearches");
              SearchBox.getElementsByTagName("input")[0].className="mainSearches";
}

//this will center all the text in the QTY boxes on the first checkout page
function CenterFormTextAreas(){
              //collect all the inputs
              var inputs = document.getElementsByTagName("input");
              var inputs;
              //start the counter at 1;
              var counter=1;
              //iterate through all of the inputs
              for (var i=0; i<inputs.length; i++){
                            //they should have id's like Quantity1, Quantity2, Quantity3
                            if (inputs[i].id=="Quantity"+counter){
                                          inputs[i].setAttribute('style','text-align:center;');
                                          counter++;
                            }
              }
}


//this function will try to set the width of the column that the left-nav sits in
function SetLeftNavWidth(){
              document.getElementById('left_nav').setAttribute('width','auto');
}


function setMyRewardsBannerAtTop(){
   //var bannerContainer = document.getElementById("rewardsBannerImage");
   var banner = '<img src="/v/vspfiles/photos/categories/25.jpg" />';
   var content = document.getElementById("content_area");
   var contentHTML = banner+"\n"+content.innerHTML;

   //bannerContainer.innerHTML="";
   content.innerHTML=contentHTML;
}

function clearInitial(clickedBox,initText){
  //var useNode = document.getElementById(clickedBox);
  if (clickedBox.value==initText){
              clickedBox.value="";
  }
}
 
 // set the yellow box on the top of the payment pages flush with the text on the left-hand side of the page
function formatAccountPaymentPages(){
 document.getElementById("content_area").getElementsByTagName("table")[0].setAttribute("style","margin-left:10px"); 
}
//aligns the text in the table on the gift card balance page
function formatGiftBalancePage(){
 document.getElementById("content_area").getElementsByTagName("form")[0].setAttribute("style","margin-left:-4px");
}

//format the text on the Affiliate Pages
function formatAffiliatePages(){
 document.getElementById("div_articleid_8").getElementsByTagName("div")[0].setAttribute("style","margin-left:-10px; width:610px;");
}

 //format the text on the applygift.asp page
 function formatApplyGiftPage(){
 var spacerImage = document.getElementById("content_area").getElementsByTagName("img")[1];
 spacerImage.setAttribute("width","10");
 spacerImage.setAttribute("height","43");
}
 
function formatReturnsPage(){
 document.getElementById("div_articleid_3").setAttribute("style","width: 620px; margin-left: -10px");
}
 

function formatReturnToHelpPlacement(){
 var useImg =  document.getElementById("content_area").getElementsByTagName("img")[1];
 useImg.setAttribute("style","vertical-align:text-bottom;");
 useImg.setAttribute("width","15");
 useImg.setAttribute("height","26");
}
 
 
 
   /*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/	
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
}

/* Below here are useful and available functions */

function MyLocalRedirect(url){
   window.location=url;
}

//returns the element n parents behind the element given
function NParent(n, element){
	var elemParent=element;
	for (var i=0; i<n;i++){
		elemParent=elemParent.parentNode;
	}
	return elemParent;
}

//allows one to parse through the url to get at the name=value data (ex @ 'http://tada.com/index.php?as=1&is=hi' gup('is'); returns 'hi')
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function OpenMyNewWindow(url, width, height) {
var myWin = window.open(url, null, 'top=10,left=10,menubar=0,resizable=1,scrollbars=1,width=' + width + ',height=' + height);

//AppendElement(myWin.document.getElementsByTagName("body")[0],"text","bottomText","MeatHub.com");
myWin.document.getElementsByTagName("body")[0].setAttribute("onload","testing");
}

function AppendElement(node,tag,id,htm){
  var ne = document.createElement(tag);
  if (id) ne.id=id;
  if (htm) ne.innerHTML-htm;
  node.appendChild(ne);
}

//hides the space where the breadcrumbs would go on the product pages
function HideBreadCurmbSpace(){
document.getElementById("content_area").getElementsByTagName("form")[0].getElementsByTagName("table")[0].setAttribute("style","margin-top:-18px;");
}

//hides the breadcrumb content on the the category pages
function HideCategoryBreadCrumbs(){
    document.getElementById("content_area").getElementsByTagName("table")[0].setAttribute("style","display:none;");
    // change the margin distance for the main element
   document.getElementById("content_area").getElementsByTagName("table")[2].setAttribute("style","margin-top:8px;");
}
