/*
	Developer: Adam Tourkow
	Date: 2007/09/17
	Description:
		Global user specific functions.
	
	Updated --------
	4/1/2008: Adding in user login / registration popup
*/

var user_func = new function()
{
	// Name space this within our class
	var o = this;
	// A place to store an element to manipulate later or with an ajax handler
	// If using in a handler, it's kept as user_func.elmt
	o.elmt = null;

	// Does the popup for login / register	
	o.popup_unknown_user = function(sAdd)
	{
		if(user.exists)
			return false;

		if(o.get_cookie('previous_user'))
		{
			var login_form = $('#login_block');
			login_form.find('form').attr('action', '/login?'+sAdd);
			login_form = ''+ 
				'<div class="icon_sign_up"><strong class="primary giant">Sign in</strong></div>' +
				'<div class="login_block" style="font-size: 12px;">' + login_form.html() + "</div>";
			$.popup.inline(login_form, {width: 250});
		}
		else
		{
			$.popup.iframe('/register?popup=true&referer='+window.location.pathname+(window.location.search?window.location.search:'')+'&'+sAdd, {height: 550, width: 470});
		}
		return true;	
	}
	
	o.watch = function(sWatchType, iID)
	{
		o.popup_unknown_user('watch_type=' + sWatchType + '&watch_type_id='+iID);
			
	}

	// Unsubscribe from alert_answer
	o.alert_answer = function(aid, value, o_to_remove)
	{
		var page = '/json/user/alert_answer.php';
		var data =
		{
			aid: aid,
			type: value
		};
		if(o_to_remove)
		{
			o.elmt = o_to_remove;
		}
		$.getJSON(page, data, user_func.alert_answer_handler);

	}
	o.alert_answer_handler = function(data)
	{
	   if(user_func.elmt && data.complete)
	      $(user_func.elmt).html('Unsubscribe Complete');
		user_func.elmt = null;
	}
	
	o.get_cookie = function( name ) 
	{
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
			return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ';', len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	}
	
}