SITEAGGREGATORCOMBO_AJAX_URL = '/common/ajax/front/components/SiteAggregatorCombo/ajax.php';

/* 
3 functions per list:
- Pagination load
- Switch tabs load
- filter by load

Reason: each take their arguments differently
*/

/*
* PAGINATION: LATEST
*
* Offset: 	pagination var
* Object: 	object to pass uniq id {elm: this, uniq_id: UniqID} 
* 			Its an object cuz the php pagination class doesn't accept individual items
*/ 
function fncSiteAggregatorCombo_PaginationLoad_Latest(offset, object){
	var uniqid	= object.uniqid;
	var filter 	= object.filter;
	
	// load
	$('SAC_Loading_'+uniqid).update('<img src="/common/templates/default/images/loadingsmall.gif" />');

	// ajax
	new Ajax.Updater({ success : $('SAC_LatestList_'+uniqid)}, SITEAGGREGATORCOMBO_AJAX_URL, {
		parameters: { 
			action		: 'Aggregator_Load_Latest',
			offset		: offset,
			uniqid		: uniqid,
			filter		: filter
		},
		evalScripts : true,
							
		onSuccess: function(transport){
			// make sure the latest stuff gets to appear
			$('SAC_LatestList_'+uniqid, 'SAC_LatestFilter_'+uniqid).invoke('show');			
			
			// hide loading and latest stuff
			$('SAC_PopularList_'+uniqid, 'SAC_PopularFilter_'+uniqid, 'SAC_Loading_'+uniqid).invoke('hide');

		},
		
		onFailure: function(transport){
			$('SAC_Loading_'+uniqid).update('Error retrieving updates. Please refresh the page.'); 
		}
	});
	
}

/*
* TABS: Latest
*/ 
function fncSiteAggregatorCombo_TabsLoad_Latest(uniqid, filter_option){
	
	// load
	$('SAC_Loading_'+uniqid).update('<img src="/common/templates/default/images/loadingsmall.gif" />');

	// ajax
	new Ajax.Updater({ success : $('SAC_LatestList_'+uniqid)}, SITEAGGREGATORCOMBO_AJAX_URL, {
		parameters: { 
			action		: 'Aggregator_Load_Latest',
			uniqid		: uniqid
		},
		evalScripts : true,
							
		onSuccess: function(transport){
			// make sure the latest list & filters appear
			$('SAC_LatestList_'+uniqid, 'SAC_LatestFilter_'+uniqid).invoke('show');			
			
			// hide loading, popular list and popular filters
			$('SAC_PopularList_'+uniqid, 'SAC_PopularFilter_'+uniqid, 'SAC_Loading_'+uniqid).invoke('hide');

			// change class of tabs
			$('SiteAggregatorCombo_'+uniqid).down('.sac_tab_latest').addClassName('selected');
			$('SiteAggregatorCombo_'+uniqid).down('.sac_tab_popular').removeClassName('selected');
			
			if(filter_option == 'dropdown') {
				// select none (in dropdown)
				var select_dropdown = $('SAC_PopularFilter_'+uniqid).down('select');
				select_dropdown[0].selected = true;
			} else {
				// un-select whatever is selected
				var sel_elm = $('SAC_LatestFilter_'+uniqid).down('.sac_filter_selected');
				$(sel_elm).removeClassName('sac_filter_selected');
				
				// select all in textlinks
				var first_elm = $('SAC_LatestFilter_'+uniqid).down('a');
				$(first_elm).addClassName('sac_filter_selected');
			}
		},
		
		onFailure: function(transport){
			$('SAC_Loading_'+uniqid).update('Error retrieving updates. Please refresh the page.'); 
		}
	});
	
}

/*
* FILTERS: Latest
*/ 
function fncSiteAggregatorCombo_FilterLoad_Latest(uniqid, filter, clicked_link){
	
	if(typeof filter == 'string') {
		// Filter option: link
		var filter = filter;
		
		var sel_elm = $('SAC_LatestFilter_'+uniqid).down('.sac_filter_selected');
		$(sel_elm).removeClassName('sac_filter_selected');
		
		$(clicked_link).addClassName('sac_filter_selected');
		
	} else if(typeof filter == 'object') {
		// Filter option: dropdown
		var filter = $(filter).getValue()
	}
	
	
	// load
	$('SAC_Loading_'+uniqid).update('<img src="/common/templates/default/images/loadingsmall.gif" />');

	// ajax
	new Ajax.Updater({ success : $('SAC_LatestList_'+uniqid)}, SITEAGGREGATORCOMBO_AJAX_URL, {
		parameters: { 
			action		: 'Aggregator_Load_Latest',
			uniqid		: uniqid,
			filter		: filter
		},
		evalScripts : true,
							
		onSuccess: function(transport){
			$('SAC_Loading_'+uniqid).hide();
		},
		
		onFailure: function(transport){
			$('SAC_Loading_'+uniqid).update('Error retrieving updates. Please refresh the page.'); 
		}
	});
}


/********************************************************************************************************

********************************************************************************************************/


/*
* PAGINATION: Popular
*
* Offset: 	pagination var
* Object: 	object to pass uniq id {elm: this, uniq_id: UniqID} 
* 			Its an object cuz the php pagination class doesn't accept individual items
*/ 
function fncSiteAggregatorCombo_PaginationLoad_Popular(offset, object){
	var uniqid  = object.uniqid;
	var filter 	= object.filter;
	
	// loading
	$('SAC_Loading_'+uniqid).update('<img src="/common/templates/default/images/loadingsmall.gif" />');

	// ajax
	new Ajax.Updater({ success : $('SAC_PopularList_'+uniqid)}, SITEAGGREGATORCOMBO_AJAX_URL, {
		parameters: { 
			action  	: 'Aggregator_Load_Popular',
			offset  	: offset,
			uniqid  	: uniqid,
			filter		: filter
		},
		evalScripts : true,
							
		onSuccess: function(transport){
			// make sure the popular stuff gets to appear
			$('SAC_PopularList_'+uniqid, 'SAC_PopularFilter_'+uniqid).invoke('show')
			
			// hide loading and latest stuff
			$('SAC_LatestList_'+uniqid, 'SAC_LatestFilter_'+uniqid, 'SAC_Loading_'+uniqid).invoke('hide');
			
		},
		
		onFailure: function(transport){
			$('SAC_Loading_'+uniqid).update('Error retrieving updates. Please refresh the page.'); 
		}
	});
	
}


/*
* TABS: Latest
*/ 
function fncSiteAggregatorCombo_TabsLoad_Popular(uniqid, filter_option){
	// load
	$('SAC_Loading_'+uniqid).update('<img src="/common/templates/default/images/loadingsmall.gif" />');

	// ajax
	new Ajax.Updater({ success : $('SAC_PopularList_'+uniqid)}, SITEAGGREGATORCOMBO_AJAX_URL, {
		parameters: { 
			action		: 'Aggregator_Load_Popular',
			uniqid		: uniqid
		},
		evalScripts : true,
							
		onSuccess: function(transport){
			// make sure the poular list and filter get shown
			$('SAC_PopularList_'+uniqid, 'SAC_PopularFilter_'+uniqid).invoke('show');			
			
			// hide loading, latest list, and latest filters 
			$('SAC_LatestList_'+uniqid, 'SAC_LatestFilter_'+uniqid, 'SAC_Loading_'+uniqid).invoke('hide');

			// change class of tabs
			$('SiteAggregatorCombo_'+uniqid).down('.sac_tab_popular').addClassName('selected');
			$('SiteAggregatorCombo_'+uniqid).down('.sac_tab_latest').removeClassName('selected');
			
			if(filter_option == 'dropdown') {
				// select none (in dropdown)
				var select_dropdown = $('SAC_LatestFilter_'+uniqid).down('select');
				select_dropdown[0].selected = true;
				
			} else {
				// un-select whatever is selected
				var sel_elm = $('SAC_PopularFilter_'+uniqid).down('.sac_filter_selected');
				$(sel_elm).removeClassName('sac_filter_selected');
				
				// select all in textlinks
				var first_elm = $('SAC_PopularFilter_'+uniqid).down('a');
				$(first_elm).addClassName('sac_filter_selected');
			}
			
		},
		
		onFailure: function(transport){
			$('SAC_Loading_'+uniqid).update('Error retrieving updates. Please refresh the page.'); 
		}
	});
	
}

/*
* FILTERS: Popular
*/ 
function fncSiteAggregatorCombo_FilterLoad_Popular(uniqid, filter){
	// load
	$('SAC_Loading_'+uniqid).update('<img src="/common/templates/default/images/loadingsmall.gif" />');

	if(typeof filter == 'string') {
		// Filter option: link
		var filter = filter;

	} else if(typeof filter == 'object') {
		// Filter option: dropdown
		var filter = $(filter).getValue()
	}

	// ajax
	new Ajax.Updater({ success : $('SAC_PopularList_'+uniqid)}, SITEAGGREGATORCOMBO_AJAX_URL, {
		parameters: { 
			action		: 'Aggregator_Load_Popular',
			uniqid		: uniqid,
			filter		: filter
		},
		evalScripts : true,
							
		onSuccess: function(transport){
			$('SAC_Loading_'+uniqid).hide();
		},
		
		onFailure: function(transport){
			$('SAC_Loading_'+uniqid).update('Error retrieving updates. Please refresh the page.'); 
		}
	});
}



