/* for IE 6 & 7 browsers */
var is_iframe_created = true;
/* for locking on history adding stuff */
var is_history_allow_add = true;
/* old window location hash used in the case of Browsers other than IE */
var expectedHash = "";
/* for IE 6 on locking IFrame related things */
var is_lock_on_frame = false;
/* for handling the bookark history */
var is_bookmark_history_item = true;
/* for handling many widget in address bar in case of like landing page */
var is_single_widget_address_bar = false;
/* iframe name */
var ajax_back_iframe_name = 'ajaxnav';
/* widnow load object -- Its a lock for all methods that are being called after some time. Once window load done we make this
variable true. */
var is_window_load = false;
/* we set it true when we have IE */
var iframesfix = false;
/* starting page history */
var start_page_history = '';
/* for handling back button issue in which we have to press two times for going to original page. */
var is_history_started = false;
/*old value of y/vertical axis scrollbar position for dont write cookies again*/
var old_vertical_position = -1;
/*for handling caching problem*/
var always_refresh = false;
/* for handling other callbacks on load page */
var another_callback = null;
/* for handling back button stuff in IE6,IE7 */
var is_null  = false;
var is_null_value ='';
/* we use this flag for selecting the filter in case if window.hash is empty. 
   We will only use this flag at landing page	
*/
var select_filters_only = false;


/* scroll once */
var scroll_done = false;

/* structure for maintaining the widgets exist in address bar when is_single_widget_address_bar is selected as false */
function ajax_state()
{
	this.widget_type = '';  /* widgert / tab */
	this.name = ''; /* name of the widget */
	this.page_no = 1;/* page no */
	this.filter_name = ''; /* filter name */
	this.tab_group = -1; /* tab group will only used in the case of tabs as on event page there are two page group */
}

function widget_info()
{
	this.page_no = 1;
	this.filter_name = 1;
	this.widget_name = '';
}

/* widget_list will contains ajax_state stuff */
var widget_list = new Array();


/* Stuff related to History */
	/* timer algorithm */
	function MyTimer(browser_type){
		this.obj = (arguments.length)?arguments[0]:document.window;	
		return this;
	}

	// The set functions should be called with:
	// - The name of the object method (as a string) (required)
	// - The millisecond delay (required)
	// - Any number of extra arguments, which will all be
	// passed to the method when it is evaluated.

	MyTimer.prototype.setInterval = function(func, msec){
		var i = MyTimer.getNew();
		var t = MyTimer.buildCall(this.obj, i, arguments);
		MyTimer.set[i].timer = window.setInterval(t,msec);
		return i;
	}

	MyTimer.prototype.setTimeout = function(func, msec){
		var i = MyTimer.getNew();
		MyTimer.buildCall(this.obj, i, arguments);
		MyTimer.set[i].timer = window.setTimeout("MyTimer.callOnce("+i+");",msec);
		return i;
	}
	

	// The clear functions should be called with

	// the return value from the equivalent set function.

	MyTimer.prototype.clearInterval = function(i){
		if(!MyTimer.set[i]) return;
		window.clearInterval(MyTimer.set[i].timer);
		MyTimer.set[i] = null;
	}

	MyTimer.prototype.clearTimeout = function(i){
		if(!MyTimer.set[i]) return;
		window.clearTimeout(MyTimer.set[i].timer);
		MyTimer.set[i] = null;
	}

	

	// Private data

	MyTimer.set = new Array();
	MyTimer.buildCall = function(obj, i, args){
		var t = "";
		MyTimer.set[i] = new Array();
		if(obj != window){
			MyTimer.set[i].obj = obj;
			t = "MyTimer.set["+i+"].obj.";
		}
		t += args[0]+"(";
		if(args.length > 2){
			MyTimer.set[i][0] = args[2];
			t += "MyTimer.set["+i+"][0]";
			for(var j=1; (j+2)<args.length; j++){
				MyTimer.set[i][j] = args[j+2];
				t += ", MyTimer.set["+i+"]["+j+"]";
			}
		}
		t += ");";
		MyTimer.set[i].call = t;
		return t;
	}
	MyTimer.callOnce = function(i){
		if(!MyTimer.set[i]) return;
		eval(MyTimer.set[i].call);
		MyTimer.set[i] = null;
	}
	MyTimer.getNew = function(){
		var i = 0;
		while(MyTimer.set[i]) i++;
		return i;
	}
/* end of time stuff.*/
	function add_selected_tab_history(tab_names)
	{		
		//alert(tab_names);
		if(typeof(tab_names) != 'undefined')
		{
			start_page_history = tab_names;
			tab_names = '';
		}
		if(tab_names != '')
		{
			
			//alert('we are restoring history');
			var window_hash = window.location.hash;
			//alert('window_hash :: ' + window_hash);
			if( window_hash != "#" && window_hash != "" )
			{
				/*
					deserializing(building global widget_list) the default tab_names
				*/
				always_refresh = true;
				deserialize_window_hash(tab_names);	
				/*
					synchronizing the item that is on address bar with the items that we set by default on page load.
				*/
				synchronize_main_widget_list(window_hash,'');
				//alert('after synchronizing');
				//synchronize_window_hash();
				restore_history();
				set_window_hash(serialize_widget_list());
				always_refresh = false;
			}			
			else
			{
				deserialize_window_hash(tab_names);
				set_window_hash(serialize_widget_list());				
				//restore_history();
				//add_browser_history(tab_names);
			}
		}else
		{
			/* here we check the filter either they are selected correctly or not, other case we will select filters explicitly */
			if(select_filters_only)
			{
				try
				{
					var temp_widgets = new Array();
					// alert(start_page_history);
					temp_widgets = deserialize_window_hash(start_page_history, '');
					// alert('Array length' + temp_widgets.length);
					restore_history(temp_widgets);
				}catch(ex)
				{
					//alert(ex.message);
                    log_exceptions(" Method: - add_selected_tab_history " + ex.message);
				}				
			}
		}
		
		
	}
	function add_tab_history(tab_names,group_id)
	{
		if(tab_names != '')
		{
			add_browser_history("tab=" + tab_names + "&group_id=" + group_id);
		}		
	}
	function add_widget_history(widget_name,pageno,filter)
	{
		add_browser_history("widget=" + widget_name + "&page=" + pageno + "&filter=" + filter);
	}
	
	function add_browser_history(history_item)
	{
		if( is_history_allow_add == false )
		{
			return;
		}
		is_lock_on_frame = true;
		try
		{						
			add_history(history_item);			
			//is_history_allow_add = true;
		}catch(ex)
		{
			//alert('exception occured ' + ex.message);
            log_exceptions(" Method: - add_browser_history " + ex.message);
		}
		is_lock_on_frame = false;
	}
	
	
	/*
		this function is used to create the main widget_list by adding new item
	*/
	function synchronize_widget_list(new_widget_info)
	{
		var my_new_widget_list = new Array();
		var widget_found,tab_found;
		var widget_index=0;
		for(widget_index=0; widget_index<widget_list.length; widget_index++)
		{
			widget_found = (widget_list[widget_index].name==new_widget_info.name && widget_list[widget_index].widget_type=='widget');
			tab_found = (widget_list[widget_index].tab_group==new_widget_info.tab_group && widget_list[widget_index].widget_type==new_widget_info.widget_type && new_widget_info.widget_type=='tab');

			if(!(widget_found ||  tab_found))
			{
				my_new_widget_list.push(widget_list[widget_index]);
			}
		}
		my_new_widget_list.push(new_widget_info);
		return my_new_widget_list;
	}
	
	/*
		this function is used to update main widget_list with new items
	*/
	function synchronize_main_widget_list(history_item,is_start_history)
	{
		var new_list = deserialize_window_hash(history_item,'')
		var temp_list = null;
		if(typeof(is_start_history) != 'undefined')		
		{
			for(history_index=0;history_index<new_list.length; history_index++)
			{
				temp_list = synchronize_widget_list(new_list[history_index]);
			}
		}
		else
		{				
			var new_widget_item = read_widget_info(history_item);
			temp_list = synchronize_widget_list(new_widget_item);
		}				
		/*
			updating the main widget_list
		*/
		widget_list.length = 0;
		widget_list = temp_list;
	}
	
	function add_history(history_item)
	{		
		if(!is_single_widget_address_bar)/* multiple addresses can come into address bar */
		{			
			//var found_widget_update = false;
			if(start_page_history != '')
			{
				var window_hash = window.location.hash;
				if( !(window_hash!="#" && window_hash!=""))
				{
					deserialize_window_hash(start_page_history);	
				}				
				set_window_hash(serialize_widget_list());				
				synchronize_main_widget_list(history_item,'');
				start_page_history  = '';				
			}else
			{
				synchronize_main_widget_list(history_item);
			}
			//serialize the widget & tab information and then assign it to window's location hash.
			history_item = serialize_widget_list();
		}		
		set_window_hash(history_item);
	}	
	/* this method is used to get the frame document. We use this for getting history item from it */
	function get_iframe_document(frame_name)
	{
		var _historyFrame = document.getElementById(frame_name)
		var frameDoc = null;
		if(_historyFrame != null)
		{
			frameDoc = _historyFrame.contentWindow.document;			
		}	
		return frameDoc;
	}

	
	/* this method used to set ajax actions at address bar */	
	function set_window_hash(history_item)
	{		
		is_history_started = true;
		window.location.hash = history_item;
		expectedHash = "#" + history_item;
		if(iframesfix)
		{
			//my_iframe_url ='http://localhost:3000/?hash=' + history_item;
			//document.getElementById(ajax_back_iframe_name).setAttribute('src',my_iframe_url);			
			_historyFrame = document.getElementById(ajax_back_iframe_name)
			var frameDoc = _historyFrame.contentWindow.document;
			var frameDoc = get_iframe_document(ajax_back_iframe_name);
			if(frameDoc != null)
			{
				frameDoc.open("javascript:'<html></html>'");
				//<title>" + (document.title) + "</title>
				frameDoc.write("<html><head><scri" + "pt type=\"text/javascript\"></scri" + "pt></head><body><input id='hash_value' name='hash_value' value='" + history_item + "'></input></body></html>");
				//alert('value of the added history ::: ' + frameDoc.getElementById('hash_value').value);
				frameDoc.close();
			}
		}
	}
	
	/* AjaxIframesFixer */
	function AjaxIframesFixer(iframeid) 
	{
		try
		{
			this.iframeid = iframeid;
			if (document.getElementById(ajax_back_iframe_name))
			{
				//this.fixLinks();					
				//this.locator = new PageLocator("document.frames['"+this.iframeid+"'].location", "?hash=");
				//this.windowlocator = new PageLocator("window.location.href", "#");
				this.timer = new MyTimer(this);
				this.timer.setTimeout("checkWhetherChanged", 50, "");
			}
		}catch(ex)
		{
			//alert(ex.message);
            log_exceptions(" Method: - AjaxIframesFixer " + ex.message);
		}
	}
	AjaxIframesFixer.prototype.checkWhetherChanged = function(location)
	{		
		try
		{
			if( is_lock_on_frame == false && is_window_load==true)
			{				
				//alert('now we are changing browser history');
				/* change this for getting input element value from the frame document */
				//history_item_value = this.locator.getHash();
				history_item_value = '';				
				/*
					getting iframe value for getting difference b/w address bar hash-value and stored value in iframe
				*/
				my_iframe_doc = get_iframe_document(ajax_back_iframe_name);/* getting iframe document */
				my_hash_box = my_iframe_doc.getElementById('hash_value');/* getting value of hash item */
				
				window_hash = window.location.hash
				if(window_hash.indexOf('#')>-1)
				{
					window_hash = window_hash.substr(1,window_hash.length);
					
				}
				
				if( my_hash_box != null )
				{
					history_item_value = my_hash_box.value;
					
					if(is_null== true && is_null_value!='' && is_null_value==history_item_value)
					{
						history.forward();
						is_null  = false;
						is_null_value = '';
	
					}

					/*if(window_hash == history_item_value)
					{
						history.forward();
						my_iframe_doc = get_iframe_document(ajax_back_iframe_name);
						my_hash_box = my_iframe_doc.getElementById('hash_value');
						history_item_value = ''
						if( my_hash_box != null )
						{
							history_item_value = my_hash_box.value;
						}
					}*/
				}
				else
				{
					is_null  = true;
					is_null_value = window_hash;
					go_back_once_more();
				}			
				
				
				
				//alert('history item :: ' + history_item_value + " window_hash :: " + window_hash);
				if( history_item_value != window_hash && history_item_value!='' && is_bookmark_history_item==false )
				{/* this stuff will run when we click press back button in IE browser */
					
					/* first clear the widget_list array by setting its length propertly to zero */
					/* assign hash to window location */
					/* deserialize hash */					
					widget_list.length = 0;
					window.location.hash = history_item_value;
					deserialize_window_hash(window.location.hash);
					restore_history();
					is_history_started = true;
				}
				else if( history_item_value=="" && window_hash!="" && is_bookmark_history_item==true )
				{/* this case is for handling of bookmarks stuff */
					/* for restoring history. */
					always_refresh = true;
					deserialize_window_hash(window.location.hash);
					restore_history();
					always_refresh = false;
				}
				else if( history_item_value==window_hash && history_item_value!='' && window_hash!='' && is_bookmark_history_item==true )
				{/* this case is for handling caching stuff at event/people page  as iframe was also cached that we are using for IE */	
					always_refresh = true;
					deserialize_window_hash(window.location.hash);
					restore_history();
					always_refresh = false;
				}
				is_bookmark_history_item = false;
			}			
		}catch(ex)
		{
                    //alert("exception checkWhetherChanged :: " +  ex.message);
                    // commenting out due to Ticket #4425
                    //log_exceptions(" Method: - AjaxIframesFixer.prototype.checkWhetherChanged " + ex.message);
		}
		this.timer.setTimeout("checkWhetherChanged", 500, "");
	}
	/* END AjaxIframesFixer */

	/* for fixing two click back button for going on page.*/
	function go_back_once_more()
	{
		//alert('going back to once more');
		if(is_history_started)
		{
			history.go(-1);
			is_history_started = false;
		}
	}

	/*
		create IFrame when browser is IE(6 | 7)
	*/
	function FixBackAndBookmarking()
	{
		try
		{
			if(!document.getElementById || !document.getElementsByTagName) return;
			if(iframesfix)
			{
				fix = new AjaxIframesFixer(ajax_back_iframe_name);
			}
		}catch(ex)
		{
			log_exceptions(' Method:- FixBackAndBookmarking ' + ex.message);
		}
	}

	/*
		main function that we use for setting history managing stuff
	*/
	function initialize_history_manager()
	{
		try
		{		
			var detect = navigator.userAgent.toLowerCase();
			var timer = new MyTimer(this);
			
			if(detect.indexOf("msie")>-1)
			{
				iframesfix = true;
				timer.setTimeout("FixBackAndBookmarking", 500, "");
			}else /* FF & Safari */
			{
				var bookmark_fix = new AjaxBookmarkFix();
			}
		}catch(ex)
		{
			//alert('initialize_history_manager ' + ex.message);
            log_exceptions(' Method:- initialize_history_manager ' + ex.message);
		}
	}	

	/*
		click tab and open its first page if its current page is 2
	*/
	function select_tab_navigate(selected_tab)
	{
		try
		{
			var tab_to_click = $('a_' + selected_tab);
			if( tab_to_click != null )
			{
				tab_to_click.onclick();
			}
			
			page_no = get_widget_selected_page(selected_tab)
			//alert('select_tab_navigate :: ' + page_no);
			log_exceptions("select tab navigate page ::: " + page_no,true);
			if((page_no>=2 && page_no<=10)&& check_widget_entry(selected_tab) == false )
			{	
				opened_page_no = $('pagination-'+selected_tab).getElementsByClassName('current');
				page_to_open_li= opened_page_no[0].previous(page_no-2);// getting previous li element
				anchor_to_click = page_to_open_li.getElementsBySelector('a');//gettign anchor element enclosed in li
				//to_click.readAttribute('href')
				anchor_to_click[0].onclick();//click it to open request page.
			}		
		}catch(ex)
		{
			log_exceptions(" Method :- select_tab_navigate :: " + ex.message);
		}
	}
	
	/* get  selected pageno of widget */
	function get_widget_selected_page(widget_name)
	{
		try
		{
			if( $('pagination-' + widget_name) != null )
			{	
				//alert('pagination-' + widget_name);
				/* loading first page of the widget if there is pagination and page no 2 */
				opened_page_no = $('pagination-'+widget_name).getElementsByClassName('current');
				if(opened_page_no != null && opened_page_no[0]!=null)
				{
					page_no = opened_page_no[0].innerHTML.strip();
					page_no = parseInt(page_no);
					return page_no;
				}
			}
		}catch(ex)
		{
			//alert(ex.message);
            log_exceptions("Method :- get_widget_selected_page " + ex.message);
		}
		return 1;/* all other cases return 1 */
	}
	
	/*
		getting html element of filter relevant to widget name and now we have 
		to check two times as there can be filter with anchor tag or with select tag.
		
	*/
	function get_filter_element_reference(widget_name,filter_name)	
	{
		var elem_selected_filter = $(widget_name + '_' + filter_name);		
		if(elem_selected_filter==null)
		{
			elem_selected_filter = $(widget_name + '_filter');
		}
		return elem_selected_filter;
	}
	/*
		this method is used to  get selected filter of widgets. We use this method in 'get_widget_state' method 
		as we want to know that what is pageno and selected filter of the widget.		
	*/
	function get_widget_selected_filter(widget_name,filter_name)
	{		
		var selected_filter = 'popular';
		var elem_selected_filter = get_filter_element_reference(widget_name,filter_name);
		var filter_tag_name = '';
		
		if( elem_selected_filter != null )
		{			
			/*
				now there can come two types of the filter.
				of Anchor type as on event page.  
				of Select type as on landing page.
			*/
			filter_tag_name = elem_selected_filter.tagName;
			if(filter_tag_name == 'A')
			{
				my_parent_element = $(widget_name + '_' + filter_name).up(1);
				filter_anchores = my_parent_element.getElementsByClassName('filter');
				for(filter_index=0; filter_index<filter_anchores.length; filter_index++)
				{
					if(filter_anchores[filter_index].visible() == false)
					{
						selected_filter = filter_anchores[filter_index].id;
						break;
					}
				}
			}else if(filter_tag_name == 'SELECT')
			{
				//if(elem_selected_filter.selectedIndex != -1)
				//{
					//alert(elem_selected_filter.options.selected);
					//alert(elem_selected_filter.options.selected);
					selected_filter = elem_selected_filter.options[elem_selected_filter.selectedIndex].value;
					//alert(selected_filter);
					//alert('SELECT :: ' + selected_filter);
					//alert('filter_tag_name.tagName :: ' + selected_filter);
				//}								
			}		
			
			if( selected_filter != '' && filter_tag_name != 'SELECT' )
			{
				//console.log(selected_filter);
				return get_splited_item(selected_filter, "_",1)	
			}						
		}
		return selected_filter;
	}
	
	/* this method used to get widget current state, we use this method before restoring the state 
	  of widgets as it might be the case  widget has same state that we are going to restore. */
	function get_widget_state(widget,filter)
	{		
		var current_widget_state = new widget_info();
		current_widget_state.page_no = get_widget_selected_page(widget);
		//getting select widget
		current_widget_state.filter_name = get_widget_selected_filter(widget,filter);
		current_widget_state.widget_name = widget;
		return current_widget_state;
		
	}
	
	function change_dropdown_filter(filter_element,filter_name)
	{
		var filter_index = 0;
		for(filter_index=0; filter_index<filter_element.options.length; filter_index++)
		{		
			if( filter_element.options[filter_index].value == filter_name)
			{
				filter_element.selectedIndex = filter_index;
				break;
				//mark this this index as the selected index.
			}
		}		
	}
	/* select filter without sending request as some time we dont need to send request */
	function select_widget_filter(filter_name,widget_name)
	{
		try
		{
			if( filter_name == 'recent' )
			{
			   change_filter(oRequestHandlers[0], widget_name, 'all', 'all', 2);
			}
			else if( filter_name == 'popular' )
			{
				change_filter(oRequestHandlers[0], widget_name, 'all', 'all', 1);
			}
			else if(widget_name=='stories')
			{
				if(filter_name.indexOf("_") > -1)
				{
					filter_name = get_splited_item(filter_name, "_",0);
				}							
				new_change_filter(['all','city','country','region','world'],filter_name,'stories');
			}				
		}catch(ex)
		{
            log_exceptions("Method :- select_widget_filter " + ex.message);
		}
	}
	
	/* restore single history items */
	function restore_single_history_item(widget_list_item,is_request_go)
	{		
		/* in case of request 'undefined' we will send request and in other case we will not send request. */
		if(typeof(is_request_go) == 'undefined')
		{
			is_request_go = true;
		}
		else
		{
			is_request_go = false;
		}
		
		if(widget_list_item.widget_type == 'tab' && is_request_go==true)
		{
			log_exceptions('restoring tab history :::: ' + widget_list_item.name,true);
			select_tab_navigate(widget_list_item.name);
		}else if(widget_list_item.widget_type == 'widget')
		{
			log_exceptions('restoring widget history :::: ' + widget_list_item.name,true);
			/*
				getting widget state for identifying the need to update widget paging/widget.
			*/
			var my_widget_state = get_widget_state(widget_list_item.name,widget_list_item.filter_name);
			//console.log(my_widget_state.widget_name + ' ' + my_widget_state.filter_name + ' ' +  my_widget_state.page_no);
			var mywidget_name = widget_list_item.name;
			
			var pageno = widget_list_item.page_no;
			var filter = widget_list_item.filter_name;
			//alert(my_widget_state.filter_name + " " + filter);
			//alert('widget_list_item.filter_name :: ' + widget_list_item.filter_name);
			//alert('my_widget_state.filter_name :: ' + my_widget_state.filter_name);
			if(my_widget_state.page_no == pageno && my_widget_state.filter_name==filter  && always_refresh == false)
			{
				/* there is no need to send request as all settings of widgets are pageno and filter are same */
				//alert('no need to send request');
				return ;
			}
			
			/*
			for tracing purpose 
			else
			{
				alert('widget page no :: ' + my_widget_state.page_no + ' '  + pageno + ' filter_name :: ' + my_widget_state.filter_name + ' '  + filter);
			}*/
			
			
			//add_widget_history("images",3,"popular");
			/*
				selecting filter.
			*/
			if($('a_' + mywidget_name)!=null && is_request_go==true)
			{
				$('a_' + mywidget_name).onclick();
			}
			
			/* handling the filter clicking stuff */
			var my_filter_link = get_filter_element_reference(mywidget_name,filter);
			/*
				if(my_filter_link == null)
				{
					my_filter_link = $(mywidget_name + '_filter');
				}
			*/
			var filter_tag_name = '';
			if( my_filter_link !=null )
			{
				filter_tag_name = my_filter_link.tagName;
			}
			if(pageno == 1)
			{   		
				
				if( my_filter_link != null )
				{
					if(filter_tag_name == 'A' && is_request_go==true)
					{
						my_filter_link.onclick();		
						return;
					}
					else if(filter_tag_name == 'SELECT' )
					{						
						//change select drop down 
						change_dropdown_filter(my_filter_link,filter);
					}					
				}
				//is_history_allow_add = true;
				//break;
			}
			else if( my_widget_state.filter_name != filter)
			{
				if( filter_tag_name == 'A'  && is_request_go==true)
				{
					select_widget_filter(filter,mywidget_name);
				}else if( filter_tag_name == 'SELECT' )
				{					
					change_dropdown_filter(my_filter_link,filter);
					//return;
				}
			}			

			/* when we dont need to send request we just simply return from this function */
			if(is_request_go == false)
			{
				return false;
			}
			
			
			var filter_value = filter;
			var category_value = "all";
			var list_type = '';
						
			var old_filter_values = readCookie("type_category_filter").split(",");
			if(old_filter_values.length == 4)
			{
				list_type = old_filter_values[0];
				category_value = old_filter_values[1];
			}	
			oRequestHandlers[0].setParameters(pageno, list_type, category_value, filter);
			oRequestHandlers[0].update(mywidget_name, "bottom");
			//alert(filter);
		}
	}
	
	function check_widget_entry(tab_name)
	{
		var entry_found = false;
		var widget_index=0;
		for(widget_index=0; widget_index<widget_list.length; widget_index++)
		{
			if(widget_list[widget_index].widget_type == 'widget' && widget_list[widget_index].name ==tab_name )
			{
				entry_found = true;
				break;
			}
		}
		return entry_found;
	}
	
	/*
		restore history from both iFrame and session activities 
	*/
	function restore_history(customize_array)
	{
		//alert('going to restart history.');
		//return;
		var array_to_iterate;
		var is_customize_array = false;
		if(typeof(customize_array)=='undefined')
		{
			array_to_iterate = widget_list;
		}
		else
		{
			array_to_iterate = customize_array;
			is_customize_array = true;
		}
		
		is_history_allow_add = false;
		var widget_index=0;
		/* restoring widget & widget in fifo fashion */
		for(widget_index=0; widget_index<array_to_iterate.length; widget_index++)
		{
			try
			{
				/*
				  In case of is_customize_array true we will just change the filter and not send the request.
				  We are only sending two parameters in this case because at some places this fucntion is being called with just one parameter and
				  we dont want to break those things.
				*/
				if(is_customize_array)
				{
					restore_single_history_item(array_to_iterate[widget_index],is_customize_array);
				}
				else
				{
					restore_single_history_item(array_to_iterate[widget_index]);
				}
			}catch(ex)
			{
				log_exceptions(" Method: - restore_history " + ex.message);
			}
		}		
		/*restoring tabs 
		for(widget_index=0; widget_index<widget_list.length; widget_index++)
		{
			if(widget_list[widget_index].widget_type == 'tab')
			{
				restore_single_history_item(widget_list[widget_index]);	
			}			
		}
		*/
		/*
			we are using onclicks of tabs for selecting tabs and at onlclick there is also code for adding history
		 	but when we are restorign history we set is_history_allow_add=false and in adding history function we only
			add history when is_history_allow_back==true.
		 */
		 is_history_allow_add = true; 
		 return;
	}

	/*Ajax History stuff for the firefox and safari.*/
	function AjaxBookmarkFix() 
	{
		this.timer = new MyTimer(this);
		if(window.location.hash != '')
		{
		   	always_refresh = true;
		}
		this.timer.setTimeout("handleHistory", 100);
	}				
	AjaxBookmarkFix.prototype.handleHistory =  function()
	{	 
		 //console.log("handleHistory  window hash :: " + window.location.hash + " :: expected hash : " + expectedHash);
		 try
		 {
			
			if ( window.location.hash != expectedHash && is_lock_on_frame == false && is_window_load==true)
			{
				expectedHash = window.location.hash;
				/*clearing the previous widgets list */
				widget_list.length = 0;
				/* this function deserialize the window's location hash into ajax_state_list */
				deserialize_window_hash(window.location.hash);
				/* restoring all widgets  & tabs information */
				restore_history();
				always_refresh = false;
				/* for case when we are only going into back direction on different pages */
				is_history_started = true;
			}			
			if(window.location.hash == '')
			{
				go_back_once_more();
			}
		 }catch(ex)
		 {
			//alert(ex.message);
            log_exceptions(" Method: - AjaxBookmarkFix.prototype.handleHistory " + ex.message);
		 }
		 //alert("handle history");
		 this.timer.setTimeout("handleHistory", 700);
	}
/*
	start history manager.This method initiate the whole history stuff for firefox and ie browsers
	-For IE we have to use the IFrame(Its due to history building mechanism)
	-For firfox,safri there is no need of Iframe because there history building mechanism is different than IE.	
*/
function start_history_manager(history_item)
{	
	try
	{
		initialize_history_manager();
		//another_callback = null;
		/* function initFlash() of masala_flash.js is also using the window onload function so
		we have to store it in another_callback for calling more stuff at window load method */
		if( typeof(window.onload) != 'undefined')
		{			
			another_callback = window.onload;
			//alert('callback is here  ' + another_callback);
		}
		//if there are some selected items on the page then show it in progress bar.
		if(typeof(history_item) != 'undefined' && history_item !='')
		{
			//alert('history manager');
			window.onload = function() 
			{									
				//alert('has some history :: '+typeof(loading_map));
				//alert(another_callback);
				if( another_callback!= null && typeof(loading_map)!='undefined' && $('map')!= null )
				{								
					check_flash_movie_loaded(history_item);
					//another_callback();
				}
				else if(another_callback!= null && (typeof(loading_map)=='undefined' || $('map')==null))
				{													
					add_selected_tab_history(history_item);
					///alert('after add_selected_tab_history method');
					another_callback();
					is_window_load = true;
				}
				else
				{				
					//alert('callback null ' + history_item);
					add_selected_tab_history(history_item);
					is_window_load = true;	
				}
				/*if(typeof(loading_map) != 'undefined')
				{
					check_flash_movie_loaded(history_item);	
				}
				else
				{
					add_selected_tab_history(history_item);
					is_window_load = true;
				}*/
				is_page_completely_loaded = true; /* for getting page status - loaded/not loaded */
			}
		}
		else
		{
			window.onload = function() 
			{
				
				//alert(typeof(loading_map));
				if( another_callback!= null  && typeof(loading_map)!='undefined' && $('map')!= null)
				{				
					//scrollTo(0,0);	
					check_flash_movie_loaded();
					//another_callback();
				}
				else if(another_callback != null  && (typeof(loading_map)== 'undefined' || $('map')==null ))
				{
					another_callback();
					is_window_load = true;
				}
				else
				{
					is_window_load = true;
				}
				/*if(typeof(loading_map) != 'undefined')
				{
					check_flash_movie_loaded();	
				}
				else
				{
					add_selected_tab_history();
					is_window_load = true;
				}*/
				is_page_completely_loaded = true; /*for getting page status - loaded/not loaded*/
			}		
		}
		//alert('start_history_manager');
	}catch(ex)
	{
		//alert(ex.message);
        log_exceptions(" Method: - start_history_manager " + ex.message);
	}
}

function redirection_start_history_manager()
{
	
	//get required variables and start work on it.
	if(typeof(is_start_history) != 'undefined')
	{
		if(typeof(my_page_history) == 'undefined')
		{
			start_history_manager();
		}
		else
		{
			start_history_manager(my_page_history);
		}
	}
}
function check_flash_movie_loaded(history_item)
{
	if( getFlashMovie('masala_swf') != null && typeof(getFlashMovie('masala_swf').reloadFlash) != 'undefined' )
	{
		//alert('now movie is loaded');
		/* run the specific code */
		if( typeof(history_item) != 'undefined' && history_item != '')
		{
			add_selected_tab_history(history_item);
		}
		is_window_load = true;
		another_callback();
	}
	else
	{
		if(scroll_done == false)
		{
			scroll_done = true;
			scrollTo(0,1);
		}	

		/* run this method after some time */
		if( typeof(history_item) != 'undefined' && history_item != '')
		{
			setTimeout("check_flash_movie_loaded('"+ history_item + "')",140);
		}else
		{
			setTimeout("check_flash_movie_loaded()",140);
		}
	}
}

/* 
	this function deserialize the window's location hash into ajax_state_list 
	and it will return the new widget_list if we provide something in the return_array.
	By default it will update the main widget_list.
*/
function deserialize_window_hash(window_hash,return_array)
{
	/*  
		example of window_hash
		widget=events&page=6&filter=recent,widget=contributions&page=4&filter=recent
	*/
	var ouput_array = false;
	var temp_widget_list = null;
	if( typeof(return_array) != 'undefined' )
	{
		ouput_array = true;
		temp_widget_list = new Array();		
	}	
	
	if(window_hash.length <= 1)
		return;	
	/* separating all widget/tabs */	
	ajax_state_list = window_hash.split(',');
	/* deserialize all provided widgets into widget_list array */
	for ( j=0; j<ajax_state_list.length; j++ )
	{
		var my_new_widget = read_widget_info(ajax_state_list[j]);
		if( ouput_array )
		{
			temp_widget_list.push(my_new_widget);
		}
		else
		{
			widget_list.push(my_new_widget);
		}
	}
	/*
		returning the newly builded widgets list without doing any change in the main widget_list.
	*/
	if( ouput_array ) 
	{
		return temp_widget_list;
	}
	//alert("deserialize_window_hash :: " + this.ajax_state_list.length);
}

/* this function is used to serialize the widgets_list for writting it on window'location hash */
function serialize_widget_list()
{
	var str_widget_info = '';
	for ( i=0; i<widget_list.length; i++ )
	{
		var my_widget = widget_list[i];
		if(str_widget_info != '' )
		{
			str_widget_info += ',';
		}
		
		if(my_widget.widget_type == 'widget')
		{
			str_widget_info +=	'widget=' + my_widget.name + '&page=' + my_widget.page_no + '&filter=' + my_widget.filter_name;
		}
		else
		{
			str_widget_info +=	'tab=' + my_widget.name + '&group=' + my_widget.tab_group;
		}
	}
	//alert(str_widget_info);
	return str_widget_info;
}


/* this function will read the single widget information and return the ajax_state structure*/
function read_widget_info(state_text)
{
	var my_state = new ajax_state();	
	/*
		some examples of state_text
		widget=Contributor&page=2&filter=popular
		tab=stories,tab=question
	*/
	//alert(state_text);
	if( state_text.indexOf("widget") > -1 ) /*type of widget*/
	{
		//first split with '&'
		//second split each with '='
		var widget_details = state_text.split('&');
		for(i=0; i<widget_details.length; i++)
		{
			var temp = widget_details[i].split('=');
			if(i == 0) /*widget name,etc*/
			{
				my_state.widget_type = 'widget';
				my_state.name = temp[1];				
			}else if(i == 1) /*page info*/
			{
				my_state.page_no = parseInt(temp[1]);
			}
			else if(i == 2) /* filter info*/
			{
				my_state.filter_name = temp[1];
			}
		}
	}
	else if( state_text.indexOf("tab") > -1 ) /* type of tab */
	{
		var tab_details = state_text.split('&');	
		for(i=0; i<tab_details.length; i++)
		{
			var temp = tab_details[i].split('=');
			if(i == 0) /*widget name,etc*/
			{
				my_state.widget_type = 'tab';
				my_state.name = temp[1];
				my_state.name = temp[1];
			}else if(i == 1) /*tab group info*/
			{
				//alert(temp[1]);
				my_state.tab_group = temp[1];
			}			
		} 
	}
	return my_state;
}

///Attaching a function on window.onload event.
 

//redirection_start_history_manager();
// var my_page_history = '<%= selected_tabs %>';
//	var is_start_history = true;

/* Some errors knowledgebase
	-- Out of stack space / Too much recursion  
	   Reason :: we called start_history_manager twice on same page and both methods of window.onload hooked 
	             (previous one and new one that we are adding in history_manager). Hooking both mehtods each other create
				 recursive cycle and hence IE show "out of stack space" and ff shows 'too much recursions'
	-- 
*/
