/****************************************************** 
    Title:   Wizzard 
    Author:  Wizzard 
******************************************************/

function Wizzard(){}

//Wizzard Slider
Wizzard.prototype.Slider = function(){};

//Wizzard Mask
Wizzard.prototype.Mask = function(){};

//Wizzard Gallery
Wizzard.prototype.Gallery = function(){};

//Wizzard Custom Select
Wizzard.prototype.CustomSelect = function(obj, options)
{
    //The CustomSelect class
    function CustomSelect(obj, options)
    {
        //The default options
        this.defaults = {
            theme: 'oringe',
            animation: 'pop',
            scrollable: true,
            disable_first_elem: false        
        };
        
        //Extend the default options
        this.options = $.extend({}, this.defaults, options);
        
        //Elements Number of the obj
        this.elements_number = obj.children().length;
        
        //Array with the elements of the obj
        this.elements = new Array();
        
        //Object name
        this.obj_name = obj.attr('name');
        
        //Object value
        this.obj_value = obj.val();
        
        //Object width
        this.obj_width = obj.width();
        
        //Assign the scroller height  
        this.scroller_height = 250;
        
        //Select Wrapper
        this.sel_wrap = sel_wrap = $('<div />').attr({
            'class':'ctm_sel '+ this.options.theme
        }).css({
            width: obj.css('width'),
            margin: obj.css('margin')
        });
        
        //Hidden Input
        this.hid_ipt = $('<input />').attr({
            'type': 'hidden',
            'name': this.obj_name,
            'value': this.obj_value
        });
        
        //Select Placeholder
        this.plc_hld = $('<div />').attr({'class': 'ctm_content'}).html('<span style="width:'+ (this.obj_width-4) +'px"></span>').css('width', this.obj_width-5);
        
        //Arrow Button
        this.arr_btn = $('<a />').attr({
            href: 'javascript:;'
        })
        
        for(var i = 0; i<this.elements_number; i++) {
            var option = new Object();
            
            option.value = obj.children().eq(i).attr('value');
            option.html = obj.children().eq(i).html();
            
            if(obj.children().eq(i).attr('selected') == true) {
                option.selected = true;
				this.selected_text = obj.children().eq(i).text();
            }
                
            if(obj.children().eq(i).attr('disabled') == true) 
                option.disabled = true;
                
            this.elements.push(option);
        }
		
		this.plc_hld.children().text(this.selected_text ? this.selected_text : this.elements[0].html);
        
        //Create the ul wrapper
        this.ul_wrap = $('<div />').attr({
            'class': 'ctm_sel_ul_wrap'
        }).css({
            'padding-top': '2px',
             'width': (this.obj_width+4)+'px'
        })
        
        //Append the new custom select
        this.ctm_sel_ul = $('<ul />').css({
            'width': (this.obj_width+4)+'px'
        });
        
        for(var i=0;i<this.elements_number;i++) {
            var dis = '';
            if(this.elements[i].disabled) {
                dis = ' class="disabled"';
            }
            if(this.elements[i].selected) {
                dis = ' class="hover"';
            }
            this.ctm_sel_ul.append('<li'+ dis +' title="' + this.elements[i].value + '">' + this.elements[i].html + '</li>');
        }
        
        this.ul_wrap.append(this.ctm_sel_ul)
        
        //Insert the custom select after the original
        obj.after(this.sel_wrap);
        sel_wrap.append(this.hid_ipt).append(this.ul_wrap).append(this.plc_hld).append(this.arr_btn);
        
        if(this.options.scrollable && this.ul_wrap.height()>this.scroller_height) {
          this.ul_wrap_orig_height = this.ul_wrap.height();
          
          //Assign the scroller arrows height
          this.scroller_arrows_height = 10;
          
          this.ctm_sel_ul.children('li').css('margin-right', this.scroller_arrows_height)
          
            this.ul_wrap.css({
                'overflow': 'hidden',
                'max-height': this.scroller_height
            })
            
            this.ctm_sel_ul.css({
                'overflow': 'hidden',
                'max-height': this.scroller_height-5
            })
        }
        
        //Remove the select element
        obj.remove();
    };

    CustomSelect.prototype.EventsListener = function()
    {
        var list_wrap = this.ul_wrap;
        var list = this.ctm_sel_ul;
        
        $(document).click(hideList);
        this.sel_wrap.click(toggleList);
        
        list.children().hover(function() {
            if(!($(this).hasClass('disabled'))) {
                $(this).siblings().removeClass('hover');
                $(this).addClass('hover');
            }
        })
        
        var input = this.hid_ipt;
        var plc = this.plc_hld;
        var child_elem = this.elements_number;
        
        list.children().click(function(e) {
            if($(this).hasClass('disabled')) {
                e.stopPropagation();
                return false;
            }
            input.val($(this).attr('title'));
            plc.children().html($(this).html())
            $(this).addClass('hover')
        })
        
        var is_scrollable = this.options.scrollable && (this.ul_wrap_orig_height > this.scroller_height);
        
        //Add custom scroll in the list
        if(is_scrollable) {
          
            //Assign the scroller slider max height
            var scroller_slider_max_height = this.scroller_height-(5+2*this.scroller_arrows_height);
          
            //Assign the koeficient of the real height of the container and height of the scroller slider
            var koef = this.ul_wrap_orig_height / this.scroller_height;

            //Assign the real slider height
            var scroller_slider_height = (1/koef) * scroller_slider_max_height ;
            
            //Assign how much to scroll the container 
            var scroll_amount = (this.ul_wrap_orig_height - this.scroller_height) / (7*koef);

            //Assing how much to scroll the scroll slider
            var scroll_slider_step = (scroller_slider_max_height - scroller_slider_height) / (7*koef);
            
            //Assign local variable to the arrows height
            var arrow_height = this.scroller_arrows_height
            
            var scroller = $('<div />').css({
                'height': this.scroller_height-5
            }).attr('class', 'ctm_scroller');
            
            var scroller_top = $('<div />').css({
                'height': this.scroller_arrows_height
            }).attr('class', 'ctm_scroller_top_arrow');
            
            var scroller_slider = $('<div />').css({
                'top':  arrow_height,
                'height': scroller_slider_height
            }).attr('class', 'ctm_scroller_slider');
            
            var scroller_bottom = $('<div />').css({
                'height': this.scroller_arrows_height
            }).attr('class', 'ctm_scroller_bottom_arrow'); 
            
            scroller.append(scroller_top).append(scroller_slider).append(scroller_bottom);
            list_wrap.append(scroller);
            
            function scroll_down(e)
            {
                e.stopPropagation();
                var current_scroll_top = list.scrollTop();
                list.scrollTop(current_scroll_top+scroll_amount);
                
                var scroller_slider_top = parseFloat(scroller_slider.css('top'));
                var scr_top = scroller_slider_top+scroll_slider_step;

                if(scr_top > (scroller_slider_max_height+arrow_height)-scroller_slider_height)
                  scr_top = (scroller_slider_max_height+arrow_height)-scroller_slider_height;
                
                if($.browser.msie && parseInt($.browser.version) < 8)
                    list.hide().show();
                    
                scroller_slider.css('top', scr_top + 'px');
            }
            
            function scroll_up(e)
            {
              e.stopPropagation();
                var current_scroll_top = list.scrollTop();
                list.scrollTop(current_scroll_top-scroll_amount);
                
                var scroller_slider_top = parseFloat(scroller_slider.css('top'));
                var scr_top = scroller_slider_top-scroll_slider_step;
                if(scr_top < arrow_height)
                  scr_top = arrow_height;
                  
                if($.browser.msie && parseInt($.browser.version) < 8)
                    list.hide().show();
                  
                scroller_slider.css('top', scr_top + 'px');
            }
            
            function reset_scroller()
            {
              list.scrollTop(0);
              scroller_slider.css('top', arrow_height + 'px');
            }
            
            //Scroll down by clickig the down arrow
            scroller_bottom.mousedown(function(e) {
                var scr_dn = setInterval(function() {
                    scroll_down(e);
                }, 100);
                
                scroller_bottom.mouseup(function() {
                    clearInterval(scr_dn)
                })
            });
            
            scroller_bottom.click(function(e) {
                e.stopPropagation();
                scroll_down(e);
            })
            
            //Scroll up by clickig the up arrow
            scroller_top.mousedown(function(e) {
                var scr_dn = setInterval(function() {
                    scroll_up(e);
                }, 100);
                
                scroller_top.mouseup(function() {
                    clearInterval(scr_dn)
                })
            });
            
            scroller_top.click(function(e) {
                e.stopPropagation()
                scroll_up(e);
            });
            
            scroller.click(function(e) {
              e.stopPropagation();
            })
            
            //Mousewheel efects
            list.bind('mousewheel', function(event, delta) {
              if(delta > 0) {
                  scroll_up(event);
              }
              else if(delta < 0) {
                  scroll_down(event);
              }
            })
            
            //Mouse drag events
            scroller_slider.mousedown(function(e){
              if (e.preventDefault) {
                  e.preventDefault();
              }
              //The current Y position of the mouse
              var mouse_y_pos = e.pageY;
              
              //The current position of the scroller slider
              var scroller_slider_top = parseFloat(scroller_slider.css('top'));
              
              
              //The current scroll top of the content              
              var current_scroll_top = list.scrollTop();
              
              //scroller_slider
              $(window.document).mousemove(function(ee) {
                if(ee.pageY > mouse_y_pos) {
                  var mouse_diff = ee.pageY - mouse_y_pos;
                  list.scrollTop(current_scroll_top + (mouse_diff*(scroll_amount/scroll_slider_step)));
                  
                  var scr_top = scroller_slider_top+(mouse_diff);
                  if(scr_top > (scroller_slider_max_height+arrow_height)-scroller_slider_height)
                    scr_top = (scroller_slider_max_height+arrow_height)-scroller_slider_height;
                  
                  if($.browser.msie && parseInt($.browser.version) < 8)
                    list.hide().show();
                    
                  scroller_slider.css('top', scr_top + 'px');
                }
                if(ee.pageY < mouse_y_pos) {
                  var mouse_diff =  mouse_y_pos - ee.pageY;
                  list.scrollTop(current_scroll_top - (mouse_diff*(scroll_amount/scroll_slider_step)));
                  
                  var scr_top = scroller_slider_top-(mouse_diff);
                  if(scr_top < arrow_height)
                    scr_top = arrow_height;
                  
                  if($.browser.msie && parseInt($.browser.version) < 8)
                    list.hide().show();
                    
                  scroller_slider.css('top', scr_top + 'px');
                }
              })
            })
            
            $(window.document).mouseup(function(e) {
              $(window.document).unbind('mousemove');
            })
            
            scroller_slider.click(function() {
                return false;
            })
            
            scroller.click(function(e) {
                var scroller_offset = scroller.offset().top;
                
                var scroll_to  = (e.pageY - scroller_offset) - (scroller_slider_height/2);
                if(scroll_to < arrow_height)
                    scroll_to = arrow_height;
                if(scroll_to > (scroller_slider_max_height+arrow_height)-scroller_slider_height)
                    scroll_to = (scroller_slider_max_height+arrow_height)-scroller_slider_height;
                
                scroller_slider.css('top', scroll_to + 'px');
                list.scrollTop((scroll_to-arrow_height)*(scroll_amount/scroll_slider_step));
                
                if($.browser.msie && parseInt($.browser.version) < 8)
                    list.hide().show();
            })
        }
        
        var show_effect = this.Effects[this.options.animation];
        var hide_effect = this.Effects['un'+this.options.animation];
        var options = this.options;
        
        var current_object = this;
        
        function toggleList(e)
        {
            e.stopPropagation();
            if(list_wrap.hasClass('expanded')) {
                eval("list_wrap."+hide_effect+".removeClass('expanded')");
            }
            else {
                if(options.disable_first_elem)
                    list.children(":first").addClass('disabled');
                
                eval("list_wrap."+show_effect+".addClass('expanded')");
                
                for(var i in custom_selects) {
                    if(custom_selects[i] === current_object) {
                        custom_selects[i].sel_wrap.css('z-index', 50);
                        custom_selects[i].plc_hld.css('z-index', 60);
                    }
                    else {
                        custom_selects[i].sel_wrap.css('z-index', 10);
                        custom_selects[i].plc_hld.css('z-index', 0);
                    }
                }
                  
                for(var a in custom_selects) {
                    if((custom_selects[a].ul_wrap !== list_wrap) && custom_selects[a].ul_wrap.hasClass('expanded')) {
                        eval("custom_selects[a].ul_wrap."+hide_effect+".removeClass('expanded')");
                    }
                }
            }
            return false;
        }
        
        function hideList()
        {   
            eval("list_wrap."+hide_effect+".removeClass('expanded')");
        }
        
    };

    CustomSelect.prototype.Effects = {
        pop: 'show()',
        unpop: 'hide()',
        fade: 'fadeIn()',
        unfade: 'fadeOut()',
        slide: 'slideDown(300)',
        unslide: 'slideUp(300)'
    }

    //Initiates the new selects
    var obj = $(obj).not(':[multiple]');
    var custom_selects = new Array();
    
    obj.each(function(index, value) {
        var sel = new CustomSelect($(this), options);
        sel.EventsListener();
        custom_selects.push(sel);
    })
};

//Wizzard Custom Select
Wizzard.prototype.CustomCheckbox = function(obj, options)
{
    //The CustomCheckbox class
    function CustomCheckbox(obj, options)
    {
        //The default options
        this.defaults = {
            theme: 'default'
        };
        
        //Extend the default options
        this.options = $.extend({}, this.defaults, options);
        
        //Assign the obj name 
        this.obj_name = obj.attr('name');
        
        //Assign the obj value
        this.obj_value = obj.val();
        
        //Assign if the checkbox is checked
        this.is_checked = obj.is(':checked');
        
        //Create the checkbox wrapper
        this.checkbox_wrap = $('<div />').attr('class', 'ctm_checkbox');
        
        //Create the hidden input of the checkbox
        this.hidden_input = $('<input />').attr({
            'type': 'hidden',
            'name': this.obj_name,
            'value': this.obj_value
        });
        
        //Create the checkbox
        this.checkbox = $('<a />').attr({
            'href': 'javascript:;',
            'title': this.obj_name
        });
        
        //Put the new elements in the DOM
        obj.after(this.checkbox_wrap);
        this.checkbox_wrap.append(this.checkbox);
        
        //Remove the origianl checkbox
        obj.remove();
    }
    
    CustomCheckbox.prototype.EventsListener = function() 
    {
        this.checkbox.click(toggle_check);
        var checkbox = this.checkbox;
        var checkbox_wrap = this.checkbox_wrap;
        var hidden_input = this.hidden_input;
        
        function toggle_check()
        {
            if(checkbox.hasClass('checked')) {
                checkbox.removeClass('checked');
                hidden_input.remove();
            }
            else {
                checkbox.addClass('checked');
                checkbox_wrap.append(hidden_input);
            }
        }
    }
    
    //Initiates the new checkboxes
    var obj = $(obj);
    var custom_checkboxes = new Array();
    
    obj.each(function() {
        var sel = new CustomCheckbox($(this), options);
        sel.EventsListener();
        custom_checkboxes.push(sel);
    });
}

//Wizzard Custom Select
Wizzard.prototype.CustomRadioButton = function(obj, options)
{
    //The CustomCheckbox class
    function CustomRadioButton(obj, options)
    {
        //The default options
        this.defaults = {
            theme: 'default'
        };
        
        //Extend the default options
        this.options = $.extend({}, this.defaults, options);
        
        //Assign the obj name 
        this.obj_name = obj.attr('name');
        
        //Assign the obj value
        this.obj_value = obj.val();
        
        //Assign if the checkbox is checked
        this.is_checked = obj.is(':checked');
        
        //Create the checkbox wrapper
        this.radio_btn_wrap = $('<div />').attr('class', 'ctm_radio_btn');
        
        //Create the hidden input of the checkbox
        this.hidden_input = $('<input />').attr({
            'type': 'hidden',
            'name': this.obj_name,
            'value': this.obj_value
        });
        
        //Create the checkbox
        this.radio_btn = $('<a />').attr({
            'href': 'javascript:;',
            'title': this.obj_name
        });
        
        //Put the new elements in the DOM
        obj.after(this.radio_btn_wrap);
        this.radio_btn_wrap.append(this.radio_btn);
        
        //Remove the origianl checkbox
        obj.remove();
    }
    
    CustomRadioButton.prototype.EventsListener = function() 
    {
        this.checkbox.click(toggle_check);
        var checkbox = this.checkbox;
        var checkbox_wrap = this.checkbox_wrap;
        var hidden_input = this.hidden_input;
        
        function toggle_check()
        {
            if(checkbox.hasClass('checked')) {
                checkbox.removeClass('checked');
                hidden_input.remove();
            }
            else {
                checkbox.addClass('checked');
                checkbox_wrap.append(hidden_input);
            }
        }
    }
    
    //Initiates the new checkboxes
    var obj = $(obj);
    var custom_radiobuttons = new Array();
    
    obj.each(function() {
        var sel = new CustomRadioButtons($(this), options);
        sel.EventsListener();
        custom_radiobuttons.push(sel);
    });
}

var Wizzard = new Wizzard();

