(function($) {
    var slotMachine = function() {
        var credits = 0,
            points = 0,
            spinning = 3,
            spin = [17, 17, 17],
            slotsTypes = {
                'cherry': [0, 50, 100, 200, 500],
                'orange': [0, 100, 200, 500, 1000],
                'prune': [0, 100, 200, 500, 1000],
                'bell': [0, 100, 200, 500, 1000],
                'bar1': [0, 200, 500, 1000, 1500],
                'seven': [0, 0, 1200, 1250, 2000]
            },
            slots = [
                ['orange', 'bell', 'orange', 'bar1', 'prune', 'orange', 'bar1', 'prune', 'orange', 'bar1', 'bell', 'cherry', 'orange', 'prune', 'bell', 'bar1', 'cherry', 'seven', 'orange', 'prune', 'orange', 'bell', 'orange'],
                ['orange', 'bell', 'orange', 'bar1', 'prune', 'orange', 'bar1', 'prune', 'orange', 'bar1', 'bell', 'cherry', 'orange', 'prune', 'bell', 'bar1', 'cherry', 'seven', 'orange', 'prune', 'orange', 'bell', 'orange'],
                ['orange', 'bell', 'orange', 'bar1', 'prune', 'orange', 'bar1', 'prune', 'orange', 'bar1', 'bell', 'cherry', 'orange', 'prune', 'bell', 'bar1', 'cherry', 'seven', 'orange', 'prune', 'orange', 'bell', 'orange'],
                ['orange', 'bell', 'orange', 'bar1', 'prune', 'orange', 'bar1', 'prune', 'orange', 'bar1', 'bell', 'cherry', 'orange', 'prune', 'bell', 'bar1', 'cherry', 'seven', 'orange', 'prune', 'orange', 'bell', 'orange'],
                ['orange', 'bell', 'orange', 'bar1', 'prune', 'orange', 'bar1', 'prune', 'orange', 'bar1', 'bell', 'cherry', 'orange', 'prune', 'bell', 'bar1', 'cherry', 'seven', 'orange', 'prune', 'orange', 'bell', 'orange']
                ],
            startSlot = function() {
                if ( $.cookie('chancen') > 0 ) {
                    credits = $.cookie('chancen');
                } else {
                    credits = 3;
                    $.cookie('chancen', '3',  { expires: 3600 } );
                }
                
                $('#slotCredits').html(credits);
                spinning = false;
                //$('#slotSplash').hide();
                gewinntitel = '';
                $('#slotTrigger').removeClass('slotTriggerDisabled');
                $( '#slotChance_title' ).hide();
                $( '#slotCredits_title' ).show();
                $( '#slotCredits' ).show();
                
                return false;
            },
            endSlot = function() {
                    
                //$.colorbox({html: $('#popupGewinnchance').html() });
                
                //$('#gameOver').show();
                
                $('#slotChance_title').countDown({
                    startNumber: 10,
                    dispYear: false,
                    dispMonth: false,
                    dispDays: false,
                    dispHours: true,
                    dispMinutes: true,
                    dispSeconds: true,
                    dispLeadingZero: true,
                    cookie_name: 'countdown',
                    startFontSize: '50px',
                    endFontSize: '15px',
                    callBack: function(me) {
                        $.cookie('countdown', '', { expires: -0.1 });
                        //$('#gameOver').hide();
                        //$('#slotSplash').show();
                        startSlot();
                        /*$( '#slotChance_title' ).show();*/
                        $( '#slotChance_title' ).hide();
                        $( '#slotCredits_title' ).show();
                        $( '#slotCredits' ).show();
                        
                    }
                });
                
                //$( '#slotCredits_title' ).show();
            },
            spin = function() {
                this.blur();
                
                if (spinning === false) {
                    spinning = 3;
                    points = 0;
                    credits--;
                    $('#slotCredits').html(credits);
                    $.cookie('chancen', credits, { expires: 3600 } );
                    randomNumber = Math.round(Math.random() * 2) + 1;
                    if ( randomNumber == 1 ) {
                        spin[0] = parseInt( 2 );
                        spin[1] = parseInt( 2 );
                        spin[2] = parseInt( 2 );
                        gewinntitel = 'iPhone 4S 64GB';
                    } else if ( randomNumber == 2 ) {
                        spin[0] = parseInt( 4 );
                        spin[1] = parseInt( 4 );
                        spin[2] = parseInt( 4 );
                        gewinntitel = 'iPhone 4S 16GB';
                    } else if ( randomNumber == 3 ) {
                        spin[0] = parseInt( 7 );
                        spin[1] = parseInt( 7 );
                        spin[2] = parseInt( 7 );
                        gewinntitel = 'iPhone 4S 32GB';                        
                    }
                    $( '#slotCredits' ).show();
                    $( '#slotCredits_title' ).show();
                                        
                    $( '#slotChance_title' ).hide();
                    $( '#slotChance_title' ).html('');
                    $( 'span#popupProdukttitel' ).html( gewinntitel );
                    // Gewinntitel für Startseite
                    $.cookie('modell', gewinntitel, { expires: 3600 });
                    
                    $('#slotTrigger').animate({
                        top: 147,
                        height: 55
                    }, 200, 'easeOut').animate({
                        top: 370,
                        left: 453
                    }, 100, 'easeOut');
                    $('#stick').animate({
                        top: 180,
                        height: 145,
                        width: 40
                    }, 200, 'easeOut').animate({
                        top: 380,
                        height: 10,
                        width: 42,
                        left: 457
                    }, 100, 'easeOut');
                    $('#wheel1 div.up').css('top', -(spin[0] * 118 + 11904) + 'px');
                    $('#wheel2 div.up').css('top', -(spin[1] * 118 + 11904) + 'px');
                    $('#wheel3 div.up').css('top', -(spin[2] * 118 + 11904) + 'px');
                    
                    $('#wheel1 div.up').animate({
                        top: -(spin[0] * 118 - 59)
                    }, 3500 + parseInt(1500 * Math.random()));
                    $('#wheel2 div.up').animate({
                        top: -(spin[1] * 118 - 59)
                    }, 3500 + parseInt(1500 * Math.random()));
                    $('#wheel3 div.up').animate({
                        top: -(spin[2] * 118 - 59)
                    }, 3500 + parseInt(1500 * Math.random()));
                    

                    //$('#slotTrigger').addClass('slotTriggerDisabled');
                    $('#slotTrigger').animate({
                        top: 125,
                        left: 445,
                        height: 52
                    }, 700, 'bounceOut');
                    $('#stick').animate({
                        top: 160,
                        height: 164,
                        width: 40,
                        left: 449
                    }, 700, 'bounceOut');
                    setTimeout(function() {
                        stopSpin(1);
                    }, 500 + parseInt(500 * Math.random()));
                    setTimeout(function() {
                        stopSpin(2);
                    }, 500 + parseInt(500 * Math.random()));
                    setTimeout(function() {
                        stopSpin(3);
                    }, 500 + parseInt(500 * Math.random()));
                    
                }
                return false;
            },
            stopSpin = function(slot) {
                
                $('#wheel' + slot).find('div.up').animate({
                    top: -spin[slot - 1] * 118 + 80
                }, {
                    duration: 800,
                    easing: 'elasticOut',
                    complete: function() {
                        spinning--;
                        if (spinning === 0) {
                            endSpin();
                        }
                        if ( credits === 0 ) {
                        $( '#slotChance_title' ).show();                        
                        $( '#slotCredits_title' ).hide();
                        $( '#slotCredits' ).hide();
                          }
                        //$.colorbox({html: $('#popupGewinnchance').html() });
                        
                    }
                });
            },
            endSpin = function() {
                var slotType = slots[0][spin[0]],
                    matches = 1,
                    winnedPoints = 0;
                waitToSpin = 10;
                if (slotType === slots[1][spin[1]]) {
                    matches++;
                    if (slotType === slots[2][spin[2]]) {
                        matches++;
                    }
                }
                var winnedPoints = slotsTypes[slotType][matches - 1];

                if (winnedPoints > 0) {
                    
                    $( '#slotPoints' ).html(gewinntitel);
                    
                    waitToSpin = 50;
                }
                
                
                if ( credits === 0 ) {
                    $('#popupNochmal').remove();
                    
                }
                
                $.colorbox({
                        html: $('#popupGewinnchance').html(),
                        
                        width: '503px',
                        height: '350px',
                        
                        escKey: false,
                        overlayClose: false,
                        onOpen: function() {
                            $('#cboxClose').remove()
                        },
                        onClosed: function() {
                            $( '#slotChance_title' ).hide();                        
                            $( '#slotCredits_title' ).show();
                            $( '#slotCredits' ).show();
                        }
                
                });
                setTimeout(function() {
                    if (credits === 0) {
                        setTimeout(function() {
                            endSlot();
                            
                        }, 10);
                        
                    } else {
                        
                        setTimeout(function() {
                            $('#slotTrigger').removeClass('slotTriggerDisabled');
                            
                            spinning = false;
                        }, 10);
                        
                    }
                }, waitToSpin);
            };
        return {
            init: function() {
                $('#startCount').show();
                
                $( '#slotChance_title' ).show();
                        $( '#slotCredits_title' ).hide();
                        $( '#slotCredits' ).hide();
                
                $('#slotChance_title').countDown({
                    startNumber: 0,
                    dispYear: false,
                    dispMonth: false,
                    dispDays: false,
                    dispHours: true,
                    dispMinutes: true,
                    dispSeconds: true,
                    dispLeadingZero: true,
                    cookie_name: 'countdown',
                    startFontSize: '50px',
                    callBack: function(me) {
                        $.cookie('countdown', '', { expires: -0.1 });
                        $('#startCount').hide();
                        $('#slotSplash').show();
                           
                        $( '#slotChance_title' ).hide();
                        $( '#slotCredits_title' ).show();
                        $( '#slotCredits' ).show();
                        
                        startSlot();
                        
                    }
                });
                //$('#slotSplash a').bind('click', startSlot);
                //startSlot();
                
                $('#slotTrigger').bind('drag', spin);
                $('#slotTrigger').bind('click', spin);
                $('#left_header_box img#start_text').bind('click', spin);
                //$('#popupNochmal').bind('click', function() { alert('test'); });
                $('#wheel1 div.up').css('top', '1020px');
                $('#wheel2 div.up').css('top', '-45px');
                $('#wheel3 div.up').css('top', '-990px');
                
                
                
            }
        };
    }();
    
    $.extend($.easing, {
        bounceOut: function(x, t, b, c, d) {
            if ((t /= d) < (1 / 2.75)) {
                return c * (7.5625 * t * t) + b;
            } else if (t < (2 / 2.75)) {
                return c * (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75) + b;
            } else if (t < (2.5 / 2.75)) {
                return c * (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375) + b;
            } else {
                return c * (7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375) + b;
            }
        },
        easeOut: function(x, t, b, c, d) {
            return -c * (t /= d) * (t - 2) + b;
        },
        elasticOut: function(x, t, b, c, d) {
            var s = 5.70158;
            var p = 0;
            var a = c;
            if (t === 0) {
                return b;
            }
            if ((t /= d) === 1) {
                return b + c;
            }
            if (!p) {
                p = d * 0.3;
            }
            if (a < Math.abs(c)) {
                a = c;
                var s = p / 4;
            } else {
                var s = p / (1.5 * Math.PI) * Math.asin(c / a);
            }
            return a * Math.pow(1.5, -10 * t) * Math.sin((t * d - s) * (1.5 * Math.PI) / p) + c + b;
        }
    });
    $(document).ready(slotMachine.init);
    
})(jQuery);


