$(document).ready(function() {
	var flickrGame = {
		APIKEY: "76656089429ab3a6b97d7c899ece839d",
		imageArray: [],
		tempImages:[],
		theImages: [],
		chosenCards: [],
		numberPhotosReturned: 0,
		imageNum: 0
    }
	 function randOrd() {
        return (Math.round(Math.random()) - 0.5);
    }
	function doSearch() {
	    if ($("#searchterm").val() != "") {
	        $(document).off("keypress");
			 $("#infoprogress").css({
                'visibility': 'visible'
            });
            var searchURL = "http://api.flickr.com/services/rest/?method=flickr.photos.search";
            searchURL += "&api_key=" + flickrGame.APIKEY;
            searchURL += "&tags=" + $("#searchterm").val();
            searchURL += "&per_page=36"
            searchURL += "&license=5,7";
            searchURL += "&format=json";
            searchURL += "&jsoncallback=?";
            $.getJSON(searchURL, setImages);
        }
	}
	
	function setImages(data) {
	$("#infoprogress").css({
		'visibility': 'hidden'
	});
	 $("#progressdiv").css({
            'visibility': 'visible'
        });
    $("#progressdiv p").text("Loading Photo Information");
	if (data.photos.photo.length >= 12) {
		$("#searchdiv").css({
			'visibility': 'hidden'
		});
		$.each(data.photos.photo, function (i, item) {
			var imageURL = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '_' + 'q.jpg';
			flickrGame.imageArray.push({
				imageUrl: imageURL,
				photoid: item.id
			});
            var getPhotoInfoURL = "http://api.flickr.com/services/rest/?method=flickr.photos.getInfo";
			getPhotoInfoURL += "&api_key=" + flickrGame.APIKEY;
			getPhotoInfoURL += "&photo_id=" + item.id;
			getPhotoInfoURL += "&format=json";
			getPhotoInfoURL += "&jsoncallback=?";
            $.getJSON(getPhotoInfoURL, infoLoaded);
		});
	} else {
	      $("#progressdiv").css({
                'visibility': 'hidden'
        });
		alert("NOT ENOUGH IMAGES WERE RETURNED");
		addKeyPress();
   }
	flickrGame.numberPhotosReturned = flickrGame.imageArray.length;
   }
	
	    function infoLoaded(data) {
        flickrGame.imageNum += 1;
		var percentage = Math.floor(flickrGame.imageNum / flickrGame.numberPhotosReturned * 100);
        $("#progressbar").progressbar({
            value: percentage
        });
        var index = 0;
        for (var i = 0; i < flickrGame.imageArray.length; i++) {
            if (flickrGame.imageArray[i].photoid == data.photo.id) {
                index = i
                flickrGame.imageArray[index].username = data.photo.owner.username;
                flickrGame.imageArray[index].photoURL = data.photo.urls.url[0]._content;
            }
        }
        if (flickrGame.imageNum == flickrGame.numberPhotosReturned) {
            preloadImages();
        }
    }
	
	
	function preloadImages(){
		flickrGame.tempImages = flickrGame.imageArray.splice(0, 12);
        for (var i = 0; i < flickrGame.tempImages.length; i++) {
            for (var j = 0; j < 2; j++) {
                var tempImage = new Image();
                tempImage.src = "cardFront.png";
                tempImage.imageSource = flickrGame.tempImages[i].imageUrl;
                flickrGame.theImages.push(tempImage);
               
            }
        }
		drawImages();
	}
	
	function drawImages() {
		flickrGame.theImages.sort(randOrd);
        for (var i = 0; i < flickrGame.theImages.length; i++) {
            $(flickrGame.theImages[i]).attr("class", "card").appendTo("#gamediv");
        }
		addListeners();
    }
	
	function addListeners() {
        for (var i = 0; i < flickrGame.theImages.length; i++) {
            $(flickrGame.theImages[i]).on("click", function (e) {
                doFlip(e);
            });
        }
    }
	
	function removeListeners() {
        for (var i = 0; i < flickrGame.theImages.length; i++) {
            $(flickrGame.theImages[i]).off("click");
        }
    }
	
	function doFlip(e) {
        var theCard = e.target;
        $(theCard).attr("src", theCard.imageSource);
        if ($('#image1').css('backgroundImage') == "none") {
            $('#image1').css('backgroundImage', 'url(' + theCard.imageSource + ')');
        } else {
            $('#image2').css('backgroundImage', 'url(' + theCard.imageSource + ')');
        }
        if (flickrGame.chosenCards.indexOf(theCard) == -1) {
            flickrGame.chosenCards.push(theCard);
            $(theCard).off("click");
        }

        if (flickrGame.chosenCards.length == 2) {
            removeListeners();
            checkForMatch();

        }
    }
	
	 function checkForMatch() {
        if (flickrGame.chosenCards.length == 2) {
            if ($("#image1").css('background-image') == $("#image2").css('background-image')) {
				setTimeout(hideCards, 1000);
            } else {
                setTimeout(resetImages, 1000);
            }
        }

    }
	
	 function hideCards() {
        $(flickrGame.chosenCards[0]).animate({
            'opacity': '0'
        });
        $(flickrGame.chosenCards[1]).animate({
            'opacity': '0'
        });
        flickrGame.theImages.splice(flickrGame.theImages.indexOf(flickrGame.chosenCards[0]), 1);
        flickrGame.theImages.splice(flickrGame.theImages.indexOf(flickrGame.chosenCards[1]), 1);
        $("#image1").css('background-image', 'none');
        $("#image2").css('background-image', 'none');
        addListeners();
        flickrGame.chosenCards = new Array();

    }
	
	
    function resetImages() {
        $(flickrGame.chosenCards[0]).attr("src", "cardFront.png");
        $(flickrGame.chosenCards[1]).attr("src", "cardFront.png");
        $("#image1").css('background-image', 'none');
        $("#image2").css('background-image', 'none');
        addListeners();
        flickrGame.chosenCards = new Array();
    }
    function addKeyPress() {
        $(document).on("keypress", function (e) {
            if (e.keyCode == 13) {
              doSearch();
        }
        });
    }
    
	addKeyPress();
});