var webtank_events = new Array();

function updateAjax( thisDiv , thisSubAction , thisAction , thisVars ) {

	var url = '/'+thisAction.replace(/_/g,'/')+'/ajax.php';
	var pars = 'b='+thisSubAction+( thisVars ? '&'+thisVars : '' );
	
	var myAjax = new Ajax.Updater(
		thisDiv, 
		url, {
			method: 'post', 
			parameters: pars
		}
	);
	
}

// $.fn.jquery = '1.3.2'
function get_ajax_type() {
	if ( typeof(Prototype) != 'undefined' && Prototype.Version ) {
		return 'prototype';
	} else if ( $ && typeof($.fn) != 'undefined' && $.fn.jquery ) {
		return 'jquery';
	} else if ( $j && typeof($j.fn) != 'undefined' && $j.fn.jquery ) {
		return 'jquery_no_conflict';
	}
}

function ajax_update( this_div , content_id , vars , form ) {
	
	var ajax_type = false;
	if ( ajax_type = get_ajax_type() ) {
		if ( ajax_type == 'prototype' ) {
			ajax_update_prototype( this_div , content_id , vars , form );
		} else if ( ajax_type == 'jquery' ) {
			ajax_update_jquery( this_div , content_id , vars , form );
		} else if ( ajax_type == 'jquery_no_conflict' ) {
			ajax_update_jquery_no_conflict( this_div , content_id , vars , form );
		}
	}
	
}

function ajax_update_prototype( this_div , content_id , vars , form ) {
	
	if ( $(this_div) ) {
	
		var url  = '/visitor/content/ajax.php';
		var pars = 'c='+content_id+( vars ? '&'+vars : '' );
		if ( form ) {
			pars += '&'+$(form).serialize();
		}
		var myAjax = new Ajax.Updater(
			this_div, 
			url, {
				method: 'post', 
				evalScripts: true,
				parameters: pars
			}
		);
	
	} else {
//		alert('div does not exist');
	}
	
}

function ajax_update_jquery( this_div , content_id , vars , form ) {
	
	if ( jQuery('#'+this_div) ) {
	
		var url  = '/visitor/content/ajax.php';
		var pars = 'c='+content_id+( vars ? '&'+vars : '' );
		if ( form ) {
			pars += '&'+jQuery('#'+form).serialize();
		}

		jQuery.post( url , pars ,
			function(data){
				jQuery('#'+this_div).html(data);
			});

	} else {
//		alert('div does not exist');
	}
	
}

function ajax_update_jquery_no_conflict( this_div , content_id , vars , form ) {
	
	if ( $j('#'+this_div) ) {
	
		var url  = '/visitor/content/ajax.php';
		var pars = 'c='+content_id+( vars ? '&'+vars : '' );
		if ( form ) {
			pars += '&'+$j('#'+form).serialize();
		}

		$j.post( url , pars ,
			function(data){
				$j('#'+this_div).html(data);
			});

	} else {
//		alert('div does not exist');
	}
	
}

function ajax_request( content_id , vars , form ) {
	
	var ajax_type = false;
	if ( ajax_type = get_ajax_type() ) {
		if ( ajax_type == 'prototype' ) {
			ajax_request_prototype( content_id , vars , form );
		} else if ( ajax_type == 'jquery' ) {
			ajax_request_jquery( content_id , vars , form );
		} else if ( ajax_type == 'jquery_no_conflict' ) {
			ajax_request_jquery_no_conflict( content_id , vars , form );
		}
	}
	
}

function ajax_request_prototype( content_id , vars , form ) {

	var url = '/visitor/content/ajax.php';
	var pars = 'c='+content_id+( vars ? '&'+vars : '' );
	if ( form ) {
		pars += '&'+$(form).serialize();
	}
	
	var myAjax = new Ajax.Request(
		url, {
			method: 'post',
			evalScripts: true,
			parameters: pars,
			onSuccess: function(transport) {
//				alert( transport.responseText );
				for ( i = 0 ; i < webtank_events.length ; i++ ) {
					if ( webtank_events[i].event == transport.responseText ) {
						ajax_trigger_event( webtank_events[i] );
					}
				}
			}
		}
	);

}

function ajax_request_jquery( content_id , vars , form ) {

	var url = '/visitor/content/ajax.php';
	var pars = 'c='+content_id+( vars ? '&'+vars : '' );
	if ( form ) {
		pars += '&'+jQuery('#'+form).serialize();
	}

	jQuery.post( url , pars ,
		function(data){
			for ( i = 0 ; i < webtank_events.length ; i++ ) {
				if ( webtank_events[i].event == data ) {
					ajax_trigger_event( webtank_events[i] );
				}
			}
		});
}

function ajax_request_jquery_no_conflict( content_id , vars , form ) {

	var url = '/visitor/content/ajax.php';
	var pars = 'c='+content_id+( vars ? '&'+vars : '' );
	if ( form ) {
		pars += '&'+$j('#'+form).serialize();
	}

	$j.post( url , pars ,
		function(data){
			for ( i = 0 ; i < webtank_events.length ; i++ ) {
				if ( webtank_events[i].event == data ) {
					ajax_trigger_event( webtank_events[i] );
				}
			}
		});
}

function ajax_trigger_event( event ) {
	if ( typeof event == 'string' ) {
		for ( i = 0 ; i < webtank_events.length ; i++ ) {
			if ( webtank_events[i].event == event ) {
				var event = webtank_events[i];
			}
		}
	}
	switch ( event.action ) {
		case 'refresh':
			ajax_update( 'content_'+event.content_id , event.content_id , '' , '' );
		break;
		case 'redirect':
			document.location = event.param;
		break;
	}
}

function register_content_to_event( content_id , event , action , param ) {
	var event_object_found = false;
	for ( i = 0 ; i < webtank_events.length ; i++ ) {
		if ( webtank_events[i].content_id == content_id && webtank_events[i].event == event && webtank_events[i].action == action && webtank_events[i].param == param ) {
			event_object_found = true;
		}
	}
	if ( !event_object_found ) {
		var i = webtank_events.length;
		webtank_events[i] = new Object();
		webtank_events[i].content_id = content_id;
		webtank_events[i].event  = event;
		webtank_events[i].action = action;
		webtank_events[i].param = param;
	}
}

function ajax_geo_request( this_form , form_element_name , long_prop_id , lat_prop_id , property_id_array ) {
	
	var q = '';
	for ( i = 0 ; i < property_id_array.length ; i++ ) {
		q += this_form.elements[ form_element_name + property_id_array[i] ].value+' ';
	}
	
	var url = '/admin/geo/location/ajax.php';
	var pars = 'q='+encodeURIComponent(q);

	var geoAjax = new Ajax.Request(
		url, {
			method: 'post',
			parameters: pars,
			onSuccess: function(transport) {
				if ( transport ) {
					var geoLocation = transport.responseText.evalJSON(true);
					if ( geoLocation.Status.code != 200 ) {
						alert('Error: Geo location NOT found!');
						return false;
					}
					if ( geoLocation && geoLocation.Placemark[0] && geoLocation.Placemark[0].Point && geoLocation.Placemark[0].Point.coordinates[0] ) {
						this_form.elements[ form_element_name + long_prop_id ].value = geoLocation.Placemark[0].Point.coordinates[0];
						this_form.elements[ form_element_name + lat_prop_id ].value = geoLocation.Placemark[0].Point.coordinates[1];
						alert('Geo location found!');
					} else {
						alert('Error: Geo location NOT found!');
						return false;
					}
				} else {
					alert('Error: Geo location NOT found!');
					return false;
				}
			},
			onFailure: function(transport) {
				alert('Error: Geo location NOT found!');
				return false;
			}
		}
	);
	
	return false;
}

