
var play = function(symbol) {
    var name = symbol.substring(symbol.length-12,symbol.length-4);
    
    if ( flag === 0) {
	if (name === "es/paper") {
	    var choice = "paper";
	       chosenWay(choice,$papercard);
	}
	else if (name === "ges/rock") {
	    var choice = "rock";
	    chosenWay(choice,$rockcard);
	}
	else if (name === "scissors") {
	    var choice = "scissors";
	    chosenWay(choice,$scissorscard);
	}
	flag = 1;
    }
};



var chosenWay = function(choice, $card) {
	$('.cards').slideToggle();
        $('h1').remove();
        $('.cards').empty();
        $('#one').append($card);
        $('#two').append('<p>VS</p>');
        $('#one').slideToggle();
        $('#two').slideToggle();
        playGame(choice);
        $('#three').slideToggle();
	if ( tip === 0) {
	    $('#tip').append('<h1 class="blink_me">TAP TAP TO GO ON</h1>');
	    tip = 1;
	}
	
};

var restartOrEnding = function() {
    
    if (flag === 1 && (userPoint === 5 || computerPoint === 5)) {
	$("#tab1").fadeOut('slow');
	$('h1').remove();
	$('.cards').empty();
	var message;
	if (userPoint === 5) {
	    message = "WINNER!!!";
	    window.navigator.vibrate(500);
	}
	else if (computerPoint === 5) {
	    message = "LOSER :(";
	}
        $('#two').append('<h1 style="font-size: 80px; margin: auto">' + message + '</h1>');
	flag = 0;
    }
    else if (flag === 1 && (userPoint !== 5 || computerPoint !== 5)) {
    $('h1').remove();
    $('.cards').empty();
    $('#info').append('<h1>Choose one</h1>');
    $('#one').append($papercard);
    $('#two').append($rockcard);
    $('#three').append($scissorscard);
    flag = 0;
    }

};



var playGame = function(userChoice) {
    
    var computerChoice = Math.random();
            if (computerChoice < 0.34) {
                    computerChoice = "rock";
                    $('#three').append('<img src="images/rock.jpg" />');
            } else if(computerChoice >= 0.34 && computerChoice < 0.67) {
                    computerChoice = "paper";
                     $('#three').append('<img src="images/paper.jpg" />');
            } else {
                    computerChoice = "scissors";
                     $('#three').append('<img src="images/scissors.jpg" />');
            }
            
            var result = compare(userChoice,computerChoice);
            if (result === 1) {
                userPoint += 1;
                $('#info').append('<h1>YOU WIN</h1>');
		window.navigator.vibrate(200);
            }
            else if(result === 2){
                computerPoint += 1;
                $('#info').append('<h1>YOU LOSE</h1>');
            }
            else if (result === 3) {
		
                $('#info').append('<h1>PAR</h1>');
            }
            
            $('#yPoint').html(userPoint);
            $('#cPoint').html(computerPoint);
	    
            
};

var compare = function(choice1,choice2) {
    
    if(choice1 === choice2) {
        return 3;
    }
    
    if(choice1 === "rock") {
        if(choice2 === "scissors") {
            return 1;
            }
            else {
                return 2;
            }
    }
    
    if(choice1 === "paper") {
        if(choice2 === "rock") {
            return 1;
            }
            else {
                return 2;
            }
    }
    
    if(choice1 === "scissors") {
        if(choice2 === "rock") {
            return 2;
            }
            else {
                return 1;
            }
    }
};