// Javascript Document

//Wrapper functions
function fetch_feed(page){
	if(!window.stop_update){
		page = ( typeof page == 'number') ? "&page="+page : '' ;
		var cat = (document.getElementById('category')!= null) ? document.getElementById('category').value : null ;
		var loc = (document.getElementById('location')!= null) ? document.getElementById('location').value : null ;
		if(cat && loc){
			if(window.current_request.active && window.current_request.loc==loc && window.current_request.cat==cat && window.current_request.page==page){
				// Request is a duplicate
				// Block Send if there's aready a request out for the same data
			}else{
				// Request is not a duplicate
				// If there is another request already out then cancel it
				if(window.current_request.active){ a.abort(); }
				// Set Current Request data and issue request
				window.current_request = { 'active' : true , 'cat' : cat , 'loc' : loc , 'page' : page } ;
				a.sendData('cat='+cat+"&loc="+loc+page);
			}
		}
	}
}

function update_feed(s){
	if(!window.stop_update){
		fetch_feed();
		var t = setTimeout( function(){ update_feed(); } , 60000);
	}
}

function join_wrap(){
	if(!window.authenticated){
		window.location = "join_gateway.php";
	}else{
		b.sendData('cat='+document.getElementById('category').value+"&loc="+document.getElementById('location').value);
	}
}

function save_state(){
	c.sendData('cat='+document.getElementById('category').value+"&loc="+document.getElementById('location').value);
}

function generate_widget_code(){
	var c = document.getElementById('category').value;
	var l = document.getElementById('location').value;
	d.sendData('cat='+c+"&loc="+l,0,0,0,0,0,0,0,{'id':'load'},{'l_or':l,'c_or':c});	
}

function find_tab(){
	// Control Look
	vis('find_content','block','widget_content','none','join_content','none');
	class_switch('find_tab','ob','widget_tab','tb50','join_tab','tb50');
	// Set Go button functionality		
	document.getElementById('selector_go').onclick = fetch_feed;
	// Enable feed reload auto update
	window.stop_update = false;
	update_feed();
	// Save the state of the current selection - reload on change
	document.getElementById('category').onchange = function(){ save_state(); fetch_feed(); }
	document.getElementById('location').onchange = function(){ save_state(); fetch_feed(); }
}

function join_tab(){
	// Control Look
	vis('find_content','none','widget_content','none','join_content','block');
	class_switch('find_tab','tb50','widget_tab','tb50','join_tab','ob');
	// Set Go button functionality		
	document.getElementById('selector_go').onclick = join_wrap;
	// Prevent feed reload auto update		
	window.stop_update = true;
	// Save the state of the current selection - don't reload on change
	document.getElementById('category').onchange = function(){ save_state(); }
	document.getElementById('location').onchange = function(){ save_state(); }
}

function widget_tab(){
	// Control Look
	vis('find_content','none','widget_content','block','join_content','none');
	class_switch('find_tab','tb50','widget_tab','ob','join_tab','tb50');
	// Set Go button functionality		
	document.getElementById('selector_go').onclick = function(){save_state(); generate_widget_code(); }
	// Prevent feed reload auto update		
	window.stop_update = true;
	// Save the state of the current selection - don't reload on change
	document.getElementById('category').onchange = function(){ save_state(); generate_widget_code(); }
	document.getElementById('location').onchange = function(){ save_state(); generate_widget_code(); }
	//Save State
	save_state();
	//Generate Widget Code
	generate_widget_code();
}


// Body Onload Function
function init(){
	
	// Set Tab actions
	if(document.getElementById('find_tab')!= null){ document.getElementById('find_tab').onclick = find_tab; }
	if(document.getElementById('join_tab')!= null){ document.getElementById('join_tab').onclick = join_tab; }
	if(document.getElementById('widget_tab')!= null){ document.getElementById('widget_tab').onclick = widget_tab; }
	
	// Save the state of the current selection - reload on change
	if(document.getElementById('category')!= null){ document.getElementById('category').onchange = function(){ save_state(); fetch_feed(); } }
	if(document.getElementById('location')!= null){ document.getElementById('location').onchange = function(){ save_state(); fetch_feed(); } }

	// Set Default Go button function
	if(document.getElementById('selector_go')!= null){ document.getElementById('selector_go').onclick = fetch_feed; }
	
	// Set link functions if the links are present - this is controlled by variable data from the database
	if (document.getElementById('bird_join_link') != null){ document.getElementById('bird_join_link').onclick = join_tab; }
	if (document.getElementById('bird_widget_link') != null){ document.getElementById('bird_widget_link').onclick = widget_tab; }	
	if (document.getElementById('sign_up_link') != null){ document.getElementById('sign_up_link').onclick = join_tab; }	
	if (document.getElementById('user_cp') != null){ document.getElementById('user_cp').onclick = show_membership_manager; }
	
	// onload functions
	if(window.join_pending){ join_wrap(); }
	var t = setTimeout( function(){ update_feed(); } , 60000);

	
}
