/*
 * jTimepicker plugin 1.4.1
 *
 * http://www.radoslavdimov.com/jquery-plugins/jquery-plugin-jtimepicker/
 *
 * Copyright (c) 2009 Radoslav Dimov
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * Depends:
 *      ui.core.js
 *      ui.slider.js
 */

(function($) {
	var is4duration = false;
	var durationHDisp = 1;
	var durationMDisp = 0;
    $.fn.extend({

        jtimepicker: function(options) {
            
            var defaults = {
                clockIcon: 'images/icon_clock_2.gif',
                orientation: 'horizontal',
                // set hours
                hourCombo: 'hourcombo',
                hourMode: 24,
                hourInterval: 1,
                hourDefaultValue: 0,
                hourSlider: 'hourSlider',
                hourLabel: 'hour',
                
                // set minutes
                minCombo: 'mincombo', 
                minLength: 60,
                minInterval: 5,
                minDefaultValue: 0,
                minSlider: 'minSlider',
                minLabel: 'min',
                
                // set seconds
                secView: true,
                secCombo: 'seccombo',
                secLength: 60,
                secInterval: 5,
                secDefaultValue: 0,
                secSlider: 'secSlider',
                secLabel: 'sec'
                //valueid:'toto'
              
            };

            var options = $.extend(defaults, options);
            
            return this.each(function() {
				
                var html = '';
                var o = options;
                var slideWrapperId = "sliderWrap"+o.secLabel;
                var $this = $(this);
				
                var orientation = (o.orientation == 'horizontal') ? 'auto' : 'vertical';
				// for duration

                var sliderData = [
                                    {'label':o.hourLabel, 'slider':o.hourSlider, 'combo':o.hourCombo},
                                    {'label':o.minLabel, 'slider':o.minSlider, 'combo':o.minCombo}
                                    ];
               
                if(typeof($this[0].id) !== "undefined" && $this[0].id == "durationpicker"+o.secLabel){
             
                	html += $this.createInputDuration(o.hourCombo,"", o.hourDefaultValue);
                	html += $this.createInputDuration(o.minCombo,"", o.minDefaultValue);					
	                is4duration = true;
	                
                }else{ 
                	
                	 is4duration = false;
                	html += $this.createCombo(o.hourCombo, o.hourMode, o.hourInterval, o.hourDefaultValue);
                	html += $this.createCombo(o.minCombo, o.minLength, o.minInterval, o.minDefaultValue);					

                }
             	
				if(is4duration)
					slideWrapperId = "durationSlideWrap"+o.secLabel;
				
				
                if (o.secView) {
                    sliderData.push({'label':o.secLabel, 'slider':o.secSlider, 'combo':o.secCombo});
                    html += $this.createCombo(o.secCombo, o.secLength, o.secInterval, o.secDefaultValue);

                }
                html += '<img src="' + o.clockIcon + '" class="clock" />';
                html += $this.createSliderWrap(sliderData,slideWrapperId);
                $this.html(html);
               
                $('#'+slideWrapperId).addClass(orientation);
                 $('#'+slideWrapperId).addClass("durationSlideWrap");
                 
                $this.createSlider(o.hourSlider, o.hourMode, o.hourCombo, o.hourInterval, o.hourDefaultValue, o.orientation,o.secLabel,is4duration);
               	
                $this.createSlider(o.minSlider, o.minLength, o.minCombo, o.minInterval, o.minDefaultValue, o.orientation,o.secLabel,is4duration);
                
                if (o.secView) {
                    $this.createSlider(o.secSlider, o.secLength, o.secCombo, o.secInterval, o.secDefaultValue, o.orientation,o.secLabel,is4duration);
                }
				
				 
                $.each(sliderData, function(i, item) {
                	
                    $('.' + item.combo).change(function() {
                        var val = $(this).val();
                        $('.' + item.slider).slider('option', 'value', val);
                    });
                });  
                
				if(is4duration)
                $("#idDuree"+o.secLabel).click(function(){
      					
                		$this.find("#durationSlideWrap"+o.secLabel).toggle(
                			function(){
                		
		                         $(document).click(function(event) {
                           			if (!( $(event.target).is('#idDuree'+o.secLabel) || $(event.target).is('#'+slideWrapperId) || $(event.target).parents('#'+slideWrapperId).length || $(event.target).is('.clock'))) 
		                                $this.find("#durationSlideWrap"+o.secLabel).hide(500);
		                                
		                            }
		                        );
		                				
                				
                			}
                		);
                		 
                });              
                					
                $this.find('.clock').click(function() {
                    
                    $this.find('#'+slideWrapperId).toggle(function() {
                    	
                        $(document).click(function(event) {
                            if (!( $(event.target).is('#idDuree'+o.secLabel) || $(event.target).is('#'+slideWrapperId) || $(event.target).parents('#'+slideWrapperId).length || $(event.target).is('.clock'))) {
                                $this.find('#'+slideWrapperId).hide(500);
                                
                            }
                        });
                    });
                     
                });              
            }); 
        }     
    });

    $.fn.createCombo = function(id, length, interval, defValue) {
    	
        var html = '<select class="' + id + ' combo" id="' + id + '">';
        for(i = 0; i < length; i += interval) {
            var selected = i == defValue ? ' selected="selected"' : '';
            var txt = i < 10 ? '0' + i : i;
            html += '<option value="' + i + '"' + selected + '>' + txt + '</option>';
        }
        html += "</select>";

        return html;
    }
    $.fn.createInputDuration = function(id,className, defValue) {
        var html = '<input style="display:none;" id="' + id + '" type="text" value="'+defValue+'">';
         html += "</input>";

        return html;
    }
    $.fn.createSliderWrap = function(data,id) {
    	
        var html = '<div id="'+id+'">';
        $.each(data, function(i, item) {
       		
            html += '   <div><label>' + item.label + ':</label> <p class="' + item.slider + '"></p></div>';
        });
            html += '</div>';
		
        return html;
    }
    
    $.fn.createSlider = function(id, maxValue, combo, stepValue, defValue, orientation,label,_is4duration) {
        var $this = $(this);
       
        $this.find('.' + id).slider({
            orientation: orientation,
            range: "min",
            min: 0,
            max: maxValue - stepValue,
            value: defValue,
            step: stepValue,
            animate: true ,
            slide: function(event, ui) {
            	
            	try{
            		
            		if(_is4duration){
            			
						if(combo === "durationH"+label)
							durationHDisp = ui.value;
						else if(combo === "durationM"+label)
							durationMDisp = ui.value;
							
					$("#"+combo)[0].value = ui.value;
					if(durationMDisp < 10)
						$("#idDuree"+label)[0].value = durationHDisp+"h"+" 0"+durationMDisp;
            		else	
						$("#idDuree"+label)[0].value = durationHDisp+"h"+" "+durationMDisp; 
            		}
            		else
            		$this.find('.' + combo).val(ui.value);
            	}catch(err){}
                
            }            
        });
    }


})(jQuery);

