Cufon.replace('.name2 h1');
Cufon.replace('.name h3');
$(document).ready(function(){

    $("#CountryCode").jqDropDown({
        defaultStyle: false
    });


    //slider kroków na głównej
    $('.steps-slider').sliderFollow();
    $('#steps-content ul').sliderScreens();
    //logowanie i formularz kontaktowy
    $('#username, #password, .footer-container input.text, .footer-container textarea').placeholder();
    //rozwijanie panelu na głównej
    $('#sign-slide-container a').click(function(){
        $('#sign-wrapper').slideToggle('slow');
        return false;
    });

    $.channelPanel({
        firstList: '#available-list',
        secondList: '#enable-list'
    });

    //scrollery
    $('.rules-frame').verticalSlider();
    $('#available-list .list-container').verticalSlider();
    $('#enable-list .list-container').verticalSlider();
    
    //taby w Moim Koncie
    if($('body').hasClass('my-account')){
        //boxy paneli
        $panels = $('.my-account .right>div:not(.bg-bottom,.breadcrubs)');
        hash = $.getHash($.getURI());
        //schowaj wszystkie panele
        $panels.hide();
        //pokaz hasha lub pierwszy z kolei
        if(hash)
            $(hash).show();
        else
            $panels.first().show();

        //odnosniki kategorii
        $('.my-account #content .left li a').click(function(){
            $panel = $($.getHash($(this).attr('href')));
            if($panel.length){
                $panels.hide();
                $panel.show();
            }
            return false;
        });
    }
    //FAQ
    if($('body').hasClass('faq')){
        $('.questions li a').click(function(){
            $(this).parent().children('.answer').slideToggle('slow');
            return false;
        });
    }


    $('.product-options .basic, .product-options .premium').click(function(){
        $('.product-options .basic, .product-options .premium').removeClass('checked');
        $(this).addClass('checked').find(":radio").attr('checked', true);
    });

});

Fuero = function(options){
    init = function(){
        //extends jQuery with Fuero plugins
        jQuery = $.extend(jQuery, core);
        jQuery.fn = $.extend(jQuery.fn, fn);
        //if console doesn't exist
        if(typeof console === "undefined") console={
            log:function(){}
        };
        $.log('fuero initialized');
    }

    /* jQUERY PLUGINS */
    fn = {
        /* PLUGIN PATERN */
        testPlugin: function(options){
            var defaults = {
                someOption: 'value'
            }
            var settings = $.extend({}, defaults, options);
            return this.each(function(){
                $.log($(this).html());
            });
        },
        /* CONSOLE PLUGIN */
        log: function(msg){
            console.log("%s: %o", msg, this);
            return this;
        },
        /* PLACEHOLDER PLUGIN */
        placeholder: function(){
            return $(this).each(function(index, element){
                $(element).data('initial', $(element).val());
                $(element).addClass('placeholder');

                $(this).focus(function(){
                    var $this = $(this);
                    if($this.val() == $this.data('initial')) $this.removeClass('placeholder').val('');
                });

                $(this).blur(function(){
                    var $this = $(this);
                    if($this.val() == '') $this.addClass('placeholder').val($this.data('initial'));
                });
            });
        },
        sliderScreens: function(){
            var defaults = {
                duration: 300,
                step: 100,
                arrowLeft: $('.arrow-left'),
                arrowRight: $('.arrow-right')
            }
            var settings = $.extend({}, defaults, options);

            $content = $(this);
            elements = $content.children('li').length;
            element_width = $content.children('li:first').width();
            curr = 0;

            settings.arrowRight.click(function(){
                curr = curr<elements-1 ? ++curr : 0;
                margin = -(element_width*curr);
                $content.animate({
                    'margin-left': margin+'px'
                }, 'slow');
                //$.log($content.html());
                return false;
            });
            settings.arrowLeft.click(function(){
                curr = curr==0 ? 2 : --curr;
                $.log(curr);
                margin = -(element_width*curr);
                $content.animate({
                    'margin-left': margin+'px'
                }, 'slow');
                //$.log($content.html());
                return false;
            });
        },
        sliderFollow: function(options){
            var defaults = {
                duration: 300,
                flashContent: '#flash-content',
                contentId: '#steps-content ul',
                contentAction: function(step){
                    step = step.substr(4);
                    //$(settings.contentId).css('margin-left', -(step*129-129));
                    $(settings.contentId).parent().css('z-index', 110);
                    margin = -(step*129-129);
                    $(settings.contentId).animate({
                        'margin-left': margin
                    }, 'slow', function(){
                        $(settings.flashContent).html('');
                    });
                }
            }
            var settings = $.extend({}, defaults, options);

            $(this).each(function(){
                $this = $(this);
                $this.append('<div class="scroll"></div>');
                var $scroll = $this.find('.scroll');
                var $links = $this.find('li>a');
                $links.mouseover(function(){
                    $scroll.animate( {
                        left: $(this).position().left+7
                    }, settings );
                    //action with content
                    settings.contentAction($(this).parent().attr('id'));
                });
                $links.click(function(){return false;})
            })
        }
    }

    /* CORE PLUGIN */
    core = {
        /* CONSOLE PLUGIN */
        log: function(msg){
            console.log("%s", msg);
        },
        /* GET # FROM URI*/
        getHash: function(url){
            if(url === undefined)
                return null;
            if(url.indexOf("#")>-1)
                return url.substr(url.indexOf("#"))
            return null;
        },
        /* GET URI */
        getURI: function(){
            return (document.documentURI || window.location.href);
        },
        channelPanel: function(options){
            var defaults = {
                firstList: null,
                secondList: null,
                expandNode: function(){
                    $(this).parent().toggleClass('expanded').find('>ul').toggle();
                    return false;
                },
                chooseItem: function(){
                    $.log('dzieje sie');
                    $item = $(this).parent();
                    //clone clicked item
                    $itemClone = $item.clone();
                    $itemClone.click(settings.cancelItem);
                    $secondList.find('.list-container ul').append($itemClone);
                    //hide oryginal item and his empty categories
                    $item.hide().addClass('hidden').hideEmptyCategories();
                    $.log('wybrano: '+ $(this).html());
                },
                cancelItem: function(){
                    //znajdz klase odpowiedniego elementu na drugiej liscie
                    classes = $(this).attr('class').split(' ');
                    for (i in classes)
                        if(classes[i].search('item')!=-1)
                            //pokaz wlasciwy element, usun klase 'hidden' i pokaz kategorie, ktore juz nie sa puste
                            $('#available-list .expendable li.' + classes[i]).show().removeClass('hidden').showNonEmptyCategories();
                    $.log('zrezygnowano z: '+ $(this).find('a').html());
                    //usun wybrany kanal
                    $(this).remove();
                    return false;
                },
                hideEmptyCategories: function(){
                    $parent = $(this).parent();
                    if(!$parent.find('li:visible').length){
                        if($parent.hasClass('expendable')){
                            $parent.hide().addClass('hidden').hideEmptyCategories();
                        }else if($parent.parent().hasClass('expendable')){
                            $parent.parent().hide().addClass('hidden').hideEmptyCategories();
                        }
                    }
                },
                showNonEmptyCategories: function(){
                    $parent = $(this).parent();
                    if($parent.find('li:not(.hidden)').length && ($parent.hasClass('expendable') || $parent.parent().hasClass('expendable'))){
                        $parent.show().removeClass('hidden').showNonEmptyCategories();
                    }
                }
            }
            var settings = $.extend(defaults, options);
            jQuery.fn.hideEmptyCategories = settings.hideEmptyCategories;
            jQuery.fn.showNonEmptyCategories = settings.showNonEmptyCategories;
            $firstList = $(settings.firstList);
            $secondList = $(settings.secondList);
            $firstList.find('.expendable a').click(settings.expandNode);
            $firstList.find('.feed a').click(settings.chooseItem);
        }
    }
    //contructor
    init();
    return this;
}
fuero = new Fuero();


jQuery.fn.verticalSlider = function(options){
    var $this = $(this);
    $this.addClass('scroll-wrapper').wrapInner('<div class="scroll-content">').append('<div class="vertical-slider" />');
    var $slider = $this.find('.vertical-slider');
    var $content = $this.find('.scroll-content');

    $slider.height($this.height());
    /*$slider.slider({
        orientation: 'vertical',
        value: 100,
        max: 100,
        slide: function(event, ui){
            if($content.height() > $this.height()){
                $content.css('margin-top', Math.round((100-ui.value)/100 * ($this.height() - $content.height()))+'px');
            }else{
                $content.css( 'margin-top', 0);
            }
        }
    });
	*/
    $handler = $slider.find('.ui-slider-handle');
    $helper = $handler.wrap('<div class="ui-handle-helper" />').parent();
    $content.width($this.width()-$slider.width());
    $helper.height($slider.height()-$handler.height());
    $helper.css('margin-top', $handler.height()+'px');
}
