/*
 * 
 * nbb_util.js: standard javascript client-side support for nbb.
 *
 * note: if you add to this, please prefix function names by "nbb_" so i know
 *       where they come from.
 * 
*/

/*
 * functions for manipulating cookies with javascript.  originally included to augment
 *  quickquote functionality, but i'm sure i will abuse this in the future.
*/

function nbb__create_cookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function nbb__read_cookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function nbb__erase_cookie(name) {
	createCookie(name,"",-1);
}

/*
 * some very unsavory javascript to determine browser's client-area dimensions & scroll offsets
 *
 * you know, for the kids
 *
*/
var sz_x = 0;
var sz_y = 0;
var sz_offset_x = 0;
var sz_offset_y = 0;

function nbb_w_sz_proc() {
		if( typeof( window.innerWidth ) == 'number' ) {
          //Non-IE
          sz_x = window.innerWidth;
          sz_y = window.innerHeight;
        } else if( document.documentElement &&
                ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
                //IE 6+ in 'standards compliant mode'
                sz_x = document.documentElement.clientWidth;
                sz_y = document.documentElement.clientHeight;
        } else if( document.body &&
                ( document.body.clientWidth || document.body.clientHeight ) ) {
                //IE 4 compatible
                sz_x = document.body.clientWidth;
                sz_y = document.body.clientHeight;
        }

        // compute scroll offsets
        sz_offset_x = 0, sz_offset_y = 0;
        if( typeof( window.pageYOffset ) == 'number' ) {
          //Netscape compliant
          sz_offset_y = window.pageYOffset;
          sz_offset_x = window.pageXOffset;
        } else if( document.body &&
                ( document.body.scrollLeft || document.body.scrollTop ) ) {
                //DOM compliant
                sz_offset_y = document.body.scrollTop;
                sz_offset_x = document.body.scrollLeft;
        } else if( document.documentElement &&
                ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
                //IE6 standards compliant mode
                sz_offset_y = document.documentElement.scrollTop;
                sz_offset_x = document.documentElement.scrollLeft;
        }
}

function nbb_w_sz_mod() {
	nbb_w_sz_proc();
	
	/* don't mess with the homepage columns */
	if (home_page == 1) return;
	
	/* left hand column */
	if (sz_y > 440) {
		if (sz_y > 700) sz_y = 700;
		$("nbb_GL_LH_C").style.position = "relative";
		$("nbb_GL_LH_C").style.height = "" + (sz_y - 120 ) + "px";
	}
	
	/* right hand column 
	 * 
	 * since the right hand column will usually be longer than the
	 * left, we'll assume [for now] that we don't need to increase
	 * it's length
	*/
	/*
	$("nbb_GL_RH_C").style.position = "absolute";
	$("nbb_GL_RH_C").style.height = "" + (sz_y - 123 ) + "px";
	*/

	/* footer: we shouldn't have to jiggle the footer as long as
	 * 	the right hand column remains the longest . . . 
	 *
	 * i really dislike this "fix".
	 *
	*/
}

/*
 * ajax ui helpers
*/
function nbb_aj_start() {
	if ($("aj_wait")) {
		$("aj_wait").style.display = '';
	}
}

function nbb_aj_stop() {
	if ($("aj_wait")) {
		$("aj_wait").style.display = 'none';
	}
}

/*
 * category-page specific functions
*/

function nbb_nudgeCategoryImage() {
	if ($("nbb_categoryImageContainer")) {
		var ice = $("nbb_categoryImageContainer");
		ice.style.position = 'relative';
		ice.style.top = '-65px';
		ice.style.margin = '5px';
	}
}

/*
 * nbb_browserCategoryHack
 *
 * nudge the category paragraph up, set some styles so the
 *  description paragraph on the category page renders correctly.
 *
 * called from: footer.js : nbb_windowOnload
 *
*/
function nbb_browserCategoryHack() {
	if ($("nbb_safariCategoryHack")) {
		var bubbleHeadOffsetValue = nbb_dyn_bubblehead_t();
		
		var el = $("nbb_safariCategoryHack");

		/* i hate to say it, but i have no idea where the little funky characters are coming from
		 *  on the aluminum blinds description; i have looked EVERYWHERE and this is the only way i can
		 *  get rid of them.  chalk this one up to yet another mystery of the information superhighway.  ....
		*/
		var str = el.innerHTML;
		str = str.replace(/[^\n\r -~]/g, "");
		el.innerHTML = str;
		/* move along nothing to see here */

		if (BrowserDetect.browser == "Safari") {
			el.style.position = 'relative';
			el.style.top = bubbleHeadOffsetValue + 'px';
			el.style.margin = '13px';
			//el.style.left = '4px';
			
			var cel = $("nbb_categoryImageContainer");
			cel.style.position = 'relative';
			cel.style.top = bubbleHeadOffsetValue + 'px';
			cel.style.left = '-4px';
			
			var ce = $("center_content");
			ce.style.position = 'relative';
			ce.style.left = '5px';
			
		} else if (BrowserDetect.browser == "Firefox") {
			var cel = $("nbb_categoryImageContainer");
			if (cel) {
				cel.style.position = 'relative';
				cel.style.top = (bubbleHeadOffsetValue+5) + 'px';
			}

			/* swap div content into span & hide div to
			 *  ensure proper formatting
			*/
			$("nbb_firefoxCategoryHack").innerHTML =
				$("nbb_safariCategoryHack").innerHTML + "<br>";
			$("nbb_safariCategoryHack").style.display = 'none';	
			$("nbb_firefoxCategoryHack").style.position = 'relative';
			$("nbb_firefoxCategoryHack").style.top = '10px';
			$("nbb_firefoxCategoryHack").style.left = '2px';
				
		} else {
			el.style.margin = '6px';
		}
	}
	
	/* once everything is moved around, display.  this containing
	 *  element is originally set to visibility=hidden to avoid
	 *  the visitor seeing the browser playing musical chairs with
	 *  the various content components.
	*/
	if ($("nbb_categoryDescriptionBlockContainer")) {
		$("nbb_categoryDescriptionBlockContainer").style.visibility = '';
	}
	
}

/*
 * nbb_testimonialImageHack
 *
 * nudge the testimonial rounded corner link down a few pixels
 *  so it renders correctly in firefox.
 *
*/
function nbb_testimonialImageHack() {
	if ($("nbb_testimonialImageHack")) {
		var tie = $("nbb_testimonialImageHack");
		tie.style.top = '8px';
	}
}

/*
 * nbb_quickquote_row_hack
 *
 * pixel-height adjustments for different UI inconsistencies
 *
 * you really, really don't want to have to change this if you can help it.
 *
*/
function nbb_quickquote_row_hack() {
	if ($('sr_qq_h_hack')) {
		if (BrowserDetect.browser == "Firefox") {
			$('sr_qq_h_hack').style.position = 'relative';
			$('sr_qq_h_hack').style.top = '-10px';
		} 
	}
	if ($("ff_qq_h_hack")) {
		$('ff_qq_h_hack').style.position = 'relative';
		$('ff_qq_h_hack').style.top = '5px';
	}
	if ($("pr_qq_h_hack")) {
		if (BrowserDetect.browser == "Firefox") {
			$("pr_qq_h_hack").style.position = "relative";
			$("pr_qq_h_hack").style.top = "8px";
			$('pr_qq_h_hack_h').style.position = "relative";
			$('pr_qq_h_hack_h').style.top = "4px";
		} else if (BrowserDetect.browser == "Explorer") {
			$("pr_qq_h_hack").style.position = "relative";
			$("pr_qq_h_hack").style.top = "7px";
			$('pr_qq_h_hack_h').style.position = "relative";
			$('pr_qq_h_hack_h').style.top = "5px";
		}
	}
}

/*
 *
 * OVERLAY AND WINDOWING SYSTEM FUNCTIONS
 *
*/

/* IE fallback for nbb_modal_dialog funk (IE is special in how it provides relative coordinates for 'browser-document center') */
function IE_popUp(w, h, URL) {
	if (!w || w ==0) w=450;
	if (!h || h ==0) h=450;
	var day = new Date();
	var id = day.getTime();
	var x_c = Math.round(screen.width / 2 - (w / 2));
	var y_c = Math.round(screen.height / 2 - (h / 2));
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=" + w + ",height=" + h + ",left = " + x_c + ",top = " + y_c + "');");
}

function nbb_modal_dialog(u, w, h) {
	if (BrowserDetect.browser == "Explorer") {
		IE_popUp(w,h,u)
	} else {
		Dialog.alert({url: u, options: {method: 'get'}}, 
			{className: "alphacube", width:w, height:h, okLabel: "Close"});
	}
}

/*
 *
 * dynamic content endpoint functions
 *
 */

/* (?) idea: functions to cache the contents of dynamic content endpoints & restore at a later date. */

/*
 * nbb_show_dyn_?(): make visible a dynamic endpoint's content [c,r,l]
*/

function nbb_show_dyn_c() {
	nbb_show_dyn("center_column_content");
}

function nbb_show_dyn_r() {
	nbb_show_dyn("rh_column_content");
}

function nbb_show_dyn_l() {
	nbb_show_dyn("lh_column_content");
}

/*
 * nbb_hide_dyn_?(): hide a dynamic endpoint's content [c,r,l]
*/

function nbb_hide_dyn_c() {
	nbb_hide_dyn("center_column_content");
}

function nbb_hide_dyn_r() {
	nbb_hide_dyn("rh_column_content");
}

function nbb_hide_dyn_l() {
	nbb_hide_dyn("lh_column_content");
}

/*
 * nbb_set_dyn_?( content ): set a dynamic endpoint's content [c,r,l]
*/

function nbb_set_dyn_c(c) {
	nbb_set_dyn("center_column_content", c);
}

function nbb_set_dyn_r(c) {
	nbb_set_dyn("rh_column_content", c);
}

function nbb_set_dyn_l(c) {
	nbb_set_dyn("lh_column_content", c);
}

/*
 * nbb_set_dyn(endpoint_id, content): set a element [endpoint_id] to contain certain content.
 *
 * note: this does not automatically make the content visible.
 *
*/

function nbb_set_dyn(ep, c) {
	$(ep).innerHTML = c;
}

/*
 * nbb_show_dny( endpoint_id ): make visible an element [endpoint_id]
*/

function nbb_show_dyn(ep) {
	$(ep).style.display = '';
}

/*
 * nbb_hide_dny( endpoint_id ): hide an element [endpoint_id]
*/

function nbb_hide_dyn(ep) {
	$(ep).style.display = 'none';
}

/*
 * 
 * tooltip stub functions
 * note: i will revisit this later when i can find some acceptable tooltip code & design
 *
*/

function ajax_hideTooltip() { }
function ajax_showTooltip() { }
