/**
* @name  		Common user account system javascript functions
* @filesource 	user_funcs.js
* @author		Web Developer (Duncan Macfarlane) <duncan@pricecheck.co.za>
* @copyright	PriceCheck 2008
* @package		PriceCheck.co.za
*/
var logged_in_timestamp,product_id=0;
var user_checker = new Ajax.PeriodicalUpdater('logged_in_box',"/xml_user_functions/?function=logged_in_timestamp",
{
	frequency:60.0,
	onSuccess:function(transport)
	{	
		logged_in_timestamp=transport.responseText;
		if (transport.responseText == 0)
		{
			user_checker.stop();
		}
	}
});
// General global functions
/** Associate an item with a currently logged in user */
function add_item(item,item_id) 
{
	var url ="/xml_user_functions/?function=add_item&item="+URLEncode(item)+"&item_id="+URLEncode(item_id);
	new Ajax.Request(url,
			{
				onSuccess: function(transport)
				{
					response = transport.responseText;
					alert(response);
				},
				evalScripts:true
			});
} // add_item()
/**  Removes association with an item */
function remove_item(item,item_id) 
{
	var url ="/xml_user_functions/?function=remove_item&item="+URLEncode(item)+"&item_id="+URLEncode(item_id);
	new Ajax.Request(url,
		{
			onSuccess: function(transport)
			{
				response = transport.responseText;
				alert(response);
				url="/xml_user_functions/?function=draw_objects&tool=product&what=mine";//="+URLEncode(item_id);	
				new Ajax.Updater('productchecker_mine_holder',url,
					{
						onLoading:function()
							{
								$('productchecker_mine_holder').update('Loading...');
							},
							evalScripts:true						
					});
			},
			evalScripts:true
		});
} // remove_item()
/** Pre-check - if user logged in... */
function logged_in(func) 
{
	if (logged_in_timestamp!= 0)
	{
		eval(func);
	}
	else
	{
		new Ajax.Request('/xml_user_functions/?function=logged_in',
				{
					onComplete: function(transport)
					{
						var response=transport.responseText;
						//alert(response);
						if (response == 'OUT')
						{
							draw_user_popup(func);
							return false;
						}
						else
						{						
							eval(func);
						}
					},
					evalScripts:true,
					evalJSON:true
				});	
	}
}; // logged_in()
/** Pre-check - if user logged in... */
function logged_in_my_account(func) 
{	
	if (logged_in_timestamp!= 0)
	{
		eval(func);
	}
	else
	{
		new Ajax.Request('/xml_user_functions/?function=logged_in',
			{
				onComplete: function(transport)
				{
					var response=transport.responseText;
					if (response == 'OUT')
					{
						window.location='/my_account/';
						return false;
					}
					else
					{						
						eval(func);
					}
				},
				evalScripts:true
			});	
	}
}; // logged_in()
/** Centres the generic popup */
centre_gen_popup = function()
{
    var pos = gen_popup.get_center_position();
    $('generic_popup').setStyle({top:pos.y+'px',left:pos.x+'px'});
};
/** Draws a review popup */
rate_this = function(type,object_id)
{
    setup_gen_popup('Review this '+(type=='p'?"Product":"Shop"),454,456);
    var url ='/xml_user_functions/?function=draw_review_adder&type='+type+'&object_id='+object_id;
    generic_popup_updater(url); // added centre_gen_popup() function - called on update complete
}; // rate_this
/** Draws the user login/signup popup */
draw_user_popup = function(func) 
{	
	setup_gen_popup('PriceChecker Login',300,145);
	var url ='/xml_user_functions/?function=draw_popup_login';
	generic_popup_updater(url,null,func);	
}; // draw_user_popup
/** Closes the generic popup manually */
close_gen_popup = function() 
{
	gen_popup.hide();
}; // close_gen_popup()
/** Opens the generic popup manually */
open_gen_popup = function() 
{
	gen_popup.show();
}; // close_gen_popup()
/** Sets up title and minimum dimensions of the generic popup */
setup_gen_popup = function(new_title,new_width,new_height)
{
	var n_width=new_width+'px' || '300px';
	var n_height=new_height+'px' || 'auto';
	$('gen_popup_title').update(new_title);	
	$('gen_popup_sizer').setStyle({width:n_width});
	$('gen_popup_height_sizer').setStyle({height:n_height});
	centre_gen_popup();
};
/** Removes a notification */
remove_note = function(index) 
{
	var url = "/xml_user_functions/?function=remove_note&index="+index;
	new Ajax.Request(url,
			{
				onLoading:function()
				{
					$('rem_note_'+index).update('Removing...');
				},
				onComplete: function(transport)
				{
					response=transport.responseText;
					$('notification_'+index).hide();
					$('notification_'+index).update('');
					
					if ($('notifications_link')!=null)
					{						
						if (response=='0')
							$('notebox_count').update('');				
						else
							$('notebox_count').update('('+response+')');				
						
						if ($('my_notifications_link')!=null)
						{
							if (response!='1')
								$('my_notifications_link').update('You have '+response+' new notifications.');
							else
								$('my_notifications_link').update('You have 1 new notification.');
						}
					}					
				}// onComplete()		
			});// Ajax.Request()
}; // remove_note()
/** Dismisses a notification */
dismiss_note = function(index,object_type) 
{
	var url = "/xml_user_functions/?function=dismiss_note&index="+index
		+"&object_type="+object_type;
	new Ajax.Request(url,
			{
				onLoading:function()
				{
					$('rem_note_'+index).update('Removing...');
				},
				onComplete: function(transport)
				{
					response=transport.responseText;
					$('notification_'+index).hide();										
					$('notification_'+index).update('');					
					if ($('notifications_link')!=null)
					{				
						if (response=='0')
							$('notebox_count').update('');				
						else
							$('notebox_count').update('('+response+')');				
						
						if ($('my_notifications_link')!=null)
						{
							if (response!='1')
								$('my_notifications_link').update('You have '+response+' new notifications.');
							else
								$('my_notifications_link').update('You have 1 new notification.');
						}
					}
					if ($('notificationchecker_tool')!=null)
					{
						url="/xml_user_functions/?function=draw_objects&tool=notification&what=mine";
						checker_ajax_updater('notification','mine',url);
					}
				}// onComplete()		
			});// Ajax.Request()
}; // dismiss_note()
/** Logs user out via the popup */
function do_popup_logout(){      
  new Ajax.Request('/xml_user_functions/?function=logout_user',
		  {
	  		evalScripts:true,
	  		onLoading:function()
	  		{
	  			if ($("products_pricechecker_tools") != null)
	  			{
	  				$('products_pricechecker_tools').update('Loading...');
	  			}
	  		},
	  		onSuccess:function()
	  		{	  			
	  			logged_in_timestamp=0;
	  			user_checker.stop();
	  			set_cookie('user_toolbar','shown',9);
	  			login="&nbsp;|&nbsp;<img src=\"/images/pricechecker/new2.png\">&nbsp;<a style='color:blue;' href=\"javascript:void(0);\" onclick=\"draw_user_popup();\" id=\"user_popup_link\">Login/Signup!</a>";
				$('bread_user_login').update(login); 
				$('toolbar_toggle').update('');	  			
	  			if ($("products_pricechecker_tools") != null)
				{										
					url= "/xml_user_functions/?function=draw_pricechecker_tools&product_id="+product_id;
					new Ajax.Updater('products_pricechecker_tools',url,
							{
								onSuccess: function()
								{
									new Effect.Highlight('products_pricechecker_tools',{startcolor: '#CFE0FF',endcolor: '#ffffff' });
								},
								evalScripts:true
							}); // Ajax.Updater
				}	
	  		}		  
		  });  
  Effect.Fade('user_toolbar',
	      {
      		afterFinish :function()
      		{					
				new Ajax.Updater('user_toolbar',"/xml_user_functions/?function=draw_toolbar",
						{
						onSuccess: function() 
							{								
								Effect.Appear('user_toolbar',{duration:0.3});								
							} //onSuccess			
						}); // Ajax.Updater				
			} // afterFinish			
		   }); // Effect.Fade  
}; // do_popup_logout()
/**	Traps the Enter key */
trap_enter = function(event,func)
{
	var keynum;	
	if(window.event) // IE
	{
		keynum = event.keyCode;
	}
	else if(event.which) // Netscape/Firefox/Opera
	{
		keynum = event.which;
	}
	if (keynum==13) // Was key==enter? 
	{
		eval(func);
	}								
};// trap_enter()
set_product_id = function(new_id){product_id=new_id;};
get_product_id = function(){return product_id;};