// JavaScript Document
(function($) {
	$.fn.jform = function(obj) {
		
		if(!Boolean(obj)){
			obj = {};
		}
		
		if(obj.confirmation){
			this.append('<fieldset class="sectionwrap confirmation"><legend>'+$('#confirmation_string').text()+'</legend><div class="content"></div></fieldset>')
		}
		
		this.find('[type="submit"]').hide();
		
		var currentID = Boolean(obj.startpage)?obj.startpage:0;
		var numberOfPage = this.find('fieldset').length;
		var target = this;


		if(Boolean(obj.setTips)){
			this.find('input[type="text"]').jform_setTips()
		}
		
		if(Boolean(obj.datepicker)){
			if(Boolean(obj.datepickerValue)){
				if(Boolean(obj.setTips)){
					var onClose = obj.datepickerValue.onClose;
					obj.datepickerValue.onClose =  function(dateText, inst){
						$(this).blur();
						if(Boolean(onClose)){
							this.onClose = onClose
							this.onClose();
						}
					}
				}
				this.find('input'+obj.datepicker).datepicker(obj.datepickerValue);
				
			}else{
				this.find('input'+obj.datepicker).datepicker();
			}
			this.find('input'+obj.datepicker).attr('readonly',true)
		}
		
		function jform_page_change(id){
			if(id>=0 && id<=numberOfPage){
				if(!Boolean(obj.onPrevNextEvent)){
					target.children('.jform_cycle').cycle(id);
					currentID = id;
					return true
				}else if(typeof(obj.onPrevNextEvent)=='function'){
					var canNext = obj.onPrevNextEvent(currentID,id,id>currentID, target.find('.jform-step.step'+currentID), target.find('.jform-step.step'+id)); 
					if(canNext==null || canNext){
						target.children('.jform_cycle').cycle(id);
						currentID = id
						return true;
					}
				}
				return false;
			}
		}
		
		this.wrap('<div id="jform"/>').before('<div id="jform-header" class="stepsguide"><ul id="jform-pages"></ul></div>').after(function(){
				var _prev = 'prev';
				var _next = 'next';
				if(Boolean(obj.prevTemplate)){
					if(typeof(obj.prevTemplate)=='string'){
						_prev = obj.prevTemplate.replace('%page%',currentID-1);
					};
					if(typeof(obj.prevTemplate)=='function'){
						_prev = obj.prevTemplate(currentID-1, $(this))
					};
				}
				if(Boolean(obj.nextTemplate)){
					if(typeof(obj.nextTemplate)=='string'){
						_next = obj.nextTemplate.replace('%page%',currentID-1);
					};
					if(typeof(obj.nextTemplate)=='function'){
						_next = obj.nextTemplate(currentID-1, $(this))
					};
				}	
				return '<div id="jform-footer" class="formpaginate"><ul id="jform-prev-next"><li ><a class="prev" href="javascript:void(0)">'+_prev+'</a></li><li class="status"></li><li ><a class="next" href="javascript:void(0)">'+_next+'</a>'+(Boolean(obj.confirmation)?'<a id="jform-submit-button" style="display:None">Submit</a>':'')+'</li></ul></div>'
		})
		
		if(obj.confirmation){
			$('#jform-submit-button').click(function(e){
				e.preventDefault()
				target.submit();
			})
		}
		
		this.children('fieldset:first').wrap('<div class="jform_cycle"/>');
		this.children('.jform_cycle').append(this.find('fieldset').not('fieldset:first'));
		
		this.find('fieldset').wrap(function(){
			var index = $(this).parent().children().index(this)
			var face = index+1;
			if(Boolean(obj.pageTemplate)){
				if(typeof(obj.pageTemplate)=='string'){
					face = obj.pageTemplate.replace('%page%',index+1);
				};
				if(typeof(obj.pageTemplate)=='function'){
					face = obj.pageTemplate(index+1, $(this))
				};
			}
			$('#jform-pages').append('<li class="jform-page step'+index+'"><a href=".step'+index+'">'+face+'</a></li>')
			return '<div class="jform-page-content step'+index+'"/>'
		})
		
		
		
		$('#jform-pages a').click(function(e){
			e.preventDefault()
			if(jform_page_change(parseInt($(this).attr('href').split('.step')[1].split(' ')[0]))){
			}
		})
		
		$('#jform-prev-next .prev').click(function(e){
			e.preventDefault()
			jform_page_change(currentID-1)
		})
		
		$('#jform-prev-next .next').click(function(e){
			e.preventDefault()
			jform_page_change(currentID+1)
		})
		
		this.children('.jform_cycle').cycle({ 
    		fx: (Boolean(obj.fx))?obj.fx:'fade', 
    		speed: (Boolean(obj.speed))?obj.speed:'fast', 
    		timeout: 0, 
    		startingSlide: currentID,
    		nowrap:  true,
    		before:function(currSlideElement, nextSlideElement, options, forwardFlag){
    			if(obj.confirmation){
    				if(options.nextSlide == numberOfPage-1){
    					target.find('.confirmation .content').html('');
    					var clone = target.find('fieldset').not('.confirmation').clone()
    					clone.find('input,select,textarea').replaceWith(function(){
    						if($(this).attr('type')=='checkbox'){
    							if(target.find('[name="'+$(this).attr('name')+'"]').attr('checked')){
    								return '<div class="input checkbox">'+'Yes'+'</div>';
    							}else{
    								return '<div class="input checkbox">'+'No'+'</div>';
    							}
    						}else if($(this).attr('type')=='submit'){
    							return null;//target.find('[type="submit"]').clone().show();
    						}else if($(this).is('select')){
    							return '<div class="input select">'+target.find('[name="'+$(this).attr('name')+'"]>option:selected[value!=""]').text()+'</div>';
    						}else{
    							return '<div class="input '+$(this).attr('type')+'">'+target.find('[name="'+$(this).attr('name')+'"]').attr('value')+'</div>';
    						}
    					})
    					clone.find('label.tips').remove();
        				target.find('.confirmation .content').append(clone);
    				}
    			}
    			
    			$('#jform-pages .step'+options.currSlide).removeClass('active')
    			
    			//console.log(options.currSlide+'=>'+options.nextSlide);
    		},
    		
    		after:function(currSlideElement, nextSlideElement, options, forwardFlag){
 
    			$('#jform-pages .step'+options.currSlide).addClass('active')
    			$('#jform-prev-next li').removeClass('inactive');
    			if(options.currSlide <= 0){
    				$('#jform-prev-next .prev').parent().addClass('inactive')
    			}
    			if(options.currSlide >= numberOfPage-1){
    				$('#jform-prev-next .next').parent().addClass('inactive')
    			}
    			
    			if(obj.confirmation){
    				if(options.currSlide == numberOfPage-1){
    					target.find('.confirmation').parent().height(target.find('.confirmation').parent().parent().innerHeight())
    					var height = 0;
    					target.find('.confirmation').children().not('.content').each(function( ){ height += $(this).outerHeight()});
    					target.find('.confirmation .content').height(target.find('.confirmation').parent().innerHeight()-height)
    					target.find('.confirmation .content').css({'overflow-y':'auto','overflow-x':'hidden'})
    					$('#jform-submit-button').show();
    				}else{
    					$('#jform-submit-button').hide();
    				}
    			}
    			
    			
    			$('#jform-prev-next .status').html($('#page_string').text().replace('%num%', options.currSlide+1).replace('%numOfPage%', numberOfPage))
    			//console.log(options.currSlide+'=>'+options.nextSlide);
    		}
    		
		});
	};
	
	$.fn.jform_setTips = function(){
		this.filter('input[type="text"]').each(function(){
			var temp = $(this);
			var tips = $("label[for='"+temp.attr("id")+"'].tips");
			if(tips.length > 0){
				temp.wrap('<div/>').after(tips);
				//var label = temp.clone().removeAttr('id').removeAttr('name').attr('readonly',true).attr('value',tips.text())
				label = $('<a href="javascript:void(0)" class="'+temp.attr('class')+'"><span>'+tips.text()+'</span></a>');
				label.width(temp.width());
				tips.html(label);
				if(!Boolean(temp.attr("value"))){
					temp.hide();
					tips.show();
				}else{
					tips.hide();
					temp.show();
				}
				label.click(function(e){
					e.preventDefault()
					$(this).parent().hide();
					temp.show();
					temp.focus()
				})
				label.focus(function(e){
					e.preventDefault()
					$(this).click()
				})
				temp.blur(function(){
					if(!Boolean($(this).attr("value"))){
						$(this).hide();
						tips.show();
					}else{
						$(this).show();
						tips.hide();
					}			
				})
			}
		})
	};
})(jQuery);


