

	// Y
	var Y = YAHOO,
		$d = YAHOO.util.Dom,
		$e = YAHOO.util.Event,
		$c = YAHOO.util.Connect,
		$j = YAHOO.lang.JSON,
		$a = YAHOO.util.Anim,
		$ = YAHOO.util.Dom.get;
 
	// namespace
	Y.namespace('BitsyBox');
 
	// getGoodTarget
	Y.getGoodTarget = function(el,get,limit) {
 
		// stuff
		var i = 0;
 
		// no limit
		if ( !limit ) limit = 5;
 
		// what type
		if ( typeof get == 'string' ) {
 
			// loop
			while ( el && !$d.hasClass(el,get) &&  i < limit ) {
				el = el.parentNode;
				i++; 
			}
 
			// what
			if ( el && $d.hasClass(el,get) ) {
				return el;
			}
 
		}
 
		// bad
		return false;
 
	}
 
	// BitsyBox
	Y.BitsyBox = function(p) {
		this.init(p);
	}
 
		// prototype
		Y.BitsyBox.prototype = {
 
			// properties
			params : {},
			data : { panels: {} },
 
			// custom events
			onDocClick : {},
 
			// init 
			init : function(p) {
 
				// set
				this.params = p;
 
				// cutom eents
				this.onDocClick = new Y.util.CustomEvent('click',this);				
 
				// attach events handler
				$e.on(document.body,'click',this.click,this,true);
				$e.on(document.body,'mouseover',function(e){ this.mouse(e,'over') },this,true);
				$e.on(document.body,'mouseout',function(e){ this.mouse(e,'out') },this,true);			
 			
 				// execute
				$e.on(window,'load',BB.executeQueue);
 				 				
 			
			},
 
			// go to url
			goToUrl : function(url) {
				location.href = url;
			},
 
			// click
			click : function(e) {
 
				// get target
				var tar = oTar = $e.getTarget(e);
 
				// fire it
				this.onDocClick.fire({'tar':tar,'event':e});
 
				// what up 
				if ( ( tar = Y.getGoodTarget(oTar,'purchase',5) ) && tar.tagName.toLowerCase() == 'a' ) {
				
					// stop
					$e.stopEvent(e); tar.blur();			
					
					// id
					var id = 'purchase-panel';		
					
					// puroverlay
					if ( !this.data.panels[id] ) {
						// create a panel
						this.data.panels[id] = new Y.BitsyBox.Panel({'name':id,'overlay':true,'noOpen':true});
					}
					
					// add loading
					$d.addClass(tar,'loading');
				
					// load 
					this.data.panels[id].load({
						'url': tar.getAttribute('href'),
						'open': true,
						'onLoad': function() {
							$d.removeClass(tar,'loading');
						}
					});				
				
				}
				else if ( ( tar = Y.getGoodTarget(oTar,'panel',5) ) ) {
 
					// stop
					$e.stopEvent(e); tar.blur();
 
					// id
					var id = tar.id;
 
					// check if panel exist
					if ( id && this.data.panels[id] ) {
						this.data.panels[id].open(); return;
					}
 
					// name id
					if ( !id || id == "" ) {
						id = $d.generateId();
					}
 
					// create a panel
					var panel = new Y.BitsyBox.Panel({'name':id,'overlay':true,'noOpen':true});
 
					// load 
					panel.load({
						'url': this.addParamsToUrl(tar.getAttribute('href'),{'.context':'xhr'}),
						'open': true
					});
 
					// set it 
					this.data.panels[id] = panel;
 
					// add id to lik
					tar.id = id;
 
				}
 
			},
 
			// mouse
			mouse : function(e,type) {
 
				// get it 
				var tar = oTar = $e.getTarget(e);
 
			},
			
			initPricingPage : function() {
			
				// loop through each one 
				for ( var p in BB.Data.pricing ) {
					if ( typeof p == 'string' ) {
						
						// options
						var o = BB.Data.pricing[p];					
						
						// build a slider
						$("#slider-"+p).slider({
							'max': o[4],
							'min': 1,
							'step': 1
						});
						
						// data
						BB.Data.pricing[p].number = 1;						
						
						// event
						$("#slider-"+p).bind('slide slidechange',[p,o,this],function(event,ui){
						
							// set it 
							var d = event.data;
						
							// val
							var val = parseInt($(this).slider('value'));				
						
							// set it 
							$("#val-"+d[0]).html(val*d[1][3]);
							$("#opt-"+d[0]).val(val);
						
							// number
							BB.Data.pricing[d[0]].number = val;
						
							// calc
							d[2].calcPricingTotal();
						
						});
						
						
						/*

						// slider
						var slider = YAHOO.widget.Slider.getHorizSlider("sliderbg-"+p, "sliderthumb-"+p, -2,200,1);
						
						
						BB.Data.pricing[p].number = 1;						
						
						// set the id
						slider.optId = p;
						slider.parent = this;
					
						// slider	
				        slider.subscribe("change", function(offset,s) {
							
							// plan
							var plan = BB.Data.pricing[s.optId];							
							
							var n = Math.ceil(offset/2);
							
							var v = Math.round(n*plan[1]);
									
								// number
								BB.Data.pricing[s.optId].number = n;
							
				            // update the text box with the actual value
							$('val-'+s.optId).innerHTML = v;
				
							// do total
							s.parent.calcPricingTotal();				
				
				        },slider);
						
*/
						
					} 
				}
			
			},
			
			calcPricingTotal : function() {
			
				var total = 0.00;
			
				for ( var p in BB.Data.pricing ) {
					if ( typeof p != 'function' ) {

						var n = parseInt(BB.Data.pricing[p].number);
						
						if ( n > 0 ) {

							total += parseFloat(n*(BB.Data.pricing[p][0]*1));
						
						}
						
					}
				}
				
				$('#val-1').html("$"+Math.round(total)+".00<span>/month</span>");
			
			},
 
			// add query string to url
			addParamsToUrl : function(url,params) {
 
				var qs = [];
 
				for ( var p in params ) {
					qs.push(p+"="+params[p]);
				}
 
				// what 
				if ( url.indexOf('?') !== -1 ) {
					return url + '&' + qs.join('&');
				}
				else {
					return url + '?' + qs.join('&');
				}
 
			}
 
		};
 
	// namespace
	Y.namespace("BitsyBox.Panel");
 
	// panel
	Y.BitsyBox.Panel = function(p) {
		this.init(p);
	}
 
		// prop
		Y.BitsyBox.Panel.prototype = {
 
			// properties
			params : {},
			wrap : null,
			cnt : null,
			width : 954,
			height : 500,
 
			// init 
			init : function(p) {
 
				// params
				this.params = p;
 
				// render 
				if ( !p.noRender ) {
					this.render();
				}
 
				// no open
				if ( !p.noOpen ) {
					this.open();
				}
 
			},
 
			// render
			render : function() {
 
				// local
				var p = this.params;
 
				// wrap
				var wrap = document.createElement('div');
					wrap.className = 'panel-wrap';
					wrap.id = p.name + '-wrap';
 
				// inner
				wrap.innerHTML = "<div id='"+p.name+"-content' class='cnt'></div><a href='#' class='panel-close'>close</a>";
 
				// set stype
				$d.setStyle(wrap,'display','none');
 
				// append
				document.body.appendChild(wrap);
 
				// attach
				$e.on(wrap,'click',function(e){		
 
					// tar 
					var tar = $e.getTarget(e);
 
					// find
					if ( $d.hasClass(tar,'panel-close') ) {
						$e.stopEvent(e); this.close();
					}
 
				},this,true);
 
				// global
				this.wrap = wrap;
				this.cnt = $(p.name+"-content");
 
			},
 
			// close
			close : function() {
 
				// if overlay
				if ( this.params.overlay ) {
 
					// fade in
					var a = new $a('panel-overlay',{opacity:{to:0}},.2);
						a.onComplete.subscribe(function(type,args,me){
							$d.setStyle('panel-overlay','display','none');
						},this);
						a.animate();									
 
				}			
 
				// fade in
				var a = new $a(this.wrap,{opacity:{to:0}},.2);
					a.onComplete.subscribe(function(type,args,me){
						$d.setStyle(me.wrap,'display','none');
					},this);
					a.animate();									
 
			},
 
			// open
			open : function(p) {
 
				// if overlay
				if ( this.params.overlay ) {
 
					// not in
					if ( !$d.inDocument('panel-overlay') ) {
						var d = document.createElement('div');
							d.id = 'panel-overlay';
						document.getElementsByTagName('body')[0].appendChild(d);
					}
 
					// set
					$d.setStyle('panel-overlay','opacity',0);
 
					// show
					$d.setStyle('panel-overlay','display','block');
 
					// fade in
					var a = new $a('panel-overlay',{opacity:{to:.4}},.2);
						a.animate();									
 
				}
 
				// show
				$d.setStyle(this.wrap,'opacity',0);
 
				// show
				$d.setStyle(this.wrap,'display','block');		
 
				// figure where to center
				var dw = parseInt($d.getViewportWidth(),10);
				var dh = parseInt($d.getViewportHeight(),10);
 
				// center
				var pw = parseInt($d.getStyle(this.wrap,'width'),10);
				var ph = parseInt($d.getStyle(this.wrap,'height'),10);
 
					// is it too big 
					if ( pw > this.width ) {
						pw = this.width;
						$d.setStyle(this.wrap,'width',this.width+'px');
					}
 
					if ( ph > this.height ) {
						ph = this.height;
						$d.setStyle(this.wrap,'height',this.height+'px');
					}
 
				// x
				var x = (dw/2) - (pw/2);
				var y = (dh/2) - (ph/2);
 
					// opven
					if ( y < 10 ) {
						y = 10;
					}
 
 
				// place
				$d.setXY(this.wrap,[x,y]);
 
				// fade in
				var a = new $a(this.wrap,{opacity:{to:1}},.4);
					a.animate();							
 
			},
 
			// load 
			load : function(p) {
 
				// callback
				var callback = {
					'success': function(o) {
 
						// j 
						var j = false;
 
						// try
						try {
							j = $j.parse(o.responseText);
						}
						catch(e) { return; }
 
						// good
						if ( j.stat != 1 ) {
							return;
						}
 
 						// cnt
						var t = o.argument[0];
 
						// set
						o.argument[0].cnt.innerHTML = j.html;
					
						
							// check for any forms
							// and send them to postbacl
							var forms = o.argument[0].cnt.getElementsByTagName('form'); 
							
							for ( var f in forms ) {
								// if there is no target and 
								// there isn't an ignore
								if ( typeof forms[f] == 'object' && !forms[f].getAttribute('target') && !$d.hasClass(forms[f],'ignore') ) {
								
									// add the postback
									forms[f].setAttribute('target','postback');
									
									// change url to add add ctx
									forms[f].setAttribute('action', t.addParamsToUrl( forms[f].getAttribute('action'), {'.context':'xhr'} ) );
								
								}
							}				
						
						// check for body class
						if ( j.bootstrap ) {
							
							// class
							if ( j.bootstrap.c ) {
								$d.addClass(t.cnt,j.bootstrap.c);
							}
						
							// js
							if ( j.bootstrap.js ) {
								for ( var s in j.bootstrap.js ) {
									eval(j.bootstrap.js[s]);
								}
							}
						
						}
 
 						// load
 						if ( o.argument[1].onLoad ) {
 							o.argument[1].onLoad();
 						} 
 
						// if open
						if ( o.argument[1].open ) {
							o.argument[0].open();
						}
 
					},
					'argument': [this,p]
				};
 
				// add 
				var url = this.addParamsToUrl(p.url,{'.context':'xhr','.name':this.params.name});
 
				// fire
				var r = $c.asyncRequest('GET',url,callback);
 
			},
 
			// add query string to url
			addParamsToUrl : function(url,params) {
 
				var qs = [];
 
				for ( var p in params ) {
					qs.push(p+"="+params[p]);
				}
 
				// what 
				if ( url.indexOf('?') !== -1 ) {
					return url + '&' + qs.join('&');
				}
				else {
					return url + '?' + qs.join('&');
				}
 
			}			
 
		}
  
