function doAjaxAction(theAction){
  switch(theAction.d){
    case '_prompt' :
      // CALL PROMPT PROCEDURE
      prompt(theAction.c);
      break;
    case '_messageBox' :
      // CALL MESSAGE BOX PROCEDURE
      alert(theAction.c);
      break;
    case '_javascript' :
      // EXECUTE JAVASCRIPT CODE
      eval(theAction.c);
      break;
    case '_window' :
      // OPEN A WINDOW MODAL
      openWindow(theAction.c);
      break;
    case '_redirect' :
      // OPEN A WINDOW MODAL
      window.location.replace(theAction.c);
      break;
    default:
      $('#'+theAction.d).html(theAction.c);
      break;
  }
}
function ajaxAction(actions, returns, params){
	params = $.extend({
		'_a':actions,
		'_r':returns,
		'_d':(new Date()).getTime()
	},params);
  $.ajax({
    type: 'POST',
    url: '/',
    data: params,
    error:function(jqXHR, textStatus, errorThrown){
      alert(jqXHR.status);
      alert(errorThrown);
    },
    success: function(data){
      data=eval('('+data+')');
			$.each(data, function(i,item){
				doAjaxAction(item);
			});
		},
    dataType: 'text'
  });
}

var _windowStack = new Array();
function openWindow(content){
	_windowStack.unshift(content);
	_displayWindow();
}


function _displayWindow(){
	if($('#_window').length!=0){
		$('#_window').remove();
	}
	$("<div id=\"_window\" title=\"\"></div>").appendTo("body")
	$('#_window').html(_windowStack[0]);
	$('#_window').dialog({
		modal:true,
		close: function(ev, ui) {
			_windowStack.shift();
			$('#_window').remove();
			if(_windowStack.length!=0){
				_displayWindow();
			}
		}
	});
}




/*************/

$(document).ready(function()  
{ 
  $("a#1").click(function() { show(0); return false; });
	$("a#2").click(function() { show(1); return false; });
	$("a#3").click(function() { show(2); return false; });
	$("a#4").click(function() { show(3); return false; });
	$("a#5").click(function() { show(4); return false; });
	$("a#6").click(function() { show(5); return false; }); 
	var images = $("#gallery li");
	var thumbs = $("#thumbs img");
	var thumblinks = $("#thumbs a");
	var index = thumbs.length-1;
	var thumbsHeight = 114;
	thumblinks.slice(thumblinks.length-3,thumblinks.length).clone().attr('id', function() { var id = $(this).id; return 'b' + this.id}).prependTo("#thumbs");
	$("a#b2").click(function() { show(1); return false; });
	$("a#b3").click(function() { show(2); return false; });
	$("a#b4").click(function() { show(3); return false; });  
	for (i=0; i<thumbs.length; i++) {
		$(thumbs[i]).addClass("thumb-"+i);
		$(images[i]).addClass("image-"+i);
	}
	show(index);
	setInterval(sift, 8000);
	function sift() {
		if ( index < thumbs.length && index > 0) {index -= 1 ; }
		else { index = thumbs.length-1 }
		show ( index );
	} 
	function show(num) {
		images.fadeOut(455);
		$(".image-"+num).stop().fadeIn(455);
		/*var scrollPos = ( (num-1) * thumbsHeight ) + 114;
		$("#thumbs").stop().animate({scrollTop: scrollPos}, 455); */
	} 
	
});  
