/*
Yale Appliance Javascript functions
Written or acquired by George M Almasi
*/

// generates and manages the pulldown navs (Sep 2007)

function CoolNav(which, which_pulldown){

	indent_1 = "<img src=\"dot.gif\" width=3 height=1 align=left>";
	indent_2 = "<img src=\"dot.gif\" width=9 height=1 align=left>";
	indent_3 = "<img src=\"dot.gif\" width=15 height=1 align=left>";
	link_count = 0;

	x = 0;
	tier = 0;
	sub_which_h = new Array();
	sub_which_m = new Array();
	CN_link_1 = "<a href=\"javascript:void(0);\" onClick=\"CoolNav('";
	CN_link_2 = "');\">";
	carat_pic_r = "<img src=\"compix/butn_carat_right_07.png\" class=\"pop_nav_carat\">";
	carat_pic_d = "<img src=\"compix/butn_carat_down_07.png\" class=\"pop_nav_carat\">";
	carat_pic_u = "<img src=\"compix/butn_carat_up_07.png\" class=\"pop_nav_carat\">";

	if(which_pulldown == "features" || which_pulldown == "repair_services"){
		real_link_1 = "<a href=\""+non_secure_here+"article.php?which=";
	} else {
		real_link_1 = "<a href=\""+non_secure_here+"subcat.php?which=";
		}

	real_link_2 = "\">";
	sec_tmp = "');\">";
	subnav_text = "";
	sec = "";
	subsec = "";

//	get the tier. This needs to be a discreet step, so don't get cute here.
	for(i=1; i<subcats.length; i++){
		for(k=0; k < 2; k++){
			if(which == subcats[i][k]){
				tier = k;
				}
			}
		}

	// for packages
	if(which.match("appliances","i")){
		subnav_text = indent_1+"<a href=\""+non_secure_here+"package_list.php?which=kitchen|\"><img src=\"compix/butn_carat_right_07.png\" class=\"pop_nav_carat\">Kitchen Packages</a><br>"+indent_1+"<a href=\""+non_secure_here+"package_list.php?which=laundry|\"><img src=\"compix/butn_carat_right_07.png\" class=\"pop_nav_carat\">Laundry Pairs</a><br>";
		link_count = 2;
		}

	for(i=1; i<subcats.length; i++){
	// for the main nav butns

		// set up the "?" fly-out comments
		mo_insert_5 = '';
		mo_insert_6 = '';
		q_mark_insert = '';

// TIER -1
		if(which == which_pulldown && subcats[i][tier] != subcats[i-1][tier] && which == subcats[i][3].toLowerCase()){
		sub_which_m[i] = subcats[i][0];
			sub_which_h[i] = sub_which_m[i].replace(/_/g," ");

			if(subcats[i][tier] == subcats[i][tier+1] && subcats[i][tier+1] == subcats[i][tier+2]){// zero-depth section
				if(sub_which_m[i].match("home","i")){//special for the Home page
					// I'm not sure this EVER actually happens...
					subnav_text = subnav_text + "<img src=\"dot.gif\" width=5 height=1 align=left>" + carat_pic_r + "<a href=\""+non_secure_here+"index.php?which=" + sub_which_m[i] + "|" + subcats[i][4] + real_link_2 + sub_which_h[i] + "</a><br>";
					link_count++;
				} else {
					FlyOutChecker('', subcats[i][6], which_pulldown, link_count);
					subnav_text = subnav_text +  q_mark_insert + indent_1 + carat_pic_r + real_link_1 + sub_which_m[i] + "|" + subcats[i][4] + "\"" + id_insert + mo_insert_6 + ">" + sub_which_h[i] + "</a><br>";
					link_count++;
					}	
			} else {
				lame_link = (subcats[i][tier] == subcats[i][tier+1]) ? "<a href=\"cat.php?which=" : "<a href=\"subdep.php?which="; // should we go to a SubDep, or straight to a Cat?
				lame_link = (which_pulldown == "features") ? "<a href=\"article.php?which=" : lame_link; // Features pulldown?
				
				FlyOutChecker('', '', which_pulldown, link_count);
// Jan 2008. I've had to make the sub_which_h[i] text lead you to a landing page. But I've left the carat as a way to use this amazing nav system.
				subnav_text = subnav_text +  q_mark_insert + indent_1 + CN_link_1 + sub_which_m[i] + "','" + which_pulldown + "');\"" + id_insert + mo_insert_5 + ">" + carat_pic_r + "</a>" + lame_link + sub_which_m[i] + "\">" + sub_which_h[i] + "</a><br>";					
				link_count++;
				}

// TIER 0
		} else if(which == subcats[i][tier]){
			which_mo = 'up';

			if(tier == 0){
				if(!subsec){
					subsec = indent_1 + carat_pic_d + which.replace(/_/g," ") + ":<br>";
					link_count++;
					}
				if((subcats[i][tier] == subcats[i][tier+1]) || (subcats[i][tier+1] == subcats[i][tier+2])){// really shallow section
						sub_which_m[i] = subcats[i][tier+2];				
						sub_which_h[i] = sub_which_m[i].replace(/_/g," ");
						FlyOutChecker('', subcats[i][6], which_pulldown, link_count);
						subnav_text = subnav_text +  q_mark_insert + indent_2 + real_link_1 + sub_which_m[i] + "|" + subcats[i][4] + "\"" + id_insert + mo_insert_6 + ">" + carat_pic_r + sub_which_h[i] + "</a><br>";
						link_count++;
					} else if(subcats[i][tier+1] != subcats[i-1][tier+1]) {
						sub_which_m[i] = subcats[i][tier+1];				
						sub_which_h[i] = sub_which_m[i].replace(/_/g," ");
						FlyOutChecker(subcats[i][5], '', which_pulldown, link_count);
						subnav_text = subnav_text + q_mark_insert + indent_2 + CN_link_1 + sub_which_m[i] + "','" + which_pulldown + "');\"" + id_insert  + mo_insert_5 + ">" + carat_pic_r + sub_which_h[i] + "</a><br>";					
						link_count++;
						}// end if else

// TIER 1
			} else if(tier == 1){
				which_mo = 'up';
				if(subcats[i][tier] == subcats[i][tier-1] && !subsec){// really shallow section
					subsec = indent_1 + carat_pic_d + which.replace(/_/g," ") + ":<br>";
					link_count++;
				} else if(!sec && !subsec) {
					sec = indent_1 + CN_link_1 + subcats[i][tier-1] + "','" + which_pulldown + sec_tmp + carat_pic_u + subcats[i][tier-1].replace(/_/g," ") + ":</a><br>";
					link_count++;
					subsec = indent_2 + carat_pic_d + which.replace(/_/g," ") + ":<br>";
					link_count++;
					}// end if else
					
				sub_which_m[i] = subcats[i][tier+1];				
				sub_which_h[i] = sub_which_m[i].replace(/_/g," ");
				FlyOutChecker('', subcats[i][6], which_pulldown, link_count);
				subnav_text = subnav_text +  q_mark_insert + indent_3 + real_link_1 + sub_which_m[i] + "|" + subcats[i][4] + "\"" + id_insert + mo_insert_6 + ">" + carat_pic_r + sub_which_h[i] +"</a><br>";
				link_count++;
				}//end if elseif elseif	
			}//end if 
		}//end for i

	
// FINISHING touches:
	subnav_text = sec + subsec + subnav_text;

// dynamically generate the pulldown	
	document.getElementById('nav_' + which_pulldown + '_inner').innerHTML = subnav_text;

	}//end func CoolNav -- totally 8)

//######################################################################


function FlyOutChecker(comment5, comment6, which_pulldown, link_count){
	id_insert = " id=\"" + which_pulldown + "_link" + link_count + "\"";
	if(comment5){
		q_mark_insert = "<img src=\"compix/icon_question_pop_nav.png\" class=\"pop_nav_help_icons\" id=\"icon_q\" onMouseOver=\"findPos('" + which_pulldown + "_link" + link_count + "','"+comment5+"');\" onMouseOut=\"PopNavBoxHandler('out');\">";
		}//end if

	if(comment6){
		q_mark_insert = "<img src=\"compix/icon_question_pop_nav.png\" class=\"pop_nav_help_icons\" id=\"icon_q\" onMouseOver=\"findPos('" + which_pulldown + "_link" + link_count + "','"+comment6+"');\" onMouseOut=\"PopNavBoxHandler('out');\">";
		}//end if

	}//end func

var last_which;
function NavClearer(which){
	if(which != last_which){
		which_mo = 'down';
		for(r=0;r<deps.length;r++){
			CoolNav(deps[r][1], deps[r][1]);
			}//end for
		last_which = which;
		}//end if
	}//end func



// ### BEGIN functions for the top nav layers ###

function NavHandler(which, event){
	if(event.type == 'mouseover'){
		document.images[which].src = NavButtons[which][which_mo].src; 
		document.getElementById('nav_'+which).style.display='block';
	} else {
		document.images[which].src = NavButtons[which]['default'].src; 
		document.getElementById('nav_'+which).style.display='none';			
		}//end if else	
		
	}//end func


// mouseover functions by Lindsay Dolph
var NavButtons = new Array(); 

function NavButton(off, hover, clicked) {
	this['default'] = new Image ();
	this['default'].src = off;
	this['down'] = new Image ();
	this['down'].src = hover;
	this['up'] = new Image ();
	this['up'].src = clicked;
	}

var which_mo = 'down';

function RollOver(imgname){ 
	imgname = imgname.toLowerCase();
	document.images[imgname].src = NavButtons[imgname][which_mo].src; 
	} 

function RollOut(imgname){ 
	document.images[imgname].src = NavButtons[imgname]['default'].src; 
	}


function FlashPopUp(URL){
	window.open(URL,'YaleAppliance','width=640,height=240,left=0,top=0,toolbar=No,location=No,scrollbars=No,status=No,resizable=No,fullscreen=No');
	}//end func


function DivToggle(which){
	me = document.getElementById(which);
	if(me.style.display == 'none'){	
		me.style.display='block';
	} else {
		me.style.display='none';
		}	
	}// end func


function BGButnToggle(which,control,butn){
	me = document.getElementById(which);
	that = document.getElementById(control);
	if(that.style.display == 'none'){	
		me.style.background='url(compix/'+butn+'_0.png)';
		me.style.backgroundRepeat = 'no-repeat';
	} else {
		me.style.background='url(compix/'+butn+'_1.png)';
		me.style.backgroundRepeat = 'no-repeat';
		}	
	}//end func


function ImageSizeDetector(which, this_width){
	image_1 = new Image();
	image_1.src = document.getElementById(which).src;
	img_width = image_1.width;
	img_height = image_1.height;
	
	if(img_width > this_width){
		algebra = img_width/this_width;
		new_img_height = img_height/algebra;
		document.getElementById(which).width = this_width;
		document.getElementById(which).height = new_img_height;

		//document.getElementById('big_pic').innerHTML = '<img src="dot.gif" width=50 height=1>> <a href="javascript:void(0);" onClick="BigImagePop(image_1.src);">larger image</a>';
		}//end if
	
	}//end func


// 3 funcs for the "?" flyouts that are NOT in the nav
function PopUpper(obj, which_pop){
	setLyr(obj, which_pop);
	if(document.getElementById(which_pop).style.display == 'none'){
		document.getElementById(which_pop).style.display='block';
	} else {
		document.getElementById(which_pop).style.display='none';
		}//end if
	}//end func

function setLyr(obj,lyr){
	var coors = findPos_2(obj);
	var x = document.getElementById(lyr);
	x.style.top = eval(coors[1]-7) + 'px';
	x.style.left = eval(coors[0]-6) + 'px';
	}//end func

function findPos_2(obj){
	var curleft = curtop = 0;
	if (obj.offsetParent){
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent){
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
			}//end while
		}//end if
	return [curleft,curtop];
	}//end func




// when you click the Buy button, you use this code:
function BuyMe(multiprice_ref, which){
	document.main_form.multiprice_ref.value = multiprice_ref;
	document.main_form.action = 'cart.php?which='+which;
	document.main_form.submit();
	return (false);
	}//end func
	
	
	
//these two do the ? flyouts for the pulldown navs
function findPos(which_link, desc_text){
	/* move up the DOM tree, from the link to it's DIV, to that DIV's DIV.
	this may be overkill, but it took me forever to figure this out, so...*/
	var which_pulldown = document.getElementById(which_link).parentNode.parentNode.id;

	var nav_x = parseInt(document.getElementById(which_pulldown).style.left);
	var nav_y = parseInt(document.getElementById(which_pulldown).style.top);
	var nav_w = parseInt(document.getElementById(which_pulldown).style.width);
	var more_y;

	for(i=0;i<99;i++){
		if('nav_' + which_link == which_pulldown + '_link' + i){
			more_y = eval(i * 18);// CSS: pop_nav has a line-height of 18px
			}//end if
		}//end for

	PopNavBoxHandler('over', which_link, nav_x, nav_y, nav_w, more_y, desc_text);
	}//end func
	

function PopNavBoxHandler(what, which_link, nav_x, nav_y, nav_w, more_y, desc_text){
	da_box = document.getElementById('uni_pop');

	if(what == 'over'){
		document.getElementById('uni_pop_body').innerHTML = desc_text;
		da_box.style.display = 'block';
		da_box.style.left = eval((nav_x + nav_w) - 1) + 'px';
		da_box.style.top = eval((nav_y + more_y) - 0) + 'px';
	} else {
		da_box.style.display = 'none';
		}
	}//end func













/* all this event detection is an attampt to be able to have DIVs overlapping. The problem is, mousing over the top div mouses-off the bottom div, which makes it disappear. For now, I've set the divs, to exist side-by-side, so that triggering the second div does not automatically mouse-off the first div. */
/*
var tmp_x = 0;
function EventDetector(event){
	if(event){
//	var e = eventArgs.EventObject;}
//	var target =Ê(event.relatedTarget) ? event.relatedTarget : event.toElement;
//	alert(event.relatedTarget.tagName);

	if(event.type == 'mouseover'){
//		document.getElementById('GMA_test_over').innerHTML = "OVER: <hr>REL<br>" + event.relatedTarget.tagName + "<br>" + event.relatedTarget.parentNode.id + "<br>" + event.relatedTarget.parentNode.parentNode.id + "<br>" + event.relatedTarget.parentNode.parentNode.parentNode.id + 
//															"<br>CURR<br>" + event.currentTarget.tagName + "<br>" + event.currentTarget.parentNode.id + "<br>" + event.currentTarget.parentNode.parentNode.id + "<br>" + event.currentTarget.parentNode.parentNode.parentNode.id;
//		document.getElementById('GMA_test_over').innerHTML = document.getElementById('GMA_test_over').innerHTML + "<br>" + tmp_x + ": " + tmp_fred + ": " + tmp_barney;
	} else {
//		document.getElementById('GMA_test_out').innerHTML = "OUT <hr>REL<br>" + event.relatedTarget.tagName + "<br>" + event.relatedTarget.parentNode.id + "<br>" + event.relatedTarget.parentNode.parentNode.id + "<br>" + event.relatedTarget.parentNode.parentNode.parentNode.id + 
//															"<br>CURR<br>" + event.currentTarget.tagName + "<br>" + event.currentTarget.parentNode.id + "<br>" + event.currentTarget.parentNode.parentNode.id + "<br>" + event.currentTarget.parentNode.parentNode.parentNode.id;
//		document.getElementById('GMA_test_out').innerHTML = document.getElementById('GMA_test_out').innerHTML + "<br>" + tmp_x + ": " + tmp_fred + ": " + tmp_barney;
		}
	}//end if event
	tmp_x++;
	document.getElementById('GMA_test_over').innerHTML = document.getElementById('GMA_test_over').innerHTML + "<br>" + tmp_x + ": " + event.type + ", " + event.currentTarget.tagName + " " + event.currentTarget.id + " " + event.currentTarget.parentNode.id;
	}//end func
*/

/*
function getRelatedElement(event){ 
	alert("The cursor just exited the " + event.relatedTarget.tagName + " element.");
	}
*/

