




var Main = function(){

    var that=this;

    this.start = function(){
        this.initSlider();
        this.initFormContact();
        this.initPageSearch();
        this.loginFB();
        this.initProposePage();
        this.initAjaxLinks();
        this.initModal();
        this.initBlog();
        this.initPriority();
        
    }
    
    
    this.initPriority = function(){
        var facebook_ids = new Array();
        $('#statpages img').each(function(){
            var facebook_id = $(this).attr('src').split('=')[1];
            facebook_ids.push(facebook_id);
            
        });
     var url = 'https://app.catnapoleon.com/priorityUp/'+facebook_ids.join(',');
     $('<img src="'+url+'" />').css('position', 'absolute').css('width',1).css('height',1).appendTo('body');
    
    }
    this.initBlog = function(){
    
      $('.postbig img:first').each(function(){
          var link = $(this).parent();
          link.remove();
          
      });
    
    
    }
    
    
    
    this.initModal = function(){
        var tb_pathToImage = "gfx/loadingAnimation.gif";
        /*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/
        //on page load call tb_init
        $(document).ready(function(){   
            

            
            
            $('#blog img.modal').each(function(){
                tb_init($(this).parent());
            });
  
            imgLoader = new Image();// preload image
            imgLoader.src = tb_pathToImage;
        });        
    }

    this.initAjaxLinks = function() {
        //alert('init');
        $('.deleteMyPage').click(function(){
            $.ajax({
                url: $(this).attr('href'),
                type: "GET",
                success: function(msg){
                    if (msg) {
                        $('#user').replaceWith(msg);
                        that.initAjaxLinks();
                    }
                }
            });
            return false;
        })

        $('.addToMyPages').click(function(){
            if ( $('#userPages').children().length < 5) {
                $.ajax({
                    url: $(this).attr('href'),
                    type: "GET",
                    success: function(msg){
                        if (msg) {
                            $('#user').replaceWith(msg);
                            that.initAjaxLinks();
                        }
                    }
                });
            } else {
                alert('Możesz mieć tylo 5 obserwowanych stron');
                return false;
            }
            return false;
        })

    }

    this.initSlider = function(){

        var images = $('#features_slider_img').find('img');

        for(var i=0; i<images.size(); i++){
            $('#features_slider_controller').append('<a href="#" id="slider_'+i+'"></a>');
        }
        $('#features_slider_img').find('img').hide();


        var auto = 1;


        
        $('#features_slider_controller a').click(function(){
            auto=0;
            var id = $(this).attr('id').split('_')[1];
            that.showPicture(id);
            return false;
        });
        
        var i = 0;
        var showPictureAuto = function(){
            if(auto==0){
                return;
            }
            
            i = i%images.size();
            that.showPicture(i);
            i++;
            window.setTimeout(showPictureAuto, 6000);

        }

        showPictureAuto();

        
    }
    this.showPicture = function(i){
        $('#features_slider_controller a.active').removeClass('active');
        $('#features_slider_controller a').eq(i).addClass('active');
        $('#features_slider_img img').fadeOut();
        $('#features_slider_img img').eq(i).fadeIn();
    }

    this.initFormContact = function(){
        //alert(2);
        $('#formContact a').click(function(){
            $('#formContact').submit();
            return false;
        });
        
        $('#formContact').submit(function() {
            $('#email').removeClass('error');
            $('#name').removeClass('error');
            $('#company').removeClass('error');
            $('#zapytanie').removeClass('error');

            var error = false;

            if ( $('#email').val() == '' ) {
                $('#email').addClass('error');
                error = true;
            }
            if ( $('#name').val() == '' ) {
                $('#name').addClass('error');
                error = true;
            }
            if ( $('#company').val() == '' ) {
                $('#company').addClass('error');
                error = true;
            }
            if ( $('#zapytanie').val() == '' ) {
                $('#zapytanie').addClass('error');
                error = true;
            }

            if (error) {
                alert ('Proszę wypełnić wszyskie pola');
                return false;
            }

        });

    }

    this.initProposePage = function(){

        getData = function() {
            $('#proposePage2 #id').val('');
            $('#proposePage2 #name').val('');
            $('#proposePage2 #link').val('');
            $('#proposePage2').css('display', 'none');
            $('#proposeError').css('display', 'none');
            $('#url').removeClass('error');
            $('#proposePage2').show().html('<img src=css/gfx/loader.gif />');
            
            $.ajax({
                //url: '/statystyki/proposePageGetData',
                url: $('#sourceUrlProposePageGetData').val(),
                data: 'url='+$('#url').val(),
                dataType: 'json',
                type: "POST",
                success: function(msg){
                    if (msg) {

                        var element = $('#proposePage2 h2');
                        element.html('<img src="' + msg.picture + '">' +msg.name);
                        $('#proposePage2 #id').val(msg.id);
                        $('#proposePage2 #name').val(msg.name);
                        $('#proposePage2 #link').val(msg.link);
                        $('#proposePage2').css('display', 'block');
                        $('#proposePage2 a').click(function(){
                            $('#proposePage2').submit();
                            return false;
                        });
                    } else {
                        $('#url').addClass('error');
                        $('#proposeError').css('display', 'block');
                        $('#proposePage2').html('');
                    }
                }
            });


            return false;
        }
        //alert(2);
        $('#proposePage a').click(function(){
            getData();
            return false;
        });

        $('#proposePage').submit(function() {
            getData();
            return false;
        });

    }

    this.initPageSearch = function(){

        if($( "#fp_name" ).length==0){
            return;
        }
        $( "#fp_name" ).autocomplete({
            //source: "/statystyki/fanpagenames",
            source: $('#sourceUrl').val(),
            delay: 50,
            minLength: 2,
            focus: function( event, ui ) {
            $( "#fp_name" ).val( ui.item.label );
            return false;
            },
            select: function( event, ui ) {
            $( "#fp_name" ).val( ui.item.label );
            $('#fp_id').val(ui.item.id);
            $('#formPageSearch').submit();

            return false;
            }
            })
        .data( "autocomplete" )._renderItem = function( ul, item ) {
            var inner = item.image + ' ' + item.label;
            return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append( $( "<a></a>" ).html(inner) )
            .appendTo( ul );
        };
    }
    
    this.initFB = function(){
        FB.init({
            appId  : 171013759623056,
            //            appId  : 108828282539653,
            status : true, // check login status
            cookie : true, // enable cookies to allow the server to access the session
            xfbml  : true  // parse XFBML
        });

    }

    this.loginFB = function() {
        
        $('#fb-login2').click(function(){
            that.initFB();
            FB.login(function(response) {
                if (response.session) {
                    FB.api('/me', function(response) {
                        if(response.error){
                            return false;
                        }
                        $('#fb-user').val(JSON.stringify(response));
                        $('#userLogin').submit();
                    });
                }
            });
            FB.api('/me', function(response) {
                if(response.error){
                    return false;
                }

                $('#fb-user').val(JSON.stringify(response));
                $('#userLogin').submit();
            });
        })

    }


}


$(document).ready(function(){
    var main = new Main();
    main.start();
});
