(function( $ ){
	
  var settings = {
		     noSteps:0,
		     currentStep:null,
		     updatedCompleted:function(){}
  };
   
  var locker={
		  goingTo:0,
		  status:"locked"
  };
  
  var template={
		  firstStep:{
			  current:" ",
			  marked:" ",
			  unmarked:" "
		  },
		  lastStep:{
			  current:" ",
			  marked:" ",
			  unmarked:" "
		  },
		  middleStep:{
			  current:" ",
			  marked:" ",
			  unmarked:" "
		  },
		  header:"",
		  footer:"",
		  additionalInfo:""
  };
  
  
  var defaultStep={
		  title:"",
		  description:"",
		  beforeLoading:function(){},
		  afterLoading:function(){},
		  width:"0px",
		  height:"0px"
  };
  
  
  var render=function($this){
	  var i;
	  var data = $this.data("wizard");
	  $this.empty();
	  if(data.settings.currentStep!=undefined){
		  if((data.template.header!="")&&(data.template.header!=undefined))
			  $.tmpl(data.template.header, data.steps[data.settings.currentStep]).appendTo($this); 
	  }
	  for(i=0;i<data.steps.length;i++){
			 if(i==0){
				 if(i==data.settings.currentStep)
					 $.tmpl(data.template.firstStep.current, data.steps[i]).appendTo($this);
				 if(i<data.settings.currentStep)
					 $.tmpl(data.template.firstStep.marked, data.steps[i]).appendTo($this);
			 }else if(i==data.steps.length-1){
				 if(i==data.settings.currentStep)
					 $.tmpl(data.template.lastStep.current, data.steps[i]).appendTo($this);
				 if(i>data.settings.currentStep)
					 $.tmpl(data.template.lastStep.unmarked, data.steps[i]).appendTo($this);
			 }else{
				 if(i==data.settings.currentStep)
					 $.tmpl(data.template.middleStep.current, data.steps[i]).appendTo($this);
				 if(i>data.settings.currentStep)
					 $.tmpl(data.template.middleStep.unmarked, data.steps[i]).appendTo($this); 
				 if(i<data.settings.currentStep)
					 $.tmpl(data.template.middleStep.marked, data.steps[i]).appendTo($this); 
			 }

		 }
	  if(data.settings.currentStep!=undefined){
		  if((data.template.footer!="")&&(data.template.footer!=undefined))
			  $.tmpl(data.template.footer, data.steps[data.settings.currentStep]).appendTo($this); 
	  }
	  
  };
  


  var methods = {
     init : function( temp, step ) {

       return this.each(function(){
         
         var $this = $(this),
             data = $this.data('wizard'),
             version = 0.1;
         
         if ( ! data ) {
           $(this).data('wizard', {
               target : $this,
               version : version,
               settings : $.extend(true, {}, settings),
               template: $.extend(true, {}, template),
               defaultStep: $.extend(true, {}, defaultStep),
               steps: [],
               locker:$.extend(true, {}, locker)
           });
           data = $this.data('wizard');
         }
         
         if ( temp ) { 
             $.extend( data.template, temp );
         }
         
         if ( step ) { 
             $.extend( data.defaultStep, step );
         }

         
       });
     },
     
     destroy : function( ) {

       return this.each(function(){

         var $this = $(this),
             data = $this.data('wizard');

         // Namespacing FTW
         $(window).unbind('.wizard');
         $this.removeData('wizard');

       });

     },
     
     show : function( ) {  },
     hide : function( ) {  },
     update : function(stepNo) {
    	 return this.each(function(){
    		 var $this = $(this),
    		 data = $this.data('wizard');
                 
                 if(stepNo!=undefined){
                     if(typeof(stepNo)=="number"){	 
                             if(data.steps[stepNo]!=undefined){
                                     data.settings.currentStep=stepNo;
                             }
                     }
                 }
    		 
    		 render($this);
    		 data.settings.updatedCompleted.apply( $this );
    	 });
     },
     addStep: function (step){
    	 return this.each(function(){
    		 var $this = $(this),
    		 	data = $this.data('wizard');
    		 	temStep={
    			 	title:data.defaultStep.title,
    			 	description:data.defaultStep.description,
    			 	beforeLoading:data.defaultStep.beforeLoading,
    			 	afterLoading:data.defaultStep.afterLoading,
    			 	width:data.defaultStep.width,
    			 	height:data.defaultStep.height
    		 	};
    		 if(step) $.extend( temStep, step );
    		 data.steps.push(temStep);
    		 data.settings.noSteps=data.steps.length;
    		 if(data.settings.currentStep==undefined) data.settings.currentStep=0;
    		 //$this.data('wizard',data);
    	 });
     },
     
     changeStep: function (stepNo,step){
    	 return this.each(function(){
    		 var $this = $(this),
    		 data = $this.data('wizard');
    		 if(stepNo==undefined) return;
    		 if(typeof(stepNo)=="number"){	 
	    		 if(data.steps[stepNo]!=undefined){
	    			 $.extend( data.steps[stepNo], step );
	    		 }
    		 }

    	 });
     },
     
     goToStep: function (stepNumber){
    	 return this.each(function(){
    		 var $this = $(this),
    		 data = $this.data('wizard');
    		 
    		 if(data.settings.currentStep!=undefined){
	    		 if((stepNumber>=0)&&(stepNumber<=data.settings.noSteps-1)){
	    			 data.locker.status="unlocked";
	    			 data.locker.goingTo=stepNumber;
	    			 data.steps[stepNumber].beforeLoading.apply( $this );
	    			 if(data.locker.status=="unlocked"){
	    				data.settings.currentStep=stepNumber;
	    			 	render($this);
                                        data.settings.updatedCompleted.apply( $this );
	    			 	data.steps[stepNumber].afterLoading.apply( $this );
	    			 }
	    			 data.locker.status="locked";
	    		 }
    		 }
    	 });
     },
     
     next: function(){
    	 return this.each(function(){
    		 var $this = $(this),
    		 data = $this.data('wizard');
    		 if(data.settings.currentStep!=undefined){
    			 var stepNumber=data.settings.currentStep+1;
	    		 if((stepNumber>=0)&&(stepNumber<=data.settings.noSteps-1)){
	    			 data.locker.status="unlocked";
	    			 data.locker.goingTo=stepNumber;
	    			 data.steps[stepNumber].beforeLoading.apply( $this );
	    			 if(data.locker.status=="unlocked"){
	    				 data.settings.currentStep=stepNumber;
		    			 render($this);
		    			 data.settings.updatedCompleted.apply( $this );
		    			 data.steps[stepNumber].afterLoading.apply( $this );
		    		 }
	    			 data.locker.status="locked";
	    		 }
    		 }
    		 
    	 });
     },
     
     
     back: function(){
    	 return this.each(function(){
    		 var $this = $(this),
    		 data = $this.data('wizard');
    		 if(data.settings.currentStep!=undefined){
    			 var stepNumber=data.settings.currentStep-1;
	    		 if((stepNumber>=0)&&(stepNumber<=data.settings.noSteps-1)){
	    			 data.locker.status="unlocked";
	    			 data.locker.goingTo=stepNumber;
	    			 data.steps[stepNumber].beforeLoading.apply( $this );
	    			 if(data.locker.status=="unlocked"){
	    				 data.settings.currentStep=stepNumber;
		    			 render($this);
		    			 data.settings.updatedCompleted.apply( $this );
		    			 data.steps[stepNumber].afterLoading.apply( $this );
		    		 }
	    			 data.locker.status="locked";
	    		 }
    		 }
    		 
    	 });
     },
     
     lock:function(){
    	 return this.each(function(){
	    	 var $this = $(this),
			 data = $this.data('wizard');
	    	 data.locker.status="locked";
    	 });
     },
     
     unlock:function(){
    	 return this.each(function(){
	    	 var $this = $(this),
			 data = $this.data('wizard');
	    	 data.locker.status="unlocked";
    	 });
     },
     
     stepsNumber:function(){
    	 var $this = $(this),
		 data = $this.data('wizard');
    	 return data.settings.noSteps;
     },
     
     currentStep:function(){
    	 var $this = $(this),
    	 data = $this.data('wizard');
    	 return data.settings.currentStep;
     },
     
     updatedCompleted:function(f){
    	 return this.each(function(){
	    	 var $this = $(this),
			 data = $this.data('wizard');
	    	 data.settings.updatedCompleted=f;
    	 });    	 
     },
     
     getStep: function (stepNo){
        var $this = $(this),
    	data = $this.data('wizard');
    	if(stepNo==undefined) return;
    	if(typeof(stepNo)=="number"){	 
             if(data.steps[stepNo]!=undefined){
	    	 return data.steps[stepNo];
	     }else return null;
    	}else return null;
     }
     
  };

  $.fn.wizard = function( method ) {
    
    if ( methods[method] ) {
      return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.wizard' );
    }    
  
  };

})( jQuery );

