var BONGUSTO = function() {

  function window_width() {
    if (self.innerHeight) { //non IE
      return self.innerWidth;
    } else if (document.documentElement && document.documentElement.clientHeight) { // IE 6 Strict Mode
    	return document.documentElement.clientWidth;
    } else if (document.body) { // other Explorers
    	return document.body.clientWidth;
    }
  }
  
  function constrain(n, min, max) {
    if (n < min) {
      return min;  
    } else if (n > max) {
      return max;
    } else {
      return n;
    }
  }

  return {
    
    'product_add': function(o, id) {
        var o = $(o);
        
        //unhightlight all
        $A($('content').getElementsByClassName('highlighted')).each(function(o) {
            o.removeClassName('highlighted');
        });
        
        //send "add" ajax request
        var product = o.up('.product');
        if (product) {
            product.addClassName('highlighted'); //highlight current
            var notice = product.down('.notice');
            notice.update('<img src="/img/throbber.gif" alt="Loading..." />');
            new Ajax.Updater(notice, '/controls/ShoppingHandler.ashx', {
                method: 'get',
                parameters: { command: 'add', id: id },
                evalScripts: true
            });
        } else {
            alert('Error: No product found');
        }
        
        //update shopping cart
        new Ajax.Updater('cart', '/controls/ShoppingHandler.ashx', {
          method: 'get',
          parameters: { command: 'cartinfo' },
          evalScripts: true
        });
        
        return false;
    },

    'lightbox': function(url) {
        var l = $('lightbox'), lbg = $('lightbox-bg'); 
        
        lbg.style.display = 'block';  
        l.style.display = 'block';
        if (!window.XMLHttpRequest) { //IE6 doesn't do position: fixed
            var dE = document.documentElement;
            var height = $('main').offsetHeight + 300;
            l.style.height = height+'px';
            lbg.style.height = height+'px';
            l.style.backgroundPosition = (dE.clientWidth/2-770/2)+'px '+(dE.scrollTop+(dE.clientHeight/2)-512/2)+'px';
            lbg.style.background = '#000000';
        }
        l.style.backgroundImage = 'url('+url+')';
        l.onclick = function() {
            lbg.hide();  
            l.style.backgroundImage = '';
            l.hide();  
        }
        return false;	     
    },
    
    'bubble_over': false,
    'bubble_actuator': false,
    'bubble': function(o, text) {
      var bubble, offset, width, left, bubble_width, bitoffset, img;
      
      bubble = $('bubble');
      if(bubble) {
        
        bubble.onmouseover = function() {
          BONGUSTO.bubble_over = true;
        };
        bubble.onmouseout = function() {
          BONGUSTO.bubble_over = false;
          BONGUSTO.bubble();
        };
      
        if(!o) {
          setTimeout(function() {
            if(!BONGUSTO.bubble_over && !BONGUSTO.bubble_actuator) bubble.style.display = 'none';
          }, 300);
        } else {
          BONGUSTO.bubble_actuator = o;
          setTimeout(function() {
            if(BONGUSTO.bubble_actuator == o) BONGUSTO.bubble_actuator = false;
          }, 310);
          bubble.style.display = 'none';
          bubble.removeClassName('yes');
          if($(o).up('div').hasClassName(o.className)) bubble.addClassName('yes');
          $('bubblecontent').update(text);
          width = window_width();
          bubble_width = 200;
          offset = Position.cumulativeOffset(o);
          left = constrain(offset[0]+Math.round(o.offsetWidth/2)-bubble_width/2, 10, width-bubble_width-o.offsetWidth-6);
          bitoffset = offset[0] + Math.round(o.offsetWidth/2) - left - 7;                          
          $('bubblebit').style.backgroundPosition = bitoffset+'px 0';
          bubble.setStyle({
            'left': left+'px',
            'top': offset[1]+o.offsetHeight+2+'px',
            'display': 'block'          
          });
        }
        
      } //if bubble

    }

  };
}();

	// textzoom ///////////////////////////////////////////////////////////////////

	var textzoom_inited = 0;

	function textzoom_init() {
		var initial = 0;
		if(document.cookie) {
			initial = textzoom_readcookie();
			if(initial=='NaN') { initial = "80"; }
		} else if(document.body.style.fontSize) {
			initial = document.body.style.fontSize;
		} else if(document.all && document.styleSheets) {	// ie
			initial = document.styleSheets[0].rules[0].style.fontSize;
		} else if(document.styleSheets) {					// w3c
			initial = document.styleSheets[0].cssRules[0].style.fontSize;
		}
		if(initial && typeof(initial)=="string" && initial.indexOf('%') > -1) { initial = initial.replace('%', ''); }
		if(initial) { textzoom_set(initial); }
		textzoom_inited = 1;
	}

	function textzoom_zoom(delta) {	
		if(textzoom_inited == 0) { textzoom_init(); }
		var dbsf = document.body.style.fontSize;
		var newsize = parseInt(dbsf.substring(0, dbsf.length-1))+delta+'';
		if(newsize.indexOf('-') == -1) {
			textzoom_createcookie(newsize);
			textzoom_set(newsize);
		}
	}
	
	function textzoom_set(newsize) {
		document.body.style.fontSize = newsize+'%';
		if(document.getElementById) {
			if(document.getElementById('currentfontsize')) {
				document.getElementById('currentfontsize').innerHTML = newsize+'%';
			}
		}
	}

	function textzoom_createcookie(val) {
		var date = new Date();
		date.setTime(date.getTime()+(365*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		document.cookie = 'textzoom='+val+expires+'; path=/';
	}

	function textzoom_readcookie() {
		var nameEQ = 'textzoom=';
		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;
	}
