var Suwak = {
    mouse		: false,
    left		: 0,
    pageWidth	: 0,
    ratio		: 1.333333333333,

    init: function() {
        if (!document.getElementById('suwak') || !document.getElementById('pole_suwak')) {
            alert('Brak zdefiniowanego suwaka');
            return false;
        }

        document.onmouseup		= Suwak.mouseUp;
        document.onmousemove	= Suwak.mouseMove;
        $('#suwak').mousedown(Suwak.mouseDown);
        Suwak.left  			= parseInt($('#suwak').css('left'));
        Suwak.pageWidth			= jQuery(document).width() - 300;
    },

    mouseDown: function(e) {
        Suwak.mouse = true;
        Suwak.start = document.all ? window.event.clientX : e.pageX;
    },

    mouseUp: function() {
        Suwak.mouse = false;
        Suwak.left  = parseInt($('#suwak').css('left'));
    },

    mouseMove: function(e) {
        if (!Suwak.mouse) return;

        var x = document.all ? window.event.clientX : e.pageX ;
        x =  x - Suwak.start + Suwak.left;
        Suwak.resizeGame(x);
    },

    resizeGame: function(x) {
        if (x < 0) x = 0;
        if (x > 150) x = 150;
        $('#suwak').css('left', x + 'px');
        var p = x / 150;
        var w = parseInt(Suwak.pageWidth * p);
        var h = parseInt(w / Suwak.ratio);
        $('#ramka').css('width', w + 'px');
        $('#ramka').css('height', h + 'px');
        if (w > 831) {
            $('#game_box').css('width', (w + 32) + 'px');
            $('#game td.game_resize').css('width',(w-461+32)+'px');
        } else {
            $('#game_box').css('width', '851px');
            $('#game td.game_resize').css('width','390px');
        }
    },
    
    fitToPage: function() {
        width  = jQuery(window).width();
        height = jQuery(window).height();
        ratio  = width/height;

        if (ratio > Suwak.ratio) {
            h = height - 2;
            w = parseInt(h * Suwak.ratio);
        } else {
            w = width - 2;
            h = parseInt(w / Suwak.ratio);
        }
        
        if (w > 831) {
            $('#game_box').css('width', (w + 32) + 'px');
            $('#game td.game_resize').css('width',(w-461+32)+'px');
        }
        
        $('#ramka').css({
           'width':  w + 'px',
           'height': h + 'px' 
        });
        
        if (ratio > Suwak.ratio) {
            x = 0;
            y = $('#ramka').offset().top;
        } else {
            x = $('#ramka').offset().left;
            y = $('#ramka').offset().top - parseInt((height - h) / 2);
        }
        
        window.scrollTo(x, y);
    }
}


var Voting = {
    value: function(v) {
        $('#glosuj').css('width', 20*v+'%');
    },

    post: function(id,v) {     
        $.ajax({
            url: '/vote/',
            type: 'post',
            cache: false,
            data: 'id='+id+'&vote='+v,
            dataType: 'json',
            error: function(data) {
                alert(data.responseText);
            },
            success: function(data){
                alert(data.message);
            }
        });
    }
}


var Loading = {
    height: null,

    update: function() {
        if ($('#loading_box_progress_bar')) {
            progress = parseInt($('#loading_box_progress_bar').css('width'));

            if (progress < 100) {
                $('#loading_box_progress_bar').css('width', progress+1+'%');
                $('#loading_box_progress_percent').html(progress+1+'%');
                setTimeout(Loading.update, 100);
            } else {
                Loading.showGame();
            }
        }
    },
    
    begin: function() {
        $('#pole_suwak').hide();
        $('#fit_to_page').hide();
        
        Loading.height = parseInt($('#ramka').css('height'));
        
        document.getElementById('ramka').style.height     = '0px';
        document.getElementById('ramka').style.visibility = 'hidden';
        document.write('<iframe id="loading_box" src="http://www.pdr.pl/google_ramka.html" frameborder="0" scrolling="no"></iframe>');
        document.write('<div id="loading_box_progress"><div id="loading_box_progress_bar"></div><div id="loading_box_progress_percent">0%</div></div>');
        document.write('<a id="skip_loading" href="javascript:void(0)" onclick="Loading.showGame()">Ładowanie gry</a>');

        document.getElementById('ramka').onload = function() { Loading.finished(); }
        
        $(document).ready(Loading.update);
    },

    finished: function() {
        $('#skip_loading').html('Gra została załadowana, kliknij aby rozpocząć');
    },

    showGame: function() {
        $('#loading_box').remove();
        $('#loading_box_progress').remove();
        $('#skip_loading').remove();

        $('#ramka').css('height', Loading.height+'px');
        $('#ramka').css('visibility', 'visible');
        
        $('#pole_suwak').show();
        $('#fit_to_page').show();
    }
}

