/**
 * @author Ayumu Takai 
 * @extends jquery
 */

jQuery.fn.flowchart = function(options){
    var _flowchart = this;
    var current = "#flowchart_0";
    var prev = new Array();
    var opt = options;
    
    $.ajax({
    	type:"GET",
        url:options.xml,
        dataType:"xml",
        success: xmlParser,
        error:xmlError
    });

    function select(anchor,mode){
      $(_flowchart).children('.selected').removeClass('selected');
      path = $(anchor).attr('href').replace(/^.+#/,'#')
      $(path).addClass('selected');
      if(mode == "forward"){
      	prev.push(current);
      }
      current = path;
    }
    
    function xmlError(XMLHttpRequest, textStatus, errorThrown){
    	alert(textStatus);
    }
    
    function xmlParser(xml){
      $(xml).find('title').each(function(){
	        elm = $('<div class="title" id="flowchart_' + $(this).attr("id")  +'"></div>').appendTo(_flowchart);
            anchor = $('<a href="#flowchart_' + $(this).attr("onclick") + '" class="start"></a>').appendTo(elm);
            anchor.hover(function(){
            	$(this).addClass("start_on");
            },function(){
            	$(this).removeClass("start_on");
            });
            anchor.click(function(ev){
              prev = new Array();
              ev.preventDefault();
              select(ev.currentTarget,"forward");
              var p = $(opt.isAutoChange);
              $(p).text("false");
            });
      });
      
      $(xml).find('question').each(function(){
			elm = $('<div class="question" id="flowchart_' + $(this).attr('id')  +'"></div>').appendTo(_flowchart);
            $('<div class="qstr">'+$(this).find("question").text()+'</div>').appendTo(elm);
            choice = $('<ul class="choice"></ul>').appendTo(elm);
	        $(this).find('choice').each(function(){
                li = $('<li></li>').appendTo(choice);
	      	    anchor = $('<a href="#flowchart_' + $(this).attr('to') + '">'+$(this).text()+'</a>').appendTo(li);
                anchor.click(function(ev){
                	ev.preventDefault();
                	select(ev.currentTarget,"forward");
              	});
	    	});
            anchor = $('<a href="#" class="back"></a>').appendTo(elm);
            anchor.click(function(ev){
                ev.preventDefault();
                $(ev.currentTarget).attr("href",prev.pop());
                select(ev.currentTarget,"back");
              });
      });

      $(xml).find('answer').each(function(){
        	elm = $('<div class="answer" id="flowchart_' + $(this).attr('id') + '"></div>').appendTo(_flowchart);
	    	$('<div class="text">' +$(this).find('text').text() +'</div>').appendTo(elm);
            anchor = $('<a href="#flowchart_0" class="top"></a>').appendTo(elm);
            anchor.click(function(ev){
                ev.preventDefault();
                select(ev.currentTarget,"forward");
              });
            anchor = $('<a href="#" class="back"></a>').appendTo(elm);
            anchor.click(function(ev){
                ev.preventDefault();
                $(ev.currentTarget).attr("href",prev.pop());
                select(ev.currentTarget,"back");
              });
	  });

      $('#flowchart_0').addClass('selected');
    }

};

