/*
Nav script 2.0

don't set a height for the menus!
it pushes things around

about scriptaculous' morph:
overflow must be set to 'hidden' on the UL tag for it to work (why?)
*/


function hideMenu (aObj) {
	// set main menu background image to default
	
	aObj.style.backgroundImage = "url('/images/nav_bg.png')";
    // hide sub menu
    $("SubMenu_" + aObj.id.split("_")[1]).style.display = "none";
    //listObj.down('ul.subMenu').style.height = "0";
    //$('search_cat').show();
}

function showMenu (aObj) {
    // set the main menu item background image to highlight
    aObj.style.backgroundImage = "none";
    var theID = aObj.id.split("_")[1];
    /*
    if ((theID == "2") || (theID == "1")) {
	    $('search_cat').hide();
    }
    */
    // display sub menu
	 $("SubMenu_" + theID).style.display = "block";	
	/*
	new Effect.Morph(listObj.down('ul.subMenu').id , {																			
	    style:'height:' + (((listObj.down('ul.subMenu').descendants().length)/2)*22) + "px",  // line height 16, top padding 2, bottom padding 3, border bottom 1
		duration:0.2
	});
	*/
}

function navOver (e) {
  // find the main menu item object
  var obj = Event.element(e);
  var itemToShow;
  
  // loop through the main items, if its the one triggering the event, show its submenu, otherwise hide the other submenus
  $$('.mainMenuLink').each (function s(x) {
    if (x.id == obj.id) {
	  // menu highlight
	  itemToShow = x;
	  
	} else {
	  
	  if (x.style.backgroundImage.length > 0) {
	      hideMenu (x);
	      Event.observe(x,'mouseover',navOver);
	  }
	  
	}
  });
  
  showMenu (itemToShow);
  Event.stopObserving(itemToShow);
  Event.observe(document.body, 'mouseover', navOut);
  
  Event.stop(e);
}

function navOut (e) {
  var theElement = Event.element(e);
  if (theElement.descendantOf('topnav') == false) {
      var menuObjs = $$(".mainMenuLink");
      // hide the menus
      menuObjs.each( function s(x) {
	      hideMenu(x);
		  Event.observe(x, 'mouseover', navOver);
	  });	
      Event.stop(e);
  }
}


/* filters */
function setFilter (obj, filter) {
	var allSearchPairs = location.search.split("&");
	if (allSearchPairs[0].charAt(0) == "?") allSearchPairs[0] = allSearchPairs[0].slice(1); // removes the leading "?"
	for (var z=0; z<allSearchPairs.length; z++) {
		// filter filters 
		if ((allSearchPairs[z].split("=")[0] == "page") || (allSearchPairs[z].split("=")[0] == "low_price") || (allSearchPairs[z].split("=")[0] == "high_price")) {
			allSearchPairs[z] = "";
		}
	}
	var startLoc = location.search.search("filter_id");
	if (filter > 0) {
	    if (startLoc > 0) {
	        if (obj.checked) {
		        // add manufacturer to the list				
    			for (var i=0; i<allSearchPairs.length; i++) {
				    if (allSearchPairs[i].split("=")[0] == "filter_id") {
					    allSearchPairs[i] += "x" + filter;
					}
				}
    		} else {
				// remove the manufacturer from the list
    			for (var i=0; i<allSearchPairs.length; i++) {
					if (allSearchPairs[i].split("=")[0] == "filter_id") {						
						if (allSearchPairs[i].search("x") >= 0) {
						    var allFilters = allSearchPairs[i].split("=")[1].split("x");
						    for (var j=0; j<allFilters.length; j++) {
							    if (allFilters[j] == filter) allFilters[j] = "";
						    }
						    allSearchPairs[i] = "filter_id=" + allFilters.without("").join("x"); // prototype js
						} else {
							allSearchPairs[i] = "";
						}
					}
				}
			}
		} else {
			if (obj.checked) {
				// create the filter parameter
				allSearchPairs.push("filter_id=" + filter);
			} else {
				// it should never get here
			}
		}
	    // reconstruct the search query
	    location.search = allSearchPairs.without("").join("&");  // prototype js
	}
}

function setPrice (obj, lowPrice, highPrice) {
	var startLoc = location.search.search("low_price");
	var low_loc = -1;
	var high_loc = -1;
	var allSearchPairs = location.search.split("&");
	if (allSearchPairs[0].charAt(0) == "?") allSearchPairs[0] = allSearchPairs[0].slice(1);
	for (var z=0; z<allSearchPairs.length; z++) {
    	if (allSearchPairs[z].split("=")[0] == "page") {
			allSearchPairs[z] = "";
		}
	}
	// get the initial values for low and high price
	var low_price = 0;
	var high_price = 0;
	if (startLoc >= 0) {
		for (var i=0; i<allSearchPairs.length; i++) {
			if (allSearchPairs[i].split("=")[0] == "low_price") {
				if (allSearchPairs[i].split("=")[1] > 0) low_price = allSearchPairs[i].split("=")[1];
				low_loc = i;
			}
			if (allSearchPairs[i].split("=")[0] == "high_price") {
				if (allSearchPairs[i].split("=")[1] > 0) high_price = allSearchPairs[i].split("=")[1];
				high_loc = i;
			}
		}
	}
	// modify the range
	if (obj.checked) {
		// adding filter
		if (startLoc >= 0) { 
		    allSearchPairs[low_loc] = "low_price=" + Math.min(lowPrice, low_price);
		    allSearchPairs[high_loc] = "high_price=" + Math.max(highPrice, high_price);
		} else {
			// price filter wasn't set
			allSearchPairs.push("low_price=" + lowPrice, "high_price=" + highPrice);
		}
	} else {
		// removing filter
		if (startLoc >= 0) {
			if ((highPrice == high_price) && (low_price == lowPrice)) {
				// erase the price filter
				allSearchPairs[low_loc] = "";
				allSearchPairs[high_loc] = "";
			} else {
				// correct the range
			    if (lowPrice == low_price) {
					// lowest filter was clicked off
					allSearchPairs[low_loc] = "low_price=" + highPrice;
					allSearchPairs[high_loc] = "high_price=" + high_price;
				}
			    if (highPrice == high_price) {
					// highest filter was clicked off
					allSearchPairs[low_loc] = "low_price=" + low_price;
				    allSearchPairs[high_loc] = "high_price=" + lowPrice;
				}
				// middle filter was clicked off
				if (lowPrice > low_price) {
					allSearchPairs[high_loc] = "high_price=" + lowPrice;
				} else if (highPrice < high_price) {
					allSearchPairs[low_loc] = "low_price=" + highPrice;
				}
			}
		}
	}	
	// reconstruct the search query
	location.search = allSearchPairs.without("").join("&");  // prototype js
}

function headerShippingPopup(e) {
    window.open('/shipping_pop.html', '_blank', 'scrollbars=yes,height=600,width=600,titlebar=no,resizeable=no,top=100,left=100,status=no,menubar=no');
    Event.stop(e);
}





/* the main loader */
	
var column = 236;
//var widths = new Array (2.3,1,1.5,1.01);
var widths = new Array (2.3,1,1.5,1.5); // updated 11/03/09 (josh)
	
document.observe('dom:loaded', function s() {
	var lp = 0;
	var linksPerColumn = 9;
	var verticalOffset = 6;
	var horizontalOffset = 12;
	var linkHeight = 21;
	var columnWidth = 170;
	var specialOffset = 0;
	
    $$(".mainMenuLink").each ( function t(x) {
	    var topPos = 0;
	    var leftPos = 0;
	    var lp2 = 0;
  	    // first we juggle
  	    var theUL = x.up().down('ul');  	    
  	    theUL.childElements().each ( function v(y) {
	  	    y.style.position = "absolute";
	  	    topPos = (lp2 - (Math.floor(lp2/linksPerColumn) * linksPerColumn)) * linkHeight + verticalOffset;
	  	    leftPos = horizontalOffset + Math.floor(lp2/linksPerColumn) * columnWidth;
	  	    y.style.top = topPos + "px";
	  	    y.style.left = leftPos + "px";
	  	    lp2++;	   	    
   	    });
	    
	    
	  // add listener
      Event.observe(x, 'mouseover', navOver, false);	
      x.style.textDecoration = "none";
	  
      /////////////////////////////////////
      // 11/03/09 (josh. my birthday)
      // updated menu to accomodate 2 columns on Toys and Games
      // This is a bit of hack.
      // but the whole menu is a little bit of a hack
      ////////////////////////////////////////
      var menuNumber = x.id.split("_")[1];
      var theLI = $("SubMenu_" + menuNumber);
      
      ////////////////////////////
      // menuNumber 4 is the last menu.
      //////////////////////////
      if (menuNumber == 4) {
	      specialOffset = 0 - 116;
      }
      theLI.style.left = ((lp * column) + specialOffset) + "px";	      
      theLI.style.width = (Math.floor(widths[lp] * (column))) + "px";
	      
      lp = lp + 1;
	});
	
	
	// top free shipping logo loader
	$("freeShipMap").observe('click', headerShippingPopup);	
	
});	

