﻿/* JSLint verified */
/*globals document */
function createLink(cls, link, target, content) {
    "use strict";
    var elm = document.createElement('a');
    elm.className = cls;
    elm.href = link;
    elm.target = target || '_0';
    elm.innerHTML = content;
    return elm;
}
if (!Array.shuffle) { // CrossBrowser Array shuffle
    Array.prototype.shuffle = function () {
        "use strict";
        var tmp, current, top = this.length;
        if (top) {
            while (top) {
                top -= 1;
                current = Math.floor(Math.random() * (top + 1));
                tmp = this[current];
                this[current] = this[top];
                this[top] = tmp;
            }
        }
        return this;
    };
}

var BUNNYSLOTS = BUNNYSLOTS || {
    Language:
        {
            home: 'HOME',
            search: "Search",
            read: "Read more",
            visit: "Visit Casino <span style='font-size:18px;'>»</span>",
            recommended: "OUR FAVORITE CASINOS",
            votes: "Votes",
            voteUp: "I like this!",
            voteDown: "I hate this!",
            ok: "Thank you!",
            fail: "You allready votes."
        },
    Partners: eval(document.getElementById('pJson').value).shuffle(),
    PartnersScreenShots: document.getElementById('logos').value.split('|')[1],
    PartnersLogos: document.getElementById('logos').value.split('|')[0],
    SoftwareBadges:
        [
            { Title: "Playtech", Guid: "7e9", xPos: "-96px 0" },
            { Title: "BetSoft", Guid: "890", xPos: "-169px 0" },
            { Title: "WagerWorks", Guid: "35c", xPos: "-242px 0" },
            { Title: "Rival", Guid: "c35", xPos: "-314px 0" },
            { Title: "NetEnt", Guid: "59e", xPos: "-387px 0" },
            { Title: "MicroGaming", Guid: "19a", xPos: "-460px 0" },
            { Title: "Cayetano", Guid: "155", xPos: "-533px 0" },
            { Title: "OpenBet", Guid: "cfe", xPos: "-607px 0" },
            { Title: "RTG", Guid: "4e6", xPos: "-680px 0" },
            { Title: "Cryptologic", Guid: "05d", xPos: "-753px 0" },
            { Title: "CTXM", Guid: "51d", xPos: "-827px 0" },
            { Title: "Sheriff", Guid: "63e", xPos: "-898px 0" },
            { Title: "Vegas Tech", Guid: "a50", xPos: "-975px 0" },
            { Title: "Top Game", Guid: "0d0", xPos: "-1048px 0" },
            { Title: "Party Gaming", Guid: "46d", xPos: "-1121px 0" }
        ],
    SetupImages: function (that) {
        "use strict";
        var 
            elm,
            links = that.getElementsByTagName('a'),
            i = links.length, index;
        while (i) {
            i -= 1;
            elm = document.createElement('span');
            elm.innerHTML = links[i].innerHTML;
            elm.style.width = '220px';
            elm.className = 'e';

            elm = document.createElement('div');
            elm.style.backgroundImage = 'url(/images/' + that.id + '/' + that.getAttribute('data-r') + '.jpg)';
            elm.style.backgroundPosition = '-' + i * 220 + 'px 0px';
            links[i].appendChild(elm);
            if (that.id === "games") {
                // Add new badge
                if (links[i].className === 'n') {
                    elm = document.createElement('span');
                    elm.className = 'n';
                    links[i].appendChild(elm);
                }

                // add software badge
                elm = document.createElement('span');
                elm.className = 'b';
                index = this.SoftwareBadges.length;
                while (index) {
                    index -= 1;
                    if (this.SoftwareBadges[index].Guid === links[i].getAttribute('data-r')) {
                        elm.style.backgroundPosition = this.SoftwareBadges[index].xPos;
                        break;
                    }
                }
                links[i].appendChild(elm);
            }
        }
    },
    CreateGame: function () {
        "use strict";
        var 
            elm, that,
            GameProperties,
            Guid, Width, Height,
            Love, Total;

        that = document.getElementById('g');
        GameProperties = that.getAttribute('data-r').split('|');
        Guid = GameProperties[0];
        Width = 528; //GameProperties[1];
        Height = 381; //GameProperties[2];
        Love = GameProperties[3];
        Total = GameProperties[4];

        // Add game div
        elm = document.createElement('div');
        elm.className = 'g';
        that.appendChild(elm);

        // add iframe
        elm = document.createElement('iframe');
        elm.className = 'frame';
        elm.height = Height;
        elm.width = Width;
        elm.src = '/games/igame/' + Guid + '/';
        elm.frameBorder = 0;
        elm.scrolling = 'no';
        that.lastChild.appendChild(elm);

        // add sidebar
        elm = document.createElement('div');
        elm.className = 'p';
        that.appendChild(elm);
        // Payment methods logo
        elm = document.createElement('div');
        elm.className = 'pm';
        that.lastChild.appendChild(elm);
        // Bonus Text
        elm = document.createElement('div');
        elm.className = 'bon';
        elm.innerHTML = this.Partners[0].Bonus;
        that.lastChild.appendChild(elm);
        // Add visit button
        that.lastChild.appendChild(createLink('but', '/online-casino/' + this.Partners[0].Alias + '/visit/', '', this.Language.visit));

        // Tournier button
        that.lastChild.appendChild(createLink('t', 'http://www.spinpalace.com/lp/slot-tournament/?s=wgs15449&a=wgsaffad26785', '', ''));

        // Download button
        that.lastChild.appendChild(createLink('d', '/online-casino/' + this.Partners[0].Alias + '/download/', '', ''));

        // Cashier button
        that.lastChild.appendChild(createLink('c', '/online-casino/' + this.Partners[0].Alias + '/visit/', '', ''));

    },
    Init: function () {
        "use strict";
        var 
            elm, that, max;

        // Add Home button to top menu
        that = document.getElementById('tm');
        that.insertBefore(createLink('home', '/', '_self', this.Language.home), that.firstChild);

        // Add Search to the page
        that = document.getElementById('it');
        elm = document.createElement('label');
        elm.className = 'sl';
        elm.innerHTML = this.Language.search.toUpperCase() + ':<span style="display:block;"><form action="/search/" method="post"><input type="text" name="search" /><input type="submit" name="submit" value="' + this.Language.search + '" /></form></span>';
        that.insertBefore(elm, that.firstChild);

        // Load game images
        that = document.getElementById('games');
        if (that !== null) { this.SetupImages(that); }

        // load channel images
        that = document.getElementById('channels');
        if (that !== null) { this.SetupImages(that); }

        // Create game
        that = document.getElementById('g');
        if (that !== null) { this.CreateGame(); }

        // Create TopList
        that = document.getElementById('tl');
        if (that !== null) {
            max = that.getAttribute('data-r');
            if (this.Partners.length < max) { max = this.Partners.length; }
            // Add payment methods symbol
            elm = document.createElement('div');
            elm.className = 'pm';
            that.appendChild(elm);
            // Add Title for TopList
            elm = document.createElement('div');
            elm.className = 'title';
            elm.innerHTML = this.Language.recommended;
            that.appendChild(elm);
            while (max) {
                max -= 1;
                that.appendChild(createLink('anc', '/online-casino/' + this.Partners[max].Alias + '/visit/', '', ''));
                // Logo
                elm = document.createElement('div');
                elm.style.backgroundImage = 'url(/images/partners/' + this.PartnersLogos + ')';
                elm.style.backgroundPosition = '-' + (this.Partners[max].Logo * 120) + 'px 0';
                elm.className = 'logo';
                that.lastChild.appendChild(elm);
                // Add visit button
                elm = document.createElement('div');
                elm.className = 'but';
                elm.innerHTML = this.Language.visit;
                that.lastChild.appendChild(elm);
                elm = document.createElement('h5');
                elm.innerHTML = this.Partners[max].Bonus;
                that.appendChild(elm);
                // Clear floats
                elm = document.createElement('div');
                elm.style = 'clear:both;display:inline-block;';
                that.lastChild.appendChild(elm);
            }
        }
    }
};

BUNNYSLOTS.Init();

/* Google Analytics - Does not pass JSLint */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-28263758-1']);
_gaq.push(['_trackPageview']);

(function () {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
