/**
 * Copyright (c) 2008 Andreas Blixt <andreas@blixt.org>
 * Project homepage: <http://code.google.com/p/monkey-web/>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var ServiceClient = new Class({
    initialize: function (path) {
        this._path = path;
        this._queue = [];
        this._running = false;
    },
    
    call: function (action, args, onSuccess, onError) {
        if (this._running == true) {
            this._queue.push([action, args, onSuccess]);
            return;
        }

        this._running = true;

        var params = { _time: +new Date() };
        $each(args, function (v, p) {
            params[p] = JSON.encode(v);
        });

        var sc = this;
        var req = new Request.JSON({
            secure: false,
            url: sc._path + action,
            onComplete: function (result) {
                if (result) {
                    switch (result.status) {
                        case 'error':
                            if (onError)
                                onError(result.response);
                            else
                                alert(result.response.type + ': ' + result.response.message);
                            break;
                        case 'list':
                            break;
                        case 'success':
                            if (onSuccess) onSuccess(result.response);
                            break;
                        default:
                            alert('Unknown status: ' + result.status);
                            break;
                    }
                } else {
                    if (this.attempts >= 3) {
                        alert('A request failed after repeated retries. Please reload the page.');
                    } else {
                        this.attempts++;
                        this.get.delay(500, this, params);
                    }
                }

                sc._running = false;
                if (sc._queue.length > 0) {
                    sc.call.apply(sc, sc._queue.shift());
                }
            }
        });
        req.attempts = 1;
        req.get(params);
    }
});

var MonkeyService = new Class({
    Extends: ServiceClient,

    initialize: function () {
        this.parent('/game/');
    },

    addCpuPlayer: function (gameId, onSuccess, onError) {
        this.call('add_cpu_player', { game: gameId }, onSuccess, onError);
    },

    changeNick: function (newNick, onSuccess, onError) {
        this.call('change_nickname', { nickname: newNick }, onSuccess, onError);
    },

    cpuBattle: function (ruleSetId, onSuccess, onError) {
        this.call('cpu_battle', { rule_set: ruleSetId }, onSuccess, onError);
    },

    createGame: function (ruleSetId, onSuccess, onError) {
        this.call('create_game', { rule_set: ruleSetId }, onSuccess, onError);
    },

    gameStatus: function (gameId, turn, onSuccess, onError) {
        this.call('get_game_status', { game: gameId, turn: turn }, onSuccess, onError);
    },
    
    getRuleSets: function (onSuccess, onError) {
        this.call('get_rule_sets', {}, onSuccess, onError);
    },
    
    getPlayerInfo: function (onSuccess, onError) {
        this.call('get_player_info', {}, onSuccess, onError);
    },

    joinGame: function(gameId, onSuccess, onError) {
        this.call('join_game', { game: gameId }, onSuccess, onError);
    },
    
    leaveGame: function (gameId, onSuccess, onError) {
        this.call('leave_game', { game: gameId }, onSuccess, onError);
    },

    listGames: function (mode, onSuccess, onError) {
        var params = mode ? { mode: mode } : {};
        this.call('get_games', params, onSuccess, onError);
    },
    
    move: function (gameId, x, y, onSuccess, onError) {
        this.call('put_tile', { game: gameId, x: x, y: y }, onSuccess, onError);
    },
    
    sendEmail: function (name, email, gameId) {
    	this.call('send_email', {name: name, email: email, gameid: gameId}, emailSent, function(){alert('Oops! There was an error sending email! Please try again later..')});
    }
});

var MonkeyClient = new Class({
    initialize: function () {
        var mc = this;

        mc.game = null;
        mc.gameId = null;
        mc.service = new MonkeyService();

        var ruleSets;

        mc.html = {};
        $extend(mc.html, {
            game: new Element('div', {
                'class': 'game'
            }).adopt(
                
		        new Element('div', {'style': 'width: 400px; float: left;'}).adopt(
		        		new Element('p').adopt(
		                        mc.html.joinOrLeave = new Element('button', {
		                            text: 'Join'
		                        }),
		                        mc.html.addCpuPlayer = new Element('button', {
		                            text: 'Add CPU player'
		                        }),
		                        new Element('button', {
		                            events: { click: mc.setMode.bind(mc, MonkeyClient.Mode.lobby) },
		                            text: 'To the lobby'
		                        })
		                    ),
		        		mc.html.gameStatus = new Element('p'),
		        		mc.html.ruleSetDescription = new Element('p', { 'class': 'rule-set-description' }),
		        		new Element('br'),
		        		new Element('p', {'style': 'font-weight: bold;', 'text': 'Players'}),
			            mc.html.players = new Element('ol', { 'class': 'players' })
			            //mc.html.ruleSetName = new Element('p', { 'class': 'rule-set-name' }),
			            
		        ),
		        
		        new Element('div', {'class': 'gamebackground', 'id': 'gamebackground'}).adopt(
		        		new Element('table', {'id': 'gameboardtable', 'style': 'text-align: left; vertical-align: middle; margin-top: auto;', 'align': 'center', 'valign': 'middle'}).adopt(
		                    	mc.html.gameBoard = new Element('tbody')
		    		        )
		        )
            ),
            /*
            main: new Element('div', { 'class': 'monkey' }).adopt(
                mc.html.player = new Element('p', { 'class': 'player', text: 'Please wait...' })
            ).inject('body'),
			*/
            
            main: new Element('div', { 'class': 'monkey' }).inject('body'),
            
            
            
            lobby: new Element('div', {
                'class': 'lobby'
            }).adopt(
               
                ruleSets = new Element('select', {'style': 'visibility: hidden'}),
                new Element('div', {'style': 'padding-bottom: 8px; padding-left: 30px; font-size: 16px;', 'text': 'Please choose the game you like. Remember, you can always play with CPU if there are no players available!'}),
                new Element('div', {'class': 'gamecolumn'}).adopt(
                		new Element('div', {'class': 'gamecontent'}).adopt(
                				new Element('img', {'src': 'http://static2.playtictac.com/3x3.jpg', 'style': 'margin-top: 100px;', 'align': 'absmiddle', 'alt': '3x3'})
                		),
                				new Element('div', {'class': 'gamesize', 'text': '3x3'}),
                				new Element('div', {'class': 'gamedescription'}).adopt(
                						new Element('button', {
                							events: {
	                                            click: function () {
	                                                //this.disabled = true;
	                                                mc.createGame(1001);
	                                            }
	                                        },
	                                        text: 'Play - 2 players'
                						})
                				)
                		
                ),
                new Element('div', {'class': 'gamecolumn'}).adopt(
                		new Element('div', {'class': 'gamecontent'}).adopt(
                				new Element('img', {'src': 'http://static2.playtictac.com/7x7.jpg', 'style': 'margin-top: 65px;', 'align': 'absmiddle', 'alt': '7x7'})
                		),
                				new Element('div', {'class': 'gamesize', 'text': '7x7'}),
                				new Element('div', {'class': 'gamedescription'}).adopt(
                						new Element('button', {
                							events: {
	                                            click: function () {
	                                                //this.disabled = true;
	                                                mc.createGame(2001);
	                                            }
	                                        },
	                                        text: 'Play - 2 players'
                						})
                				)
                		
                ),
                new Element('div', {'class': 'gamecolumn'}).adopt(
                		new Element('div', {'class': 'gamecontent'}).adopt(
                				new Element('img', {'src': 'http://static2.playtictac.com/19x19.jpg', 'style': 'margin-top: 25px;', 'align': 'absmiddle', 'alt': '19x19'})
                		),
                		new Element('div', {'class': 'gamesize', 'text': '19x19'}),
                		new Element('div', {'class': 'gamedescription'}).adopt(
                				new Element('button', {
        							events: {
                                        click: function () {
                                            //this.disabled = true;
                                            mc.createGame(3001);
                                        }
                                    },
                                    text: 'Play - 2 players'
                				}),
                				new Element('button', { 'style': 'margin-top: 8px',
                							events: {
	                                            click: function () {
	                                                //this.disabled = true;
	                                                mc.createGame(2);
	                                            }
	                                        },
	                                        text: 'Play - 4 players'
                				}),
                				new Element('button', { 'style': 'margin-top: 8px',
                							events: {
	                                            click: function () {
	                                                //this.disabled = true;
	                                                mc.createGame(4001);
	                                            }
	                                        },
	                                        text: 'Play - 6 players'
                				})
                		)
                		
                )
            )
        });
       
        new Element('div', {'class': 'listcontrol', 'id': 'listmodebuttons'}).adopt(
        	new Element('ul').adopt(
                new Element('li', { 'class': 'play' }).adopt(new Element('a', {
                    events: {
                        click: mc.setListMode.bind(mc, 'play')
                    },
                    href: '#play',
                    text: 'Play'
                })),
                new Element('li', { 'class': 'view' }).adopt(new Element('a', {
                    events: {
                        click: mc.setListMode.bind(mc, 'view')
                    },
                    href: '#view',
                    text: 'View'
                })),
                new Element('li', { 'class': 'past' }).adopt(new Element('a', {
                    events: {
                        click: mc.setListMode.bind(mc, 'past')
                    },
                    href: '#past',
                    text: 'Past'
                }))
            ),
            new Element('table').adopt(
                new Element('thead').adopt(
                    new Element('tr').adopt(
                        new Element('th', {
                            'class': 'action',
                            text: 'View/Play'
                        }),
                        new Element('th', {
                            'class': 'rule-set',
                            text: 'Rule set'
                        }),
                        new Element('th', {
                            'class': 'players',
                            text: 'Players'
                        })
                    )
                )
            )
        ).inject('listcontrol')

        mc.service.getRuleSets(function (list) {
            mc.ruleSets = {};
            for (var i = 0; i < list.length; i++) {
                mc.ruleSets[list[i].id] = list[i];
                new Element('option', { text: list[i].name, value: list[i].id }).inject(ruleSets);
            }
            ruleSets.value = list[0].id;
        });

        mc.setMode(MonkeyClient.Mode.lobby, true);
        mc.setListMode('play');
        mc.refreshPlayer();
    },
    
    addCpuPlayer: function () {
        this.service.addCpuPlayer(this.gameId, this.refresh.bind(this));
    },
    
    cpuBattle: function (ruleSetId) {
        this.service.cpuBattle(ruleSetId, this.goToGame.bind(this));
    },
    
    createGame: function (ruleSetId) {
        this.service.createGame(ruleSetId, this.goToGame.bind(this));
    },
    
    goToGame: function (gameId, game, skipRefresh) {
        this.gameId = gameId;
        this.setMode(MonkeyClient.Mode.game, skipRefresh);
        
        if (game) this.handleStatus(game);
    },
    
    handleList_playerTd: function (index, game) {
        var mc = this;

        if (game.players[index]) {
            var turn = game.state == 'playing' && game.current_player == index + 1;
            return new Element('td', {
                'class': 'slot',
                text: game.players[index] + (turn ? ' ←' : '')
            });
        } else {
            return new Element('td', {
                'class': 'open slot'
            }).adopt(
                game.playing_as?
                new Element('span', {
                    text: 'Waiting for player to join...'
                }):
                new Element('a', {
                    events: { click: mc.joinGame.bind(mc, [game.id, game]) },
                    href: '#' + game.id,
                    text: 'Join game'
                }).addEvent('click', function() {openGameWindow()})
            );
        }
    },
    
    handleList: function (games) {
        var mc = this;
        var objGameList = $('gamelist');
        //mc.html.gameList.empty();
        objGameList.empty();
        
        if (games.length == 0) {
        	objGameList.adopt(
        	
        		new Element('tr').adopt(
                new Element('td', {
                    'class': 'no-games',
                    colspan: 3,
                    text: 'There are no games to show here.'
                })
            ));
        } else {
        	
            for (var i = 0; i < games.length; i++) {
                var g = games[i], rs = mc.ruleSets[g.rule_set_id];
                var row, turn = (g.state == 'playing' && g.current_player == 1);

                var cls = g.state;
                if (cls == 'win' && g.playing_as && g.playing_as != g.current_player) {
                    cls = 'loss';
                }

                objGameList.adopt(new Element('tr', {
                    'class': cls
                }).adopt(
                    new Element('td', {
                        'class': 'action',
                        rowspan: rs.num_players
                    }).adopt(
                        new Element('button', {
                            events: {
                                click: mc.goToGame.bind(mc, [g.id, g])
                            },
                            text: g.state == 'playing' && g.playing_as > 0 ? 'Play' : 'View'
                        }).addEvent('click', function() {openGameWindow()})
                    ),
                    new Element('td', {
                        'class': 'rule-set',
                        rowspan: rs.num_players
                    }).adopt(
                        //new Element('span', { text: rs.name + ' — ' }),
                        new Element('span', {
                            text: rs.m + '×' + rs.n + ' board, ' + rs.k + ' in a row to win, place ' + rs.q +
                                  (rs.p == rs.q ? ' each turn.' : ' first turn, then ' + rs.p + ' following turns.')
                        })
                    ),
                    mc.handleList_playerTd(0, g)
                ));
                
                for (var j = 1; j < rs.num_players; j++) {
                	objGameList.adopt(new Element('tr', {
                        'class': cls
                    }).adopt(mc.handleList_playerTd(j, g)));
                }
            }
        }
        
        $clear(mc.timer);
        mc.timer = mc.refresh.delay(5000, mc);
    },
    
    handleStatus: function (game) {
        var mc = this;

        if (game) {
            var pa = game.playing_as;
            var cp = game.current_player;
            var rs = mc.ruleSets[game.rule_set_id];

            var status;
            switch (game.state) {
                case 'waiting':
                    status = 'This game needs more players before it can start.';
                    break;
                case 'playing':
                    var nick = game.players[cp - 1];
                    status = 'This game is currently being played. It\'s ' +
                             (pa == cp ? 'your' : nick + '\'' + (nick.substring(nick.length - 1).toLowerCase() == 's' ? '' : 's')) +
                             ' turn.';
                    break;
                case 'aborted':
                    status = 'This game has been abandoned by a player and cannot continue.';
                    break;
                case 'draw':
                    status = 'This game ended in a draw.';
                    break;
                case 'win':
                    status = 'This game has ended. ';
                    if (pa == cp)
                        status += 'You won!';
                    else
                        status += game.players[cp - 1] + ' won.';
                    break;
            }

            mc.html.game.set('class', 'game player-' + pa);
            mc.html.gameStatus.set('text', status);
            
            if(game.state == 'waiting') {
	            mc.html.gameStatus.adopt(
	            		new Element('br'),
	            		new Element('b', {'text': 'Invite friend by sending this link'}),
	            		new Element('br'),
	            		new Element('input', {'type': 'text', 'class': 'invitelink', 'value': 'http://'+document.domain+'/?gameid='+mc.gameId, 'onclick': 'this.select()', 'readonly': 'true'}),
	            		new Element('br')
	            );
            }
            
            //mc.html.ruleSetName.set('text', rs.name);
            mc.html.ruleSetDescription.set('text', rs.m + '×' + rs.n + ' board, ' + rs.k + ' in a row to win, place ' + rs.q +
                                                     (rs.p == rs.q ? ' each turn.' : ' first turn, then ' + rs.p + ' following turns.'));

            var jol = mc.html.joinOrLeave;
            jol.set('text', pa ? (game.state == 'waiting' ? 'Leave' : 'Abandon') : 'Join');
            if (game.state == 'waiting' || (game.state == 'playing' && pa)) {
                jol.disabled = false;
                jol.onclick = pa ? function () {
                    this.disabled = true;
                    $clear(mc.timer);
                    mc.leaveGame();
                } : function () {
                    this.disabled = true;
                    mc.joinGame(mc.gameId);
                };
            } else {
                jol.disabled = true;
            }
            
            var acp = mc.html.addCpuPlayer;
            if (game.state == 'waiting' && pa) {
                acp.disabled = false;
                acp.onclick = function () { this.disabled = true; mc.addCpuPlayer(); };
            } else {
                acp.disabled = true;
            }

            mc.html.players.empty();
            for (var i = 0; i < rs.num_players; i++) {
                var li;
                if (game.players[i])
                    li = new Element('li', {
                        text: game.players[i]
                    }).inject(mc.html.players);
                else
                    li = new Element('li', {
                        'class': 'open',
                        text: 'Open slot'
                    }).inject(mc.html.players);

                if (game.state == 'playing' && i + 1 == cp) {
                    li.set('class', 'current');
                }
            }

            if (game.board) {
                var width = game.board.length, height = game.board[0].length;
                if (!mc.html.cells) {
                    mc.html.cells = [];
                    mc.html.gameBoard.empty();

                    for (var y = 0; y < height; y++) {
                        var row = new Element('tr').inject(mc.html.gameBoard);
                        for (var x = 0; x < width; x++) {
                            if (!mc.html.cells[x]) mc.html.cells[x] = [];

                            var player = game.board[x][y];
                            mc.html.cells[x][y] = new Element('td', {
                                events: {
                                    click: mc.move.bind(mc, [x, y]),
                                    mouseenter: function () { this.addClass('hover'); },
                                    mouseleave: function () { this.removeClass('hover'); }
                                },
                                'class': player ? 'player-' + player : 'empty'
                            }).inject(row);
                        }
                    }
                } else {
                    for (var y = 0; y < height; y++) {
                        for (var x = 0; x < width; x++) {
                            var player = game.board[x][y];
                            mc.html.cells[x][y].set('class', player ? 'player-' + player : 'empty');
                        }
                    }
                }
                
                try {
                	$('gameboardtable').setStyle('margin-top', parseInt(($('gamebackground').getHeight() - 25 - $('gameboardtable').getHeight())/2));
                } catch(e) {
                	
                }
            }

            mc.game = game;
            if (game.state == 'win' && game.board) mc.markWinTiles();
        }
        
        $clear(mc.timer);
        if (!mc.game) {
            mc.timer = mc.refresh.delay(2500, mc);
        } else if (mc.game.state == 'waiting') {
            mc.timer = mc.refresh.delay(5000, mc);
        } else if (mc.game.state == 'playing') {
            mc.timer = mc.refresh.delay(750, mc);
        }
    },
    
    handlePlayer: function (player) {
        var mc = this, s = mc.service;
        var objPlayer = $('player');
        mc.player = player;
        
        //mc.html.player.empty();
        objPlayer.empty();

        var a = new Element('a', {
            events: {
                click: function () {
                    var nick = prompt('New nickname', mc.player.nickname);
                    if (nick) s.changeNick(nick, mc.handlePlayer.bind(mc));
                }
            },
            href: '#',
            text: player.nickname
        }).inject(objPlayer);

        if (player.nickname == 'Anonymous') {
            var flasher = function (i) {
                a.set('text', 'Click here to change nickname!');
                a.toggleClass('alert');
                
                if (i-- > 0) {
                    flasher.delay(500, null, i);
                } else {
                    a.set('text', mc.player.nickname);
                }
            };

            flasher(7);
        }

        objPlayer.appendText(' — ');

        new Element('a', {
            href: player.log_url,
            text: player.anonymous ? 'Log in' : 'Log out'
        }).inject(objPlayer);
    },
    
    joinGame: function (gameId, game) {
        var mc = this;

        if (game) mc.goToGame(gameId, game, true);
        mc.service.joinGame(gameId, function (game) {
            mc.goToGame(gameId, game);
        });
    },
    
    leaveGame: function () {
        var q = 'Are you sure you want to abandon this game?';
        if (this.game.state == 'playing' && !confirm(q)) return;
        this.service.leaveGame(this.gameId, this.setMode.bind(this, MonkeyClient.Mode.lobby));
    },
    
    markWinTiles: function () {
        // TODO: Needs optimization
        var self = this.ruleSets[this.game.rule_set_id];
        var board = this.game.board;
        var player = this.game.current_player;

        for (var y = 0; y < self.n; y++) {
            for (var x = 0; x < self.m; x++) {
                var c = this.html.cells[x][y];
                
                if (c.hasClass('w')) continue;
                c.addClass('l');

                var ca = 0, cb = 0, cc = 0, cd = 0;
                for (var i = -self.k + 1; i < self.k; i++) {
                    var tx = x + i, txi = x - i, ty = y + i;
                    // Test horizontal -
                    if (tx >= 0 && tx < self.m)
                        ca += board[tx][y] == player ? 1 : -ca;
                    // Test vertical |
                    if (ty >= 0 && ty < self.n)
                        cb += board[x][ty] == player ? 1 : -cb;
                    // Test diagonal \
                    if (tx >= 0 && ty >= 0 && tx < self.m && ty < self.n)
                        cc += board[tx][ty] == player? 1 : -cc;
                    // Test diagonal /
                    if (txi >= 0 && ty >= 0 && txi < self.m && ty < self.n)
                        cd += board[txi][ty] == player ? 1 : -cd;

                    if (!self.exact && (ca == self.k || cb == self.k || cc == self.k || cd == self.k)) {
                        c.removeClass('l');
                        c.addClass('w');
                        break;
                    }
                }
            }
        }
    },
    
    move: function (x, y) {
        if (this.mode == MonkeyClient.Mode.game) {
            this.service.move(this.gameId, x, y, this.handleStatus.bind(this));
        }
    },
    
    refresh: function () {
        $clear(this.timer);

        switch (this.mode) {
            case MonkeyClient.Mode.lobby:
                this.service.listGames(this.listMode, this.handleList.bind(this));
                break;
            case MonkeyClient.Mode.game:
                var t = this.game && this.game.state == 'playing' ? this.game.turn : null;
                this.service.gameStatus(this.gameId, t, this.handleStatus.bind(this));
                this.service.listGames(this.listMode, this.handleList.bind(this));
                break;
        }
    },
    
    sendEmail: function(name, email) {
    	this.service.sendEmail(name, email, this.gameId);
    },
    
    refreshPlayer: function () {
        this.service.getPlayerInfo(this.handlePlayer.bind(this));
    },
    
    setListMode: function (newMode) {
        $('gamelist').empty().adopt(
        	
            new Element('tr').adopt(
                new Element('td', {
                    'class': 'no-games',
                    colspan: 3,
                    text: 'Loading...'
                })
            )
            
        );

        this.html.lobby.set('class', 'lobby ' + newMode);
        $('listmodebuttons').set('class', 'listcontrol ' + newMode);
        this.listMode = newMode;
        this.refresh();
    },
    
    setMode: function (newMode, skipRefresh) {
        switch (this.mode) {
            case MonkeyClient.Mode.lobby:
                this.html.lobby.dispose();
                break;
            case MonkeyClient.Mode.game:
                this.html.game.dispose();
                break;
        }
        
        this.mode = newMode;
        
        switch (this.mode) {
            case MonkeyClient.Mode.lobby:
                this.game = null;
                this.gameId = null;

                //this.html.cpuBattle.disabled = false;
                //this.html.createGame.disabled = false;
                this.html.main.set('class', 'monkey in-lobby');
                this.html.lobby.inject(this.html.main);

                break;
            case MonkeyClient.Mode.game:
                this.html.cells = null;

                this.html.gameBoard.empty();
                this.html.players.empty();

                this.html.gameStatus.set('text', 'Loading game status...');
                this.html.joinOrLeave.disabled = true;
                this.html.joinOrLeave.set('text', 'Join');

                this.html.main.set('class', 'monkey in-game');
                this.html.game.inject(this.html.main);

                break;
        }
        
        if (!skipRefresh) this.refresh();
    }
});

MonkeyClient.Mode = {
    lobby: 1,
    game: 2
};

function slideContent(currentContainer, newContainer, page) {

	page1 = 'aboutLink';
	page2 = 'playLink';
	
	if(page == page2 && $(page2).hasClass('tablistactive')) return;
	
	if(page == page2) {
		$(page1).removeClass('tablistactive');
		$(page2).addClass('tablistactive');
	} else {
		$(page2).removeClass('tablistactive');
		$(page1).addClass('tablistactive');
	}
	
	var item_in = new Fx.Morph($(newContainer), {  
           duration: 1250,   
              transition: Fx.Transitions.Quad.easeInOut,   
              wait:false  
     });  
       
     var item_out = new Fx.Morph($(currentContainer), {  
              duration: 1250,   
              transition: Fx.Transitions.Quad.easeInOut,   
              wait:false  
     });  
       
     //we will set a beginning value here  
     //this is so that it gives the illusion of continuous motion from one direction, even after the first cycle of items  
     item_in.start({  
     'left': [900, 0],  
     'opacity':[0,1]  
     });  
       
     //no beginning values needed, since we always want to push the old item out to the left  
     item_out.start({  
     'left': '-900',  
     'opacity':[0]  
     });  
}

function openGameWindow() {
	new Fx.Scroll(window).toElement($('container')); 
	slideContent('content1', 'content2', 'playLink')
}

function getUrlParameter(name) {

	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}

function sendEmail() {
	if($('name').value != '' && $('name').value != 'Tavo vardas') {
		if(validateEmail($('email').value))
			monkey.sendEmail($('name').value, $('email').value)
		else
			alert('Invalid email address!');
	} else {
		alert('Please enter your friend name!');
	}
}

function emailSent() {
	
	$('tabsright').empty(); 
	$('tabsright').adopt(
		new Element('div', {'class': 'emailsent', 'text': 'Thank you, email was sent to your friend!'})
	)
}

function validateEmail(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    return false
	 }

		 return true					
}

function inputValue(objInput, text) {
	
	if(objInput.value.length == 0) {
		objInput.value = text;
	}
}
