var helperactive=false;

var HBalloon = Class.create();
var Pointer = Class.create();

var HelperFrame = Class.create();
var FrameLink = Class.create();
var Helper = Class.create();

var helper_stack=new Array();
HBalloon.zIndex = Pointer.zIndex = 1000;
HBalloon.prototype = {
	initialize: function(balloon_id,type){
		this.type=type || 'static';
		var balloon = document.createElement('div');var b_body='';
		b_body+='<div class="balloon_top_right"><div class="balloon_top_left"><div class="balloon_top"><!--  --><div class="balloon_title" id="balloon_title_'+balloon_id+'">&nbsp;</div></div></div><div class="balloon_close" id="balloon_'+balloon_id+'_close" title="Close"><!--  --></div></div>';
		b_body+='<div class="balloon_right"><div class="balloon_left"><div class="balloon_body" id="balloon_body_'+balloon_id+'">TESTING</div></div></div>';
		b_body+='<div class="balloon_bottom_right"><div class="balloon_bottom_left"><div class="balloon_bottom"><!--  --></div></div></div>';
		balloon.setAttribute('id','balloon_'+balloon_id);balloon.setAttribute('class','balloon');balloon.innerHTML=b_body;balloon.style.position='absolute';balloon.style.top='0';balloon.style.display='none';
		balloon.style.left='0';	this.balloon=balloon; this.balloon_id=balloon_id; this.balloon_body_id='balloon_body_'+this.balloon_id;
		this.balloon_title_id='balloon_title_'+this.balloon_id;	document.observe("dom:loaded",this.onload.bind(this));
	},	
	onload:function(){		
		document.body.appendChild(this.balloon); new Draggable(this.balloon);
		this.balloon_body=$('balloon_body_'+this.balloon_id); this.balloon=$(this.balloon);
		Event.observe('balloon_'+this.balloon_id+'_close','click',this.close.bindAsEventListener(this)); this.balloon_title=$('balloon_title_'+this.balloon_id); this.balloon.style.zIndex=HBalloon.zIndex++;		
	},
	show:function(event,element,title_text,body_text,width,pos,align)
	{
		align=align||'right'; pos=pos||'center'; this.set_balloon_data(title_text,body_text); this.update_balloon(event,element,width,pos,align);
		if (!this.balloon.visible()){new Effect.Appear(this.balloon,{duration:0.2});}
	},	
	update_balloon:function(event,element,width,pos,align){		
		pos=pos||'center';align=align||'right';win_dim=document.viewport.getDimensions();balloon_dim=this.balloon.cumulativeOffset();
		balloon_dim.height=this.balloon.getHeight();balloon_dim.width=width||'150';
		if (pos=='context')	{
			var el_off=$(element).cumulativeOffset();var el_dim=$(element).getDimensions();	
			if (align=='left'){balloon_dim.left=el_off.left+(el_off.left-balloon_dim.width>0?-balloon_dim.width:0);}
			else {balloon_dim.left=el_off.left+(el_off.left+el_dim.width+balloon_dim.width<win_dim.width?el_dim.width:-balloon_dim.width);}
			balloon_dim.top=el_off.top+el_dim.height-17;		
		}
		else if (pos=='cursor')	{var mouse_x = Event.pointerX(event);balloon_dim.left=(mouse_x+balloon_dim.width<win_dim.width?mouse_x:win_dim.width-balloon_dim.width);var mouse_y = Event.pointerY(event);balloon_dim.top=mouse_y;}
		else {balloon_dim.left=(win_dim.width/2)-(balloon_dim.width/2);balloon_dim.top=(win_dim.height/2)-(balloon_dim.height/2)-17;}   
		this.balloon.setStyle({left:balloon_dim.left+'px',top:balloon_dim.top+'px',width:balloon_dim.width+'px'});this.height=balloon_dim.height;this.width=balloon_dim.width;this.left=balloon_dim.left;this.top=balloon_dim.top;
	},		
	set_balloon_data:function(title_text,body_text){body_text+="<div style='position:relative;padding-bottom:15px;'><div align='center' class='sml_txt' style='position:absolute;font-size:11px;top:2px;left:0px;width:100%;padding-top:15px;'><a href='javascript:void(0);' onclick='"+this.helper_id+".hide_always();'>Don't show me helpers on this page again</a><br><a href='javascript:void(0);' onclick='"+this.helper_id+".hide_all_always();'>Don't show me ANY helpers again</a></div></div>";this.balloon_title.update(title_text);this.balloon_body.update(body_text);},
	hide_always:function(){set_cookie('hide_helper',1,90);this.helper.stop();/*new Effect.Fade(this.balloon,{duration:0.2});*/},
	hide:function(){new Effect.Fade(this.balloon,{duration:0.2});},						
	close:function(){this.helper.stop();}						
}

Pointer.prototype = {
	initialize: function(pointer_id,target_id){
		this.visible=false;
		var pointer=document.createElement('div');var p_body='';
		p_body+='<div id="pointertip_tr_'+pointer_id+'" class="pointer_tip_corners pointer_tip_tr"><div id="pointertip_tl_'+pointer_id+'" class="pointer_tip_corners pointer_tip_tl"><div class="pointer_tip_top_bottom pointer_tip_top"><!-- --></div></div></div>';
		p_body+='<div id="pointertip_r_'+pointer_id+'" class="pointer_tip_sides pointer_tip_right"><div id="pointertip_l_'+pointer_id+'" class="pointer_tip_sides pointer_tip_left"><div class="pointer_tip_body" id="pointertip_body_'+pointer_id+'">TESTING</div></div></div>';
		p_body+='<div id="pointertip_br_'+pointer_id+'" class="pointers pointer_tip_r_tip"><div id="pointertip_bl_'+pointer_id+'" class="pointer_tip_corners pointer_tip_bl"><div class="pointer_tip_top_bottom pointer_tip_bottom"><!--  --></div></div></div>';
		pointer.setAttribute('id',pointer_id); pointer.setAttribute('class','pointer');
		pointer.innerHTML=p_body;		
		pointer.style.position='absolute'; pointer.style.display='none';pointer.style.width='250px';
		pointer.style.top='0px'; pointer.style.left='0px'; this.src_id=''; this.set_target(target_id); this.pointer_id=pointer_id;
		this.point_dir='right';	this.transition=Effect.Transitions.sinoidal; this.transition_duration=0.5;//0.8
		this.pointer=pointer; this.pointer_body_id='pointertip_body_'+pointer_id; this.never_show=false;		
		document.observe("dom:loaded",this.onload.bind(this));
	},
	onload:function(){
		document.body.appendChild(this.pointer); this.pointer_body=$(this.pointer_body_id); this.pointer=$(this.pointer_id);		
		this.pointer.style.zIndex=Pointer.zIndex++;	this.redim(); this.redim_target(); this.set_point();
	},
	centre:function(){
		win_dim=document.viewport.getDimensions();
		this.gotoxy((win_dim.width/2)-(this.pointer.getWidth()/2),(win_dim.height/2)-(this.pointer.getHeight()/2),'direct');
	},
	redim_target:function()	{
		if ($(this.target_id)!=null && this.target_id!='') {
			var pos=$(this.target_id).cumulativeOffset();this.target_left=pos.left;this.target_top=pos.top;this.target_width=$(this.target_id).getWidth();
			this.target_height=$(this.target_id).getHeight();
		} else {this.goto_x=this.goto_y=0;}
	},
	set_point:function(new_dir)	{	
		this.point_dir=new_dir||this.point_dir;
		if (this.point_dir=='left')	{
			$('pointertip_tl_'+this.pointer_id).setStyle({marginLeft:'27px'});$('pointertip_tr_'+this.pointer_id).setStyle({marginRight:'0px'});			
			$('pointertip_l_'+this.pointer_id).setStyle({marginLeft:'27px'});$('pointertip_r_'+this.pointer_id).setStyle({marginRight:'0px'});	
			$('pointertip_bl_'+this.pointer_id).writeAttribute('class','pointers pointer_tip_l_tip');
			$('pointertip_br_'+this.pointer_id).writeAttribute('class','pointer_tip_corners pointer_tip_br');
			this.goto_x=this.target_left+this.target_width;this.goto_y=this.target_top-this.height;
		}
		else {
			$('pointertip_tl_'+this.pointer_id).setStyle({marginLeft:'0px'});$('pointertip_tr_'+this.pointer_id).setStyle({marginRight:'27px'});
			$('pointertip_l_'+this.pointer_id).setStyle({marginLeft:'0px'});$('pointertip_r_'+this.pointer_id).setStyle({marginRight:'27px'});
			$('pointertip_bl_'+this.pointer_id).writeAttribute('class','pointer_tip_corners pointer_tip_bl');
			$('pointertip_br_'+this.pointer_id).writeAttribute('class','pointers pointer_tip_r_tip');
			this.goto_x=this.target_left-this.width;this.goto_y=this.target_top-this.height;
		}
	},
	gotoxy:function(to_x,to_y,now) {
		if (now=='direct') {$(this.pointer_id).setStyle({left:to_x+'px',top:to_y+'px'});}
		else {
			if (now=='spring'){this.transition=Effect.Transitions.spring;} else {this.transition=Effect.Transitions.sinoidal;}
			new Effect.Move(this.pointer_id, { x: to_x, y: to_y, mode: 'absolute',duration:this.transition_duration,fps:50,transition:this.transition});				
		}
	},
	gototarget:function(new_target,now,new_dir)	{		
		this.set_target(new_target);
		this.redim();this.redim_target();
		this.set_point(new_dir);
		this.gotoxy(this.goto_x,this.goto_y,now);this.pulse_target();
	},	
	set_target:function(new_target){new_target=new_target||'';this.src_id=this.target_id;this.target_id=((new_target!='')?$$('*[id^="'+new_target+'"]')[0].id:'');},	
	redim:function(){var pos=this.pointer.cumulativeOffset();this.left=pos.left;this.top=pos.top;this.width=this.pointer.getWidth();this.height=this.pointer.getHeight();},
	show:function(){this.visible=true;new Effect.Appear(this.pointer_id,{duration:0.1});},
	pulse_target:function(){$(this.target_id).pulsate({pulses:2,duration:1.5,from:0.3,delay:0.6});},
	settext:function(new_text){this.pointer_body.innerHTML=new_text;},
	hide:function(){this.visible=false;new Effect.Fade(this.pointer,{duration:0.2});}						
}

FrameLink.prototype = {
	initialize: function(new_text,new_type,parentFrame,rootFrame) {this.text=new_text||'';this.type=new_type||'std'; if (this.type=='std'){this.frame=new HelperFrame(parentFrame,rootFrame);} else {this.frame=null;}}
}

HelperFrame.prototype = {	
	initialize: function(parentFrame,rootFrame) { this.title=''; this.idnum=(parentFrame!=null?parentFrame.idnum+1:0); this.type=''; this.body_text=''; this.links=new Array(); this.movement='direct'; this.point_dir='right'; this.target_id=''; this.prevFrame=parentFrame; this.rootFrame=(rootFrame!=null)?rootFrame:this;},
	set_frame:function(title,type,body_text,movement,point_dir,target_id){this.title=title; this.type=type; this.body_text=body_text; this.movement=movement; this.point_dir=point_dir; this.target_id=target_id; },
	add_link:function(new_text,new_type){tempframelink=new FrameLink(new_text,new_type,this,this.rootFrame); this.links.push(tempframelink); return tempframelink.frame;}
}

Helper.prototype = {	
	initialize: function(helper_id,loop)
	{		
		this.active=false;
		this.helper_id=helper_id; this.rootFrame=new HelperFrame(null,null); this.currFrame=this.rootFrame;
		this.pointer=new Pointer('helperpointer'); this.balloon=new HBalloon('helperballoon','static');		
		this.pointer.helper_id=this.balloon.helper_id=helper_id; this.pointer.helper=this.balloon.helper=this;	
		this.loop=loop || false; this.last_type='';
		this.never_show=(get_cookie('hide_helpers')?true:(get_cookie('hide_helper_'+helper_id+'')?true:false));		
	},	
	set_frame:function(title,type,body_text,movement,point_dir,target_id){this.currFrame.set_frame(title,type,(body_text),movement,point_dir,target_id);},
	cookieplay:function(){set_cookie('hide_helpers',0,-90); set_cookie('hide_helper_'+this.helper_id,0,-90);this.never_show=false;this.replay();},
	hide_always:function(){set_cookie('hide_helper_'+this.helper_id,1,90);this.stop();},
	hide_all_always:function(){set_cookie('hide_helpers',1,90);this.stop();},
	add_link:function(new_text,new_type){tempframe=this.currFrame.add_link(new_text,new_type); if (tempframe!=null){this.currFrame=tempframe;}},
	back_track:function() {if (this.currFrame!=null && this.currFrame.prevFrame!=null){this.currFrame=this.currFrame.prevFrame;}},
	rewind:function() {this.currFrame=this.rootFrame;},
	followLink:function(link_index) {link_index=link_index||0; this.currFrame=this.currFrame.links[link_index].frame; this.play();},	
	previous:function(){if (this.currFrame!=null && this.currFrame!=this.rootFrame){this.currFrame=this.currFrame.prevFrame;this.play();}},
	reset:function(){this.currFrame=this.rootFrame;},
	replay:function(){this.rewind();this.play();},
	stop:function(){this.pause();this.reset();this.active=false;sleepy_product_count=0;set_cookie('sleepy_product_count',0);helperactive=false;sleepy_count=0;set_cookie('sleepy_count',0);},
	pause:function(){this.balloon.hide();this.pointer.hide();},
	play:function() {		
		if (this.currFrame!=null && !this.never_show){
			sleepy_product_count=0;set_cookie('sleepy_product_count',0);
			sleepy_count=0;set_cookie('sleepy_count',0);			
			helperactive=true;
			this.active=true;
			helper_id=this.helper_id;			
			var bodyText="<div>"+this.currFrame.body_text+"</div>";
			bodyText+='<div id="helper_links" style="padding-top:5px;">';
			helper_loops=this.loop;linkcount=this.currFrame.links.length;
			this.currFrame.links.each(function(link,link_index){
					linkurl='';if (link.type=='exit'){linkurl=helper_id+'.stop();';}
					else if (link.type=='prev'){linkurl=helper_id+'.previous();';}
					else if (link.type=='std'){linkurl=helper_id+'.followLink('+link_index+');';}
					specialformat='';if (linkcount==1 && link.type!='prev'){specialformat='style="float:right;"';}
					bodyText+='<a '+specialformat+'href="javascript:void(0);" onclick="'+linkurl+'">'+link.text+'</a>';
					if (helper_loops && link.type=='exit'){bodyText+='<span style="float:right;"><a href="javascript:void(0);" onclick="'+helper_id+'.replay();">Repeat?</a>&nbsp;|&nbsp;</span>';}
					bodyText+='<br>';
				});			
			bodyText+='<div class="clear"></div></div>';
			if ((this.last_type=='balloon' && this.currFrame.type=='pointer')){this.pointer.centre();this.balloon.hide();}
			if (this.last_type=='pointer' && this.currFrame.type=='balloon'){this.pointer.hide();}
			switch(this.currFrame.type) {
				case 'pointer':this.pointer.settext(bodyText); this.pointer.show(); this.pointer.gototarget(this.currFrame.target_id,this.currFrame.movement,this.currFrame.point_direction); this.last_type='pointer'; break;
				case 'balloon':this.balloon.show(null,this.currFrame.target_id,this.currFrame.title,bodyText,350,'centre'); this.last_type='balloon'; break;
				default: this.last_type='';	break;
			}			
		}
	}
}