function linksToggle(contentId){
    this.contentId = contentId;
    this.content = null;
    this.contentImg = null;
    this.buttons = null;
    this.items = null;
    this.step = 0;
    this.ti = null;
    this.time = 8000;

    this.init = function(){
        if(this.items == null) return;
        this.content = $('#'+this.contentId+'-Content');
        this.buttons = $('#'+this.contentId+'-Buttons');
        $(this.buttons).html('');
        var _this = this;
        var o = this.buttons.get(0);
        var len = this.items.length;
        if(len > 1){
            for(var i=0;i<len;i++){
                var a = document.createElement('a');
                a.setAttribute('index', i);
                a.onclick=function(){
                    _this.load(this.getAttribute('index'));
                }
                o.appendChild(a);
            }
        } else {
            this.time = 0;
        }
        this.load(0);
    };

    this.next = function() {
        this.step++;
        if(this.step >= this.items.length) this.step = 0;
        this.autoLoad(this.step);
    };

    this.autoLoad = function(index){
        clearInterval(this.ti);
        this.content.css('background-image', 'url('+this.items[index].src+')');
        $(this.content).attr('href', this.items[index].url);
        if(this.time > 0){
            //"zapalenie" klawisza
            this.buttons.children().removeClass('on');
            this.buttons.children('a[index="'+index+'"]').addClass('on');
            var _this = this;
            this.ti = self.setInterval(function(){
                _this.next();
            }, this.time);
        }
    };

    this.load = function(index){
        this.step = index;
        this.autoLoad(index);
    };

    this.add = function(_src, _url) {
        if(this.items == null) this.items = new Array();
        var i = this.items.length;
        this.items[i] = {
            src: _src,
            url: _url
        };
    };
}

var Optimist = {
  
    searchTitle: 'Napisz czego szukasz',
    newsletterTitle: 'Tutaj wpisz swój e-mail',
    searchInput: null,
    newsletterInput: null,
    loginInput: null,
    newWindow: null,
    isWin: false,

    init: function(){
        this.isWin = navigator.platform.indexOf('win')!=-1?1:0;
        this.searchInput = $('#szukaj input.inp').focus(function(){
            $(this).val('');
        }).blur(function(){
            if($(this).val().length == 0) $(this).val(Optimist.searchTitle);
        }).keypress(function(event) {
            if (event.keyCode == 13) {
                event.preventDefault();
                Optimist.seek();
            }
        }).val(Optimist.searchTitle);
    
        this.loginInput = $('#login input').keypress(function(event) {
            if (event.keyCode == 13) {
                event.preventDefault();
                document.forms['login'].submit();
            }
        });
    
        this.newsletterInput = $('input#newsletter').focus(function(){
            $(this).val('');
        }).blur(function(){
            if($(this).val().length == 0) $(this).val(Optimist.newsletterTitle);
        }).keypress(function(event) {
            if (event.keyCode == 13) {
                event.preventDefault();
                Optimist.newsletter();
            }
        }).val(Optimist.newsletterTitle);
    
    },

    newsletter: function() {
        if(this.newsletterInput.val() != this.newsletterTitle
            &&  /^([A-Za-z0-9!#%&`_=\/\$\'*+?^{}|~.-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/i.test(this.newsletterInput.val())
            ) {
            $.post('/newsletter/'+this.newsletterInput.val(), null,
                function(data) {
                    alert(data);
                });            
        } else {
            alert('Proszę podać prawidłowy adres e-mail!');
        }
    },
  
    url: function(link){
        window.location.href = link;
    },

    seek: function() {
        if(this.searchInput.val() != this.searchTitle
            &&  /^([^$\/;\'\\|&!?*()]{2,80})$/.test(this.searchInput.val())
                )
            this.url('/szukaj/'+this.searchInput.val());
    },
  
    popup: function(width, height, url) {
        var left = (screen.width/2)-(width/2);
        var top = (screen.height/2)-(height/2) - 30;    
        this.newWindow = window.open(url,'',
            'left='+left+',top='+top+
            ',width='+width+',height='+height+
            ',scrollbars=1,resizable=0');
        if (window.focus) {
            this.newWindow.focus();
        }
    },
    
    showGallery: function(type, id, file_id) {
        if(typeof(file_id)=='undefined') file_id = 0;
        var index = 0;
        $.get('/multimedia/galeria/'+type+'/'+id, function(data) {
            var obj = jQuery.parseJSON(data);
            //sprawdzenie id obrazka i wystartowanie galerii od konkretnego indeksu
            if(file_id > 0){
                for(var i in obj) {
                    if(obj[i]['file_id'] == file_id) {
                        index = i;
                    }
                }
            }
            if(obj.length > 0)
                $.fancybox(obj, {
                    'index': index,
                    'padding' : 0,
                    'transitionIn' : 'none',
                    'transitionOut' : 'none',
                    'type' : 'image',
                    'changeFade' : 100
                });          
        });        
    }    

}


var AutoStart = {
    run_after_ready_fn: null,
    add: function(fn) {
        if(this.run_after_ready_fn == null) this.run_after_ready_fn = new Array();
        var i = this.run_after_ready_fn.length;
        this.run_after_ready_fn[i] = fn;
    },
    run: function(){
        if(this.run_after_ready_fn==null) return ;
        var l = this.run_after_ready_fn.length;
        for(var i=0; i<l; i++){
            this.run_after_ready_fn[i]();
        }
    }
}

$(document).ready(function() {
    $.datepicker.setDefaults($.extend({
        showMonthAfterYear: false
    }, $.datepicker.regional['pl']));
    Optimist.init();
    AutoStart.run();
    $('.item .kgBigPhoto').fancybox({
        'padding' : 0,
        'transitionIn' : 'none',
        'transitionOut' : 'none',
        'type' : 'image',
        'changeFade' : 100
    });
    
    $('.item3 .kgBigPhoto').fancybox({
        'padding' : 0,
        'transitionIn' : 'none',
        'transitionOut' : 'none',
        'type' : 'image',
        'changeFade' : 100
    });
    
    var $imgList = $('#news div.content img');
    var j = 0, imgGal = [];
    $imgList.each(function(){
        var el = $(this);
        var src = el.attr('src');
        if(src.search('kugawa')==-1) return;
        el.attr('index', j);
        el.css('cursor','pointer');
        imgGal[j] = {
          'file_id': j,
          'href':  src.replace(/(\/\d+[xXwWfFhH]\d+)/i,"/700f500")
        };
        el.click(function(){
            $.fancybox(imgGal, {
                'index': el.attr('index'),
                'padding' : 0,
                'transitionIn' : 'none',
                'transitionOut' : 'none',
                'type' : 'image',
                'changeFade' : 100
            });
        });
        j++;
    });

//    $imgList.click(function(){
//        var el = $(this);
//        var src = el.attr('src');
//        if(src.search('kugawa')==-1) return;
//        $.fancybox({
//            'href': src.replace(/(\/\d+[xXwWfFhH]\d+)/i,"/700f500"),
//            'padding' : 0,
//            'transitionIn' : 'none',
//            'transitionOut' : 'none',
//            'type' : 'image',
//            'changeFade' : 100
//        });
//    });
  
});

