jQuery(document).ready(function($){

	//
	
	//$('.scriteria').change(function(){ alert('aaaa'); });
	
	$('.scriteria').change(function(){  CemsRoutes.search();});
	$('#iamflex').click(function(){CemsRoutes.search();});
	
	$('#date').change(function(){
		
		if($(this).val()) {
			$('#flexopt').show();
		}
		
	});
	
	$('#route-search').tabs();
	$('.expsort').click(function(){
		CemsRoutes.sortlist($(this)); return false;
	});
	
	$('#taf').click(function(){
		$("#tfdialog").dialog({
			title:'Tell a friend',
			resizable: true,
			height:350,
			width:450,
			modal: true,
			dialogClass: 'taf', 
			buttons: {
				"Send": function() {
					$( this ).dialog( "close" );
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			}
			
			
		});
	});
	
	$('.see-more-trip a').toggle(
			function(){
				$('.calendaritem').show(); 
				$('#route-search').height(508);
				$(this).parent().hide();
			},
			function(){
				
				$('#route-search').height(294);
				$(this).html('SEE MORE TRIPS');
				$(this).parent().show();
			}
	);
	
	$('.see-more-trip2 a').add('#coming-trips').click(function() {
		$('#route-search').height(294);
		$('.see-more-trip a').html('SEE MORE TRIPS').parent().show();
	});
	
	
	$('.trip-bottom').each(function(k,v){
		
		var h = $('.trip-right', this).height();
		$('.trip-left', this).height(h);
		
	});
	
	
});


CemsRoutes = {};


CemsRoutes.sortlist = function(el) {
	
	var order = el.attr('id');
	var params = new Array();
	params[0]='action=sort';
	params[1]='order='+order;
	
	if(jQuery('#iamflex').is(':checked')) {
		params.push('iamflex=1');
	}

	jQuery('.scriteria').each( function(i, v){
		
		var el = jQuery(v);
		if(el.val()) {
			params.push(el.attr('id')+"="+el.val());
		}
		
	});
	
	jQuery('.listsorting li').attr('class','');
	
	if(order.indexOf('_')) {
		var parts = order.split('_');
		if(parts[1]==1) {
			order = parts[0]+'_2';
			dir = 'desc';
		} else {
			order = parts[0]+'_1';
			dir = 'asc';
		}
		el.attr('id', order);
		jQuery('#'+order).parent().addClass(dir);
		
	} else {
		order = order+'_1';
		el.attr('id', order);
		jQuery('#'+order).parent().addClass('asc');
	}

	jQuery.ajax({
		   type: "POST",
		   url: "/wp-content/plugins/cems-routes/rpc/search.php",
		   data: params.join("&"),
		   success: function(msg){
				jQuery('#routelist').html(msg);
		   },
		   error: function(r,m) {
			   
		   }	
		 });
};


CemsRoutes.search =function() {
	var params = new Array();
	params[0]='action=search';
	
	if(jQuery('#iamflex').is(':checked')) {
		params.push('iamflex=1');
	}

	jQuery('.scriteria').each( function(i, v){
		
		var el = jQuery(v);
		if(el.val()) {
			params.push(el.attr('id')+"="+el.val());
		}
		
	});
	var isRouteList = false;
	
	if(jQuery('#routelist').attr('id')=='routelist') {
		isRouteList = true;
		params.push('routeList=true');
	}
	
	jQuery.ajax({
		   type: "POST",
		   url: "/wp-content/plugins/cems-routes/rpc/search.php",
		   data: params.join("&"),
		   success: function(msg){
			if(isRouteList) {
				jQuery('#routelist').html(msg);
			} else {
				eval('res='+msg);
				jQuery('#preres ul').html('');
				if(res.length > 0) {
					jQuery.each(res, function(i, v){
						jQuery('<li>'+(i+1)+'. <a href="'+v.url+'">'+v.name+'('+v.start+'-'+v.end+')</a></li>').appendTo(jQuery('#preres ul'));
						
					});
				} else {
					jQuery('#preres ul').html(res.length+' matches found');
				}

			}
		   },
		   error: function(r,m) {
			   
		   }	
		 });
};
