// global namespace
var BC = {
	connection: {}, // global ajax connection maintenance
	event: {},		// global (across all sites) events
	library: {},	// object library (/js/global/modules/.)
	site: {},		// per-site namespace (further defined in each site.js)
	util: {}		// global (across all sites) methods
};

// For any overlays that might be populated
YAHOO.namespace('overlays');

// old stuff

function save_shipping_method(x, y) {
	var f = document.forms[0];
	var c;
	for (var i = 0; i < f.mv_shipmode.length; i++) {
		if (f.mv_shipmode[i].checked) {
			c = f.mv_shipmode[i].value;
		}
	}
	x.href += "&mv_shipmode=" + c;
	if (y) {
		x.href += "#cart";
	}
}

if (typeof(BC.util.dom) != 'object') BC.util.dom = {};

// Cross-browser implementation of el.scrollIntoView(true)
BC.util.dom.scrollIntoView = function(el){
	el = (typeof(el) == 'string') ? document.getElementById(el) : el;
	try{
		// This should work on most browsers
		el.scrollIntoView(true);
	}
	catch(er){
		// Safari 1.2
		var T = 0;
		var pa = el;
		while (pa.parentNode){
			if (pa.offsetTop) T += pa.offsetTop;
			if (pa == document.body) break;
			pa = pa.parentNode;
		}
		window.scrollTo(0, T);
		try{ 
			el.focus();
		}
		catch(er){
			return true;
		}
	}
}

// Returns a stack trace (FF only). Other browsers return null
if (typeof(BC.util.debug) != 'object') BC.util.debug = {};
BC.util.debug.stackTrace = function(el){
    var stack;
    try { throw new Error(); }
    catch(e) { stack = e.stack; }

	if (stack) { // Pretty much only FF supports this
		stack = stack.split("\n");
		stack.shift(); // Remove error generated with try/catch
		stack.shift(); // Remove call to get_stack
		stack = stack.join("\n");
	}
	return stack;
}

function newWindow(url, win_name, height, width, toolbar, menubar, scrollbars, 
                   location, resizable, directories) {

	// Assign Defaults
	height		= (height != null) ? height : 500;
	width		= (width != null) ? width : 525;
	toolbar		= (toolbar != null) ? toolbar: 'no';
	menubar		= (menubar != null) ? menubar: 'no';
	scrollbars	= (scrollbars != null) ? scrollbars: 'yes';
	location	= (location != null) ? location: 'no';
	resizable	= (resizable != null) ? resizable: 'no';
	directories = (directories != null) ? directories: 'no';

	// Create features string
	var features = "'toolbar=" + toolbar +
                   ",menubar=" + menubar +
                   ",scrollbars=" + scrollbars +
                   ",location=" + location +
                   ",resizable=" + resizable +
                   ",directories=" + directories +
                   ",height=" + height +
                   ",width=" + width +
                   "'";
	// Intialize and display our new window
	window.open(url, win_name, features);
}

// hide_hint(ELEMENT_NAME)- hides the element supplied to the function
function hide_hint(element){
	var overlay = YAHOO.overlays.overlay_manager.find(element);
	if($(element + '_content')) {
		yd.setStyle(element+'_content', 'display', 'none');
	}
	overlay.hide();
}

// show_hint(ELEMENT_NAME)- displays the element supplied to the function
function show_hint(popup) {

	if(!YAHOO.overlays.overlay_manager)
		YAHOO.overlays.overlay_manager = new YAHOO.widget.OverlayManager(null, {});

	var paint_popup = function(p) {
		p.show();
		p.center();

		YAHOO.overlays.overlay_manager.register(p);
		YAHOO.overlays.overlay_manager.focus(p);
	}

	// Only create each overlay once
	if(!YAHOO.overlays[popup]) {
		// Move the element off the screen (to reduce flicker)
		yd.setStyle(popup, 'position', 'absolute');
		yd.setStyle(popup, 'display', '');
		yd.setX(popup, -1000);

		YAHOO.overlays[popup] = new YAHOO.widget.Overlay(popup, {
			fixedcenter: true,
			constraintoviewport: true,
			visible: false
		});
	}
	if($(popup + '_content')) {
		yd.setStyle(popup + '_content', 'display', '');
	}

	paint_popup( YAHOO.overlays[popup] );
}

function _readCookie(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 unescape(c.substring(nameEQ.length,c.length));
	}
	return '';
}

function _setCookie(name, value, expires, path, domain, secure) {
	var day = new Date();
	day.setTime(day.getTime());
	var expire_on;

	if (expires == -1) {
		expires_on = 'Mon, 1 Jan 1979 00:00:00 GMT';
	}
	else if (expires > 0) {
		//expiration unit in days.
		expires = expires * 1000 * 60 * 60 * 24;
		var expires_date = new Date(day.getTime() + expires);
		expires_on = expires_date.toGMTString();
	}
	else {
		expires_on = null;
	}

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_on : "" ) + 
	( ( path )    ? ";path=" + path          : "" ) + 
	( ( domain )  ? ";domain=" + domain      : "" ) +
	( ( secure )  ? ";secure"                : "" );

}

// this deletes the cookie when called
function _deleteCookie(name, path, domain) {
	if ( _readCookie(name) )
		document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) + 
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	return;
}
	

function cookiesEnabled() {
	_setCookie('cookies_enabled', 'true', '', '/');
	if (_readCookie('cookies_enabled')) {
		_deleteCookie('cookies_enabled', '/');
		return true;
	}
	else {
		return false;
	}
}

