//JS
function navDropdown() {
    var subNav = document.getElementById('sub-nav-list');

    if (subNav.style.display != 'none') {
        subNav.style.display = 'none';
    }else{
        subNav.style.display = 'block';
    }
}

function navDropdownRent() {
    var subNav = document.getElementById('sub-nav-list-rent');

    if (subNav.style.display != 'none') {
        subNav.style.display = 'none';
    }else{
        subNav.style.display = 'block';
    }
}

function shelfNavDropdown() {
    var subNav = document.getElementById('shelf-sub-nav-list');

    if (subNav.style.display != 'none') {
        subNav.style.display = 'none';
    }else{
        subNav.style.display = 'block';
    }
}

function shelfNavDropdownEarn() {
    var subNav = document.getElementById('shelf-sub-nav-list-earn');

    if (subNav.style.display != 'none') {
        subNav.style.display = 'none';
    }else{
        subNav.style.display = 'block';
    }
}

function updateButton(button, buttonClass, buttonText, clickFunction, remove) {
    //change the state of the button in the tooltip
    var cl = document.getElementById(button);
    cl.className = buttonClass;
    cl.innerHTML = buttonText;
    if (remove == true) {
        cl.href = clickFunction;
        cl.onclick = null;
    } else {
        cl.onclick = new Function(clickFunction);
    }
}

function updateRentalCart(num) {
    var sc = document.getElementById('cartCount');
    var games = parseInt(sc.innerHTML) + num;
    sc.innerHTML = games;
    return (games);
}

function updateMarketPlaceCart(num) {
    var sc = document.getElementById('cartCount');
    var games = parseInt(sc.innerHTML) + num;
    sc.innerHTML = games;
    return (games);
}

function checkForObject(id) {
    var o = document.getElementById(id);
    if (o) {
        return o;
    } else {
        return null;
    }

}


function addToCart(gid, size) {
    if (size == 'small') {
        updateButton('cartLink_' + gid, 'remove-button-game', 'In Cart', "https://www.gamehuddle.com/account/shoppingcart.aspx", true); //update button
    } else if (size == 'medium') {
        updateButton('cartLink_' + gid, 'remove-button', 'In Cart', "https://www.gamehuddle.com/account/shoppingcart.aspx", true); //update button

        //check if game is also featured
        var featured = checkForObject('featuredLink_' + gid);
        if (featured) {
            updateButton('featuredLink_' + gid, 'remove-button-featured', 'In Cart', "https://www.gamehuddle.com/account/shoppingcart.aspx", true); //update button
        }
    } else if (size == 'large') {
        updateButton('cartLink_' + gid, 'rent-game', 'This game is in your rental cart.', "https://www.gamehuddle.com/account/shoppingcart.aspx", true); //update button

        //check if game is also featured
        var featured = checkForObject('featuredLink_' + gid);
        if (featured) {
            updateButton('featuredLink_' + gid, 'remove-button-featured', 'In Cart', "https://www.gamehuddle.com/account/shoppingcart.aspx", true); //update button
        }
    } else if (size == 'featured') {
        updateButton('featuredLink_' + gid, 'remove-button-featured', 'In Cart', "https://www.gamehuddle.com/account/shoppingcart.aspx", true);   //update button

        //check if game is also featured
        var featured = checkForObject('cartLink_' + gid);
        if (featured) {
            updateButton('cartLink_' + gid, 'remove-button-featured', 'In Cart', "https://www.gamehuddle.com/account/shoppingcart.aspx", true); //update button
        }
    }

    var games = updateRentalCart(1); //update rental cart count at top
    shoppingCart.addGameToCart(gid, 1); //add game

	if ($('.modalPriceWrap').hasClass('shown')){
		$('.modalPriceWrap').removeClass('shown').addClass('hidden');
	}
	
	if ($('.modalRentWrap').hasClass('hidden')){
		$('.modalRentWrap').removeClass('hidden').addClass('shown');
	}	
	
    if ($('.modalRentWrap').is(':empty')) {
        $('.modalRentWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('#overlay').overlay({ onBeforeLoad: function () {
            var wrap = this.getOverlay().find(".modalRentWrap"); wrap.load("/ajax/rentpopup.aspx?gid=" + gid);}, load: true
        });
    }else {
        $('.modalRentWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('.modalRentWrap').load("/ajax/rentpopup.aspx?gid=" + gid);

        $('#overlay').fadeIn();
        $('.close').bind('click', function () { document.getElementById('overlay').style.display = "none"; });
    }
}

function addToMPCart(gid, size) {
    if (size == 'small') {
        updateButton('cartLink_' + gid, 'remove-button-game', 'In Cart', "https://www.gamehuddle.com/marketplace/shoppingcart.aspx", true); //update button
    } else if (size == 'medium') {
        updateButton('cartLink_' + gid, 'remove-button', 'In Cart', "https://www.gamehuddle.com/marketplace/shoppingcart.aspx", true); //update button

        //check if game is also featured
        var featured = checkForObject('featuredLink_' + gid);
        if (featured) {
            updateButton('featuredLink_' + gid, 'remove-button-featured', 'In Cart', "https://www.gamehuddle.com/marketplace/shoppingcart.aspx", true); //update button
        }
    } else if (size == 'large') {
        updateButton('cartLink_' + gid, 'rent-game', 'This game is in your rental cart.', "https://www.gamehuddle.com/marketplace/shoppingcart.aspx", true); //update button

        //check if game is also featured
        var featured = checkForObject('featuredLink_' + gid);
        if (featured) {
            updateButton('featuredLink_' + gid, 'remove-button-featured', 'In Cart', "https://www.gamehuddle.com/marketplace/shoppingcart.aspx", true); //update button
        }
    } else if (size == 'featured') {
        updateButton('featuredLink_' + gid, 'remove-button-featured', 'In Cart', "https://www.gamehuddle.com/marketplace/shoppingcart.aspx", true);   //update button

        //check if game is also featured
        var featured = checkForObject('cartLink_' + gid);
        if (featured) {
            updateButton('cartLink_' + gid, 'remove-button-featured', 'In Cart', "https://www.gamehuddle.com/marketplace/shoppingcart.aspx", true); //update button
        }
    }

    var games = updateMarketPlaceCart(1); //update purchase cart count at top
    marketplaceService.addGameToMPCart(gid, games); //add game

    if ($('.modalPriceWrap').hasClass('shown')) {
        $('.modalPriceWrap').removeClass('shown').addClass('hidden');
    }

    if ($('.modalRentWrap').hasClass('hidden')) {
        $('.modalRentWrap').removeClass('hidden').addClass('shown');
    }

    if ($('.modalRentWrap').is(':empty')) {
        $('.modalRentWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('#overlay').overlay({ onBeforeLoad: function () {
            var wrap = this.getOverlay().find(".modalRentWrap"); wrap.load("/ajax/marketplacepopup.aspx?gid=" + gid);
        }, load: true
        });
    } else {
        $('.modalRentWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('.modalRentWrap').load("/ajax/marketplacepopup.aspx?gid=" + gid);

        $('#overlay').fadeIn();
        $('.close').bind('click', function () { document.getElementById('overlay').style.display = "none"; });
    }
}

function priceOverlay() {
	if ($('.modalRentWrap').hasClass('shown')){
		$('.modalRentWrap').removeClass('shown').addClass('hidden');
	}
	
	if ($('.modalPriceWrap').hasClass('hidden')){
		$('.modalPriceWrap').removeClass('hidden').addClass('shown');
	}	
		
    if ($('.modalPriceWrap').is(':empty')) {
        $('.modalPriceWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('#overlay').overlay({ onBeforeLoad: function () {
            var wrap = this.getOverlay().find(".modalPriceWrap"); wrap.load("/ajax/price.htm");}, load: true
        });
    }else {
        $('.modalPriceWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('.modalPriceWrap').load("/ajax/price.htm");

        $('#overlay').fadeIn();
        $('.close').bind('click', function () { document.getElementById('overlay').style.display = "none"; });
    }
}

function auggieOverlay() {	
    if ($('.modalAuggieWrap').is(':empty')) {
        $('.modalAuggieWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('#overlay').overlay({ onBeforeLoad: function () {
            var wrap = this.getOverlay().find(".modalAuggieWrap"); wrap.load("/ajax/auggie.htm");}, load: true
        });
    }else {
        $('.modalAuggieWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('.modalAuggieWrap').load("/ajax/auggie.htm");

        $('#overlay').fadeIn();
        $('.close').bind('click', function () { document.getElementById('overlay').style.display = "none"; });
    }
}

function cartPriceOverlay() {
	if ($('.modalCardWrap').hasClass('shown')){
		$('.modalCardWrap').removeClass('shown').addClass('hidden');
	}
	
	if ($('.modalPriceWrap').hasClass('hidden')){
		$('.modalPriceWrap').removeClass('hidden').addClass('shown');
	}	
		
    if ($('.modalPriceWrap').is(':empty')) {
        $('.modalPriceWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('#overlay').overlay({ onBeforeLoad: function () {
            var wrap = this.getOverlay().find(".modalPriceWrap"); wrap.load("/ajax/price.htm");}, load: true
        });
    }else {
        $('.modalPriceWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('.modalPriceWrap').load("/ajax/price.htm");

        $('#overlay').fadeIn();
        $('.close').bind('click', function () { document.getElementById('overlay').style.display = "none"; });
    }
}

function cartCardOverlay() {
	if ($('.modalPriceWrap').hasClass('shown')){
		$('.modalPriceWrap').removeClass('shown').addClass('hidden');
	}
	
	if ($('.modalCardWrap').hasClass('hidden')){
		$('.modalCardWrap').removeClass('hidden').addClass('shown');
	}	
		
    if ($('.modalCardWrap').is(':empty')) {
        $('.modalCardWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('#overlay').overlay({ onBeforeLoad: function () {
            var wrap = this.getOverlay().find(".modalCardWrap"); wrap.load("/ajax/cardId.htm");}, load: true
        });
    }else {
        $('.modalCardWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('.modalCardWrap').load("/ajax/cardId.htm");

        $('#overlay').fadeIn();
        $('.close').bind('click', function () { document.getElementById('overlay').style.display = "none"; });
    }
}

function worksOverlay() {

    if ($('.modalHowWrap').is(':empty')) {
        $('.modalHowWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('#overlay').overlay({ onBeforeLoad: function () {
            var wrap = this.getOverlay().find(".modalHowWrap"); wrap.load("/ajax/how.htm");}, load: true
        });
    }else {
        $('.modalHowWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('.modalHowWrap').load("/ajax/how.htm");

        $('#overlay').fadeIn();
        $('.close').bind('click', function () { document.getElementById('overlay').style.display = "none"; });
    }
}

function ccidOverlay() {

    if ($('.modalCardWrap').is(':empty')) {
        $('.modalCardWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('#overlay').overlay({ onBeforeLoad: function () {
            var wrap = this.getOverlay().find(".modalCardWrap"); wrap.load("/ajax/cardId.htm");}, load: true
        });
    }else {
        $('.modalCardWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('.modalCardWrap').load("/ajax/cardId.htm");

        $('#overlay').fadeIn();
        $('.close').bind('click', function () { document.getElementById('overlay').style.display = "none"; });
    }
}

function ccidRegisterOverlay() {

	if ($('.modalHowWrap').hasClass('shown')){
		$('.modalHowWrap').removeClass('shown').addClass('hidden');
	}
	
	if ($('.modalCardWrap').hasClass('hidden')){
		$('.modalCardWrap').removeClass('hidden').addClass('shown');
	}

    if ($('.modalCardWrap').is(':empty')) {
        $('.modalCardWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('#overlay').overlay({ onBeforeLoad: function () {
            var wrap = this.getOverlay().find(".modalCardWrap"); wrap.load("/ajax/cardId.htm");}, load: true
        });
    }else {
        $('.modalCardWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('.modalCardWrap').load("/ajax/cardId.htm");

        $('#overlay').fadeIn();
        $('.close').bind('click', function () { document.getElementById('overlay').style.display = "none"; });
    }
}

function insuranceOverlay() {

    if ($('.modalInsuranceWrap').is(':empty')) {
        $('.modalInsuranceWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('#overlay').overlay({ onBeforeLoad: function () {
            var wrap = this.getOverlay().find(".modalInsuranceWrap"); wrap.load("/ajax/insurance.aspx");}, load: true
        });
    }else {
        $('.modalInsuranceWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('.modalInsuranceWrap').load("/ajax/insurance.aspx");

        $('#overlay').fadeIn();
        $('.close').bind('click', function () { document.getElementById('overlay').style.display = "none"; });
    }
}

function shippingOverlay() {

    if ($('.modalInsuranceWrap').is(':empty')) {
        $('.modalInsuranceWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('#overlay').overlay({ onBeforeLoad: function () {
            var wrap = this.getOverlay().find(".modalInsuranceWrap"); wrap.load("/ajax/shipping.htm");}, load: true
        });
    }else {
        $('.modalInsuranceWrap').html("<img src='/assets/images/ajax-loader.gif' alt='loader'/>");
        $('.modalInsuranceWrap').load("/ajax/shipping.htm");

        $('#overlay').fadeIn();
        $('.close').bind('click', function () { document.getElementById('overlay').style.display = "none"; });
    }
}

function toggleHistory(panel) {
	
	if (panel == 1) {
		if ($('.rental-history-content').hasClass('hidden')){
			$('.rental-history-content').removeClass('hidden').addClass('shown');
			$('.sharing-history-content').removeClass('shown').addClass('hidden');
			$('.rental-history').removeClass('rental-history').addClass('rental-history-active');
			$('.sharing-history-active').removeClass('sharing-history-active').addClass('sharing-history');
		}		
		
	}else {
		if ($('.sharing-history-content').hasClass('hidden')){
			$('.sharing-history-content').removeClass('hidden').addClass('shown');
			$('.rental-history-content').removeClass('shown').addClass('hidden');
			$('.sharing-history').removeClass('sharing-history').addClass('sharing-history-active');
			$('.rental-history-active').removeClass('rental-history-active').addClass('rental-history');
		}		
	}
		
}

function removeFromCart(gid, size) {
    if (size == 'small') {
        updateButton('cartLink_' + gid, 'rent-button-game', 'Rent', "addToCart('" + gid + "', 'small'); return false;", false); //update button
    } else if (size == 'medium') {
        updateButton('cartLink_' + gid, 'rent-button', 'Rent', "addToCart('" + gid + "', 'medium'); return false;", false); //update button

        //check if game is also featured
        var featured = checkForObject('featuredLink_' + gid);
        if (featured) {
            updateButton('featuredLink_' + gid, 'rent-button-featured', 'Rent', "addToCart('" + gid + "', 'featured'); return false;", false);   //update button
        }
    } else if (size == 'large') {
        updateButton('cartLink_' + gid, 'rent-button-game-cover', 'Rent', "addToCart('" + gid + "', 'large'); return false;", false); //update button

        //check if game is also featured
        var featured = checkForObject('featuredLink_' + gid);
        if (featured) {
            updateButton('featuredLink_' + gid, 'rent-button-featured', 'Rent', "addToCart('" + gid + "', 'featured'); return false;", false);   //update button
        }
    } else if (size == 'featured') {
        updateButton('featuredLink_' + gid, 'rent-button-featured', 'Rent', "addToCart('" + gid + "', 'featured'); return false;", false);   //update button

        var featured = checkForObject('cartLink_' + gid);
        if (featured) {
            updateButton('cartLink_' + gid, 'rent-button-game-cover', 'Rent', "addToCart('" + gid + "', 'large'); return false;", false);   //update button
        }
    }

    var games = updateRentalCart(-1); //update rental cart count at top
    shoppingCart.removeGameFromShoppingCart(gid, games); //remove game
}

function removeFromMPCart(gid, size) {
    if (size == 'small') {
        updateButton('cartLink_' + gid, 'rent-button-game', 'Buy', "addToMPCart('" + gid + "', 'small'); return false;", false); //update button
    } else if (size == 'medium') {
        updateButton('cartLink_' + gid, 'rent-button', 'Buy', "addToMPCart('" + gid + "', 'medium'); return false;", false); //update button

        //check if game is also featured
        var featured = checkForObject('featuredLink_' + gid);
        if (featured) {
            updateButton('featuredLink_' + gid, 'rent-button-featured', 'Buy', "addToMPCart('" + gid + "', 'featured'); return false;", false);   //update button
        }
    } else if (size == 'large') {
        updateButton('cartLink_' + gid, 'rent-button-game-cover', 'Buy', "addToMPCart('" + gid + "', 'large'); return false;", false); //update button

        //check if game is also featured
        var featured = checkForObject('featuredLink_' + gid);
        if (featured) {
            updateButton('featuredLink_' + gid, 'rent-button-featured', 'Buy', "addToMPCart('" + gid + "', 'featured'); return false;", false);   //update button
        }
    } else if (size == 'featured') {
        updateButton('featuredLink_' + gid, 'rent-button-featured', 'Buy', "addToMPCart('" + gid + "', 'featured'); return false;", false);   //update button

        var featured = checkForObject('cartLink_' + gid);
        if (featured) {
            updateButton('cartLink_' + gid, 'rent-button-game-cover', 'Buy', "addToMPCart('" + gid + "', 'large'); return false;", false);   //update button
        }
    }

    var games = updateMarketPlaceCart(-1); //update rental cart count at top
    marketplaceService.removeGameFromMPCart(gid, games); //remove game
}

function addToShelf(gid) {
    autocompleteservice.AddGidToCollection(gid); //adds game to shelf
    updateButton('shelfLink_' + gid, 'add-to-shelf', 'Remove this game from your shelf.', "removeFromShelf('" + gid + "'); return false;", false); //update button
}

function addToShelfTooltip(gid) {
    autocompleteservice.AddGidToCollection(gid); //adds game to shelf
    updateButton('shelfLink_' + gid, 'tooltip-add-to-shelf', 'Remove this game from your shelf.', "removeFromShelfTooltip('" + gid + "'); return false;", false); //update button
}

function removeFromShelf(gid) {
    autocompleteservice.RemoveGame(gid); //remove from shelf
    updateButton('shelfLink_' + gid, 'add-to-shelf', 'Own this game?<span class=\"lighter-gray twelve padding-left-5\"><span class=\"no-italic\">[</span> Add it to your shelf. <span class=\"no-italic\">]</span></span>', "addToShelf('" + gid + "'); return false;", false); //update button
}

function removeFromShelfTooltip(gid) {
    autocompleteservice.RemoveGame(gid); //remove from shelf
    updateButton('shelfLink_' + gid, 'tooltip-add-to-shelf', 'Own this game?<span style=\"font-size: 10px; padding-left: 5px;\" class=\"lighter-gray\"><span class=\"no-italic\">[</span> Add it to your shelf. <span class=\"no-italic\">]</span></span>', "addToShelfTooltip('" + gid + "'); return false;", false); //update button
}

function switchMini(cid, returnCount, page) {
    var dd = document.getElementById("ContentPlaceHolder1_miniDisplay1_gameFilter");
    var filter = dd.options[dd.selectedIndex].value;
    var genre = "";

    gameDisplay.getGameDisplay(cid, filter, returnCount, page, genre);
}

function changeMini(mini, page, pages) {
    var pPage = (parseInt(page) - 1);
    var nPage = (parseInt(page) + 1);

    //loop through displays; show/hide appropriately
    for (var i = 1; i <= pages; i++) {
        if (i == page) {
            document.getElementById(mini + "_page_" + i).style.display = "block";
        } else {
            document.getElementById(mini + "_page_" + i).style.display = "none";
        }
    }

    //switch links
    if(pPage == 0){
        document.getElementById(mini + "_leftPage").innerHTML = "<div class='previous-off'><span>Previous</span></div>";
    }else{
        document.getElementById(mini + "_leftPage").innerHTML = "<a class='arrow-left' href=\"javascript:changeMini('" + mini + "', '" + pPage + "', '" + pages + "')\"><span>Previous</span></a>";
    }

    if (nPage > pages) {
        document.getElementById(mini + "_rightPage").innerHTML = "<div class='next-off'><span>Next</span></div>";
    } else {
        document.getElementById(mini + "_rightPage").innerHTML = "<a class='arrow-right' href=\"javascript:changeMini('" + mini + "', '" + nPage + "', '" + pages + "')\"><span>Next</span></a>";
    }
}

$(function () {
    $("img.deferredLoad").each(function () {
        var $this = $(this);
        $this.attr("src", $this.attr("deferredSrc")).removeClass("deferredLoad");
    });
});


