
	$(document).ready( function() {
			build_watchlist( interval, start_date );
	});
	
	function build_watchlist( f_interval , f_start_date, f_switch )
	{
		before_server_call();
		
		if ( f_switch == '1' && typeof f_switch == 'string' ) {
			f_switch = false;
			_click_made = true;
		}
		
		// call the server.
		server_post( { m : 'build_watchlist' , interval : f_interval , start_date : f_start_date , 'switch' : f_switch, filter : filter } , server_callback );
		
		interval = f_interval;
		start_date = f_start_date;
	}
	
	function before_server_call() {
		// show loader
		$('#forScript').empty().append(loader());
	}
	
	function server_callback( data )
	{
		if ( data['err'] ) {
			// display_error can be found in the utils.js ; for start it alerts the error text
			display_error( data['err'] );
		}else if ( data['server'] &&  data['server']['data'] ){
			// we got the html from the server. now let us build the page.
			// clearer() retunr a div with style clear:both with height as parameter. found in utils.js
			$('#forScript').empty().append( data['server']['data'] );
			$('.holderForFilter').empty().append( data['server']['filter'] );
		}
		
		// add filter to the link
		if ( filter != '' ) {
			t = document.location.href.split('#');
			l = '';
			if ( t[1] )
			{
				l = t[1];
				if ( t[1].indexOf('filter') != -1 ) {
					a = t[1].split('filter');
					l = a[0];
				}
			}
			l += 'filter/' + filter;
			document.location.href = www_root + 'watchlist#' + l;
		}
		
		after_watchlist_load();
	}
	
	function after_watchlist_load() {
		$('#switch_watchlist').click(function(){
			build_watchlist( interval, start_date, true );
			return false;
		});
		
		$('#filter').change(function() {
			// any chenge on filter is not considered a click on back button
			_click_made = true;
			t = $(this).val();
			t = t.split('/');
			
			filter = t[1];
			build_watchlist( interval, start_date );
		});
		
	}
	