Pager = function(){
}

Pager.prototype = {
    index: 0,
    visibleCount: null,
    countItems: null,
    fireIndexChanged: null,

    setFireIndexChanged: function(f){
        this.fireIndexChanged = f;
    },

    setVisibleCount: function(visibleCount){
        this.visibleCount = visibleCount;
    },

    setCount: function(count){
        this.countItems = count;
    },

    getCount: function() {
        return this.countItems;
    },

    refresh: function(){
        this.fireIndexChanged.apply(this);
    },

    canMoveBack: function() {
        return this.index-this.visibleCount>=0;
    },

    canMoveForward: function() {
        return this.index+this.visibleCount<this.countItems;
    },

    inc: function(){
        if (this.canMoveForward()) {
            this.index+=this.visibleCount;
            this.fireIndexChanged.apply(this);
        }
    },

    dec: function(){
        if (this.canMoveBack()) {
            this.index-=this.visibleCount;
            this.fireIndexChanged.apply(this);
        }
    }
}

var pager;

function showActionSplash() {
    /*setTimeout(function(){
        $('#wrapper').hide();
        $('#footer').hide();
        var blackscreen = $(document.createElement('div'))
                            .addClass('blackscreen')
                            .css({
                                position: 'absolute',
                                left: 0,
                                top: 0,
                                width: $(document).width(),
                                height: $(document).height(),
                                background: '#fff',
                                opacity: 0.4,
                                cursor: 'pointer',
                                'z-index': 10000
                            })
                            .click(function(){
                                blackscreen.remove();
                                img.remove();
                                $('#wrapper').show();
                                $('#footer').show ();
                                return false;
                            });
        var img = $(document.createElement('img'))
                            .addClass('splash')
                            .attr('src', '/images/action2011.jpg')
                            .css({
                                position: 'absolute',
                                left: ($('body').width()-918)/2,
                                top: 0,
                                cursor: 'pointer',
                                'z-index': 10001
                            })
                            .click(function(){
                                blackscreen.remove();
                                img.remove();
                                $('#wrapper').show();
                                $('#footer').show ();
                                return false;
                            });
        $('body').append(blackscreen);
        $('body').append(img);
        jQuery.cookie('action-showed', 1, {path: '/', expires: 365});
    }, 500);*/
}

$(document).ready(function(){
    $('.tv-button').click(function() {
        //$('.tv').toggleClass('tv-off');
        return false;
    });
    if ($('.tv .items').length>0) {
        pager = new Pager();
        pager.setCount($('.tv .items .items-item').length);
        if (pager.getCount()<4) {
            $('.tv .tv-inner-content-c1 a').css('visibility', 'hidden');
            $('.tv .tv-inner-content-c3 a').css('visibility', 'hidden');
        }
        pager.setVisibleCount(4);
        pager.setFireIndexChanged(function(){
            var fromIndex = this.index;
            var toIndex = this.index+this.visibleCount;
            $('.tv .items .items-item').each(function(){
                var curIndex = $('.tv .items .items-item').index(this);
                if (curIndex<fromIndex) {
                    $(this).hide();
                }
                if (curIndex>=toIndex) {
                    $(this).hide();
                }
                if (curIndex>=fromIndex && curIndex<toIndex) {
                    $(this).show();
                }
            });
            $('.tv .tv-inner-content-c1 a').css('visibility', this.canMoveBack() ? 'visible' : 'hidden');
            $('.tv .tv-inner-content-c3 a').css('visibility', this.canMoveForward() ? 'visible' : 'hidden');
        })
        pager.refresh();
        $('.tv .tv-inner-content-c1 a').click(function(){
            pager.dec();
            return false;
        });
        $('.tv .tv-inner-content-c3 a').click(function(){
            pager.inc();
            return false;
        });
    }
    
    if ($("form.check-age")) {
    	
        $(".custom-select-box a.selector").click(function(e){
            e.preventDefault();
            var el = $(this).parent().find(".select-list");
            $(".select-list").not(el).removeClass("select-list-active");
            $(el).toggleClass("select-list-active");
        });
        $(".select-list a").click(function(e){
            e.preventDefault();
            $(this).addClass("selected-item").parent().parent().find("a").not(this).removeClass("selected-item");
            $(this).parent().parent().parent().find("input[type=hidden]").val($(this).attr("rel")).end().find("a.selector span.value").text($(this).text()).end().find(".select-list").removeClass("select-list-active");
        });
        
	    $("form.check-age").submit(function(){
	        var year = $(this).find("input[name=year]").val();
	        var month = $(this).find("input[name=month]").val();
	        var day = $(this).find("input[name=day]").val();
	        var remember = $(this).find("input[name=remember]").attr("checked");
	
	        if (year == "" || month == "" || day == "") {
	            return false;
	        }
	
	        var year = parseInt(year, 10);
	        var month = parseInt(month, 10);
	        var day = parseInt(day, 10);
	
	        var date = new Date(year, month, day);
	        var tempDate = new Date();
	        var dateNow = new Date(tempDate.getFullYear(), tempDate.getUTCMonth(), tempDate.getUTCDate());
	
	        var years = dateNow.getFullYear()-date.getFullYear()-1;
	
	        date.setYear(dateNow.getFullYear());
	
	        if (date<=dateNow) {
	            years++;
	        }
	        if(years>=18 && years<=100) {
				if(remember == true)  {
					jQuery.cookie('age-check', 1, {path: '/', expires: 365});
				} else {
					jQuery.cookie('age-check', 1, {path: '/', expires: 0});
				}
	            location.replace("/");
	            
	            /*$('#age_check').hide('fast');
	            $('body').removeClass('age_check');
	            $('#wrapper').show();
	            $('#footer').show ();
	
	            if (!actionShowed) {
	                showActionSplash();
	            }*/
	        } else {
	            //$(".l-form-wrapper").html("<p>Оскільки Ви ще не досягли повноліття, Ви не можете відвідати цей сайт.</p><p>Будемо раді Вам у Ваші 18 років.</p>");
	            $(".l-form-wrapper").find('form').remove();
	            $(".l-form-wrapper").find('.wrong-age').show();
	        }
	        return false;
	    });
    }
    
    /*
    var ageChecked = jQuery.cookie('age-check');
    var actionShowed = jQuery.cookie('action-showed');
    if (!ageChecked) {
        $('#wrapper').hide('fast');
        $('#footer').hide('fast');

        $(".custom-select-box a.selector").click(function(e){
            e.preventDefault();
            var el = $(this).parent().find(".select-list");
            $(".select-list").not(el).removeClass("select-list-active");
            $(el).toggleClass("select-list-active");
        });
        $(".select-list a").click(function(e){
            e.preventDefault();
            $(this).addClass("selected-item").parent().parent().find("a").not(this).removeClass("selected-item");
            $(this).parent().parent().parent().find("input[type=hidden]").val($(this).attr("rel")).end().find("a.selector span.value").text($(this).text()).end().find(".select-list").removeClass("select-list-active");
        });


/*        $('#age_check input:text').each(function(){
            var val = $(this).val();
            $(this).click(function(){
                if ($(this).val()==val) {
                    $(this).val('');
                }
            });
            $(this).blur(function(){
                if ($(this).val()=='') {
                    $(this).val(val);
                }
            });
        });*/
        
        //$('#age_check').css('display', 'block');
        //$('body').addClass('age_check');
/*
    } else {
        if (!actionShowed) {
            showActionSplash();
        }
    }*/
});
