$(function() {

	var thePath = $(location).attr('href');
	var urlPart = thePath.indexOf("boxedupfun.com");
	if (urlPart != -1) {
		var root = "http://boxedupfun.com";
	} else {
		var root = "http://localhost/boxedupfun";
	}
	
	$("#gamesuggestion").clearingInput();

  $(".addcollectionlink a").click(function() {
		var postUrl = $(this).parents("form").attr("action");
		var dataString = $(this).parents("form").serialize();		
		$.ajax({
			type: "POST",
			url: root + "/members/save-games/",
			data: dataString,
			success: function() {
				$(".addcollectionlink a").replaceWith("<span class='disabled'>In Your Collection</span>");
				$(".addwishlistlink").remove(); 
			}
		});
    	return false;
	});
	
	$(".addwishlistlink a").click(function() {
		var postUrl = $(this).parents("form").attr("action");
		var dataString = $(this).parents("form").serialize();
		//alert (dataString);return false;
		
		$.ajax({
			type: "POST",
			url: root + "/members/save-games/",
			data: dataString,
			success: function() {
				$(".addwishlistlink a").replaceWith("<span class='disabled'>On Your Wishlist</span>");
			}
		});
    	return false;
	});
	
	$(".addcollectionlist").click(function() {
		var toBeReplaced = $(this);
		var theOriginalID = $(this).parents("li").attr("id");
		var theOriginalIDIndex = theOriginalID.indexOf("-");
		var newGameID = theOriginalID.substr(theOriginalIDIndex + 1);
		$("#addtocollection .additemshere").before('<input name="fields[collection][]" type="hidden" value="' + newGameID + '"/>');
		var postUrl = $("#addtocollection").attr("action");
		var dataString = $("#addtocollection").serialize();		
		$.ajax({
			type: "POST",
			url: root + "/members/save-games/",
			data: dataString,
			success: function() {
				gamelink = $(toBeReplaced).parent(".anotherspan").children(".gamelink").attr("href");
				$(toBeReplaced).parent(".anotherspan").children(".wishlistbutton").replaceWith("<a href='" + gamelink + "?open=review' class='reviewbutton'>Write Review</a>");
				$(toBeReplaced).replaceWith("<span class='disabled '>In Your Collection</span>");
			}
		});
    	return false;
	});
	
	// ADD A GAME TO COLLECTION FROM WISHLIST ANYWHERE BUT YOUR OWN WISHLIST
	$(".addcollectionfromwishlist").click(function() {
		var toBeReplaced = $(this);
		var theOriginalID = $(this).parents("li").attr("id");
		var theOriginalIDIndex = theOriginalID.indexOf("-");
		var newGameID = theOriginalID.substr(theOriginalIDIndex + 1);
		var wishlistnum = $("#addtocollectionfromwishlist .wishlistgame").length;
			if (wishlistnum > 1) {
				$("#addtocollectionfromwishlist").find("input[value=" + newGameID + "]").remove();
				$("#removefromwishlist").find("input[value=" + newGameID + "]").remove();
			} else {
				$("#addtocollectionfromwishlist").find("input[value=" + newGameID + "]").replaceWith("<input name='fields[wishlist][]' type='hidden' value='' class='wishlistgame'/>");
				$("#removefromwishlist").find("input[value=" + newGameID + "]").replaceWith("<input name='fields[wishlist][]' type='hidden' value='' class='wishlistgame'/>");
			}
		$("#addtocollectionfromwishlist .additemshere").before('<input name="fields[collection][]" type="hidden" value="' + newGameID + '"/>');
		var postUrl = $("#addtocollectionfromwishlist").attr("action");
		var dataString = $("#addtocollectionfromwishlist").serialize();		
		$.ajax({
			type: "POST",
			url: root + "/members/save-games/",
			data: dataString,
			success: function() {
				gamelink = $(toBeReplaced).parent(".anotherspan").children(".gamelink").attr("href");
				$(toBeReplaced).parent(".anotherspan").children(".wishlistbutton").replaceWith("<a href='" + gamelink + "?open=review' class='reviewbutton'>Write Review</a>");
				$(toBeReplaced).replaceWith("<span class='disabled collectionbutton'>In Your Collection</span>");
			}
		});
    	return false;
	});
	
	// ADD A GAME TO COLLECTION FROM WISHLIST WHEN YOU ARE AT YOUR OWN WISHLIST
	$(".addcollectionfrommyownwishlist").click(function() {
		var toBeReplaced = $(this);
		var theOriginalID = $(this).parents("li").attr("id");
		var theOriginalIDIndex = theOriginalID.indexOf("-");
		var newGameID = theOriginalID.substr(theOriginalIDIndex + 1);
		var wishlistnum = $("#addtocollectionfromwishlist .wishlistgame").length;
			if (wishlistnum > 1) {
				$("#addtocollectionfromwishlist").find("input[value=" + newGameID + "]").remove();
				$("#removefromwishlist").find("input[value=" + newGameID + "]").remove();
			} else {
				$("#addtocollectionfromwishlist").find("input[value=" + newGameID + "]").replaceWith("<input name='fields[wishlist][]' type='hidden' value='' class='wishlistgame'/>");
				$("#removefromwishlist").find("input[value=" + newGameID + "]").replaceWith("<input name='fields[wishlist][]' type='hidden' value='' class='wishlistgame'/>");
			}
		$("#addtocollectionfromwishlist .additemshere").before('<input name="fields[collection][]" type="hidden" value="' + newGameID + '"/>');
		var postUrl = $("#addtocollectionfromwishlist").attr("action");
		var dataString = $("#addtocollectionfromwishlist").serialize();		
		$.ajax({
			type: "POST",
			url: root + "/members/save-games/",
			data: dataString,
			success: function() {
				if (wishlistnum > 1) {
					$(toBeReplaced).parents("li").remove();
				} else {
					profilename = $("#profilename h1").text();
					$(toBeReplaced).parents("ul").replaceWith("<h4>No Games Added to " + profilename + "\'s Wishlist Yet!</h4>");
				}
			}
		});
    	return false;
	});
	
	$(".addwishlist").click(function() {
		var toBeReplaced = $(this);
		var theOriginalID = $(this).parents("li").attr("id");
		var theOriginalIDIndex = theOriginalID.indexOf("-");
		var newGameID = theOriginalID.substr(theOriginalIDIndex + 1);
		$("#addtowishlist .additemshere").before('<input name="fields[wishlist][]" type="hidden" value="' + newGameID + '"/>');
		var postUrl = $("#addtowishlist").attr("action");
		var dataString = $("#addtowishlist").serialize();		
		$.ajax({
			type: "POST",
			url: root + "/members/save-games/",
			data: dataString,
			success: function() {
				$(toBeReplaced).replaceWith("<span class='disabled wishlistbutton'>In Your Wishlist</span>");
			}
		});
    	return false;
	});
	
	$(".friendperson").click(function() {
		var toBeReplaced = $(this);
		var theOriginalID = $("h1").attr("id");
		var theOriginalIDIndex = theOriginalID.indexOf("-");
		var newFriendID = theOriginalID.substr(theOriginalIDIndex + 1);
		$("#friendperson .additemshere").before('<input name="fields[friends][]" type="hidden" value="' + newFriendID + '"/>');
		var postUrl = $("#friendperson").attr("action");
		var dataString = $("#friendperson").serialize();		
		$.ajax({
			type: "POST",
			url: root + "/members/save-games/",
			data: dataString,
			success: function() {
				$(toBeReplaced).replaceWith("<span class='button disabled'>Your Friend!</span>");
			}
		});
    	return false;
	});
	
	$(".removefriend").click(function() {
		var toBeReplaced = $(this);
		var theOriginalID = $("h1").attr("id");
		var theOriginalIDIndex = theOriginalID.indexOf("-");
		var newFriendID = theOriginalID.substr(theOriginalIDIndex + 1);
		var friendsnum = $("#friendperson .friends").length;
			if (friendsnum > 1) {
				$("#friendperson").find("input[value=" + newFriendID + "]").remove();
			} else {
				$("#friendperson").find("input[value=" + newFriendID + "]").replaceWith("<input name='fields[friends][]' type='hidden' value='' class='friends'/>");
			}
		var postUrl = $("#friendperson").attr("action");
		var dataString = $("#friendperson").serialize();		
		$.ajax({
			type: "POST",
			url: root + "/members/save-games/",
			data: dataString,
			success: function() {
				$(toBeReplaced).parent(".button").replaceWith("<a href='#' class='button friendperson'>Friend This Person</a>");
			}
		});
    	return false;
	});
	
	$(".removecollectionlist").click(function() {
		var toBeReplaced = $(this);
		var theOriginalID = $(this).parents("li").attr("id");
		var theOriginalIDIndex = theOriginalID.indexOf("-");
		var removedGameID = theOriginalID.substr(theOriginalIDIndex + 1);
		var gameName = $(this).parents("li").find(".name").html();
		var r=confirm("Are you sure you want to remove " + gameName + " from your Collection?");
		if (r==true) {
			var collectionnum = $("#removefromcollection .collectiongame").length;
			if (collectionnum > 1) {
				$("#removefromcollection").find("input[value=" + removedGameID + "]").remove();
			} else {
				$("#removefromcollection").find("input[value=" + removedGameID + "]").replaceWith("<input name='fields[collection][]' type='hidden' value='' class='collectiongame'/>");
			}
			var postUrl = $("#removefromcollection").attr("action");
			var dataString = $("#removefromcollection").serialize();
			$.ajax({
				type: "POST",
				url: root + "/members/save-games/",
				data: dataString,
				success: function() {
					if (collectionnum > 1) {
						$(toBeReplaced).parents("li").remove();
					} else {
						profilename = $("#profilename h1").text();
						$(toBeReplaced).parents("ul").replaceWith("<h4>No Games Added to " + profilename + "\'s Collection Yet!</h4>");
					}
				}
			});
		} else {
			return false;
		}
    	return false;
	});
	
	$(".removewishlist").click(function() {
		var toBeReplaced = $(this);
		var theOriginalID = $(this).parents("li").attr("id");
		var theOriginalIDIndex = theOriginalID.indexOf("-");
		var removedGameID = theOriginalID.substr(theOriginalIDIndex + 1);
		var gameName = $(this).parents("li").find(".name").html();
		var r=confirm("Are you sure you want to remove " + gameName + " from your Wishlist?");
		if (r==true) {
			var wishlistnum = $("#removefromwishlist .wishlistgame").length;
			if (wishlistnum > 1) {
				$("#removefromwishlist").find("input[value=" + removedGameID + "]").remove();
				$("#addtocollectionfromwishlist").find("input[value=" + removedGameID + "]").remove();
			} else {
				$("#removefromwishlist").find("input[value=" + removedGameID + "]").replaceWith("<input name='fields[wishlist][]' type='hidden' value='' class='wishlistgame'/>");
				$("#addtocollectionfromwishlist").find("input[value=" + removedGameID + "]").replaceWith("<input name='fields[wishlist][]' type='hidden' value='' class='wishlistgame'/>");
			}
			var postUrl = $("#removefromwishlist").attr("action");
			var dataString = $("#removefromwishlist").serialize();		
			$.ajax({
				type: "POST",
				url: root + "/members/save-games/",
				data: dataString,
				success: function() {
					if (wishlistnum > 1) {
						$(toBeReplaced).parents("li").remove();
					} else {
						profilename = $("#profilename h1").text();
						$(toBeReplaced).parents("ul").replaceWith("<h4>No Games Added to " + profilename + "\'s Wishlist Yet!</h4>");
					}
				}
			});
		} else {
			return false;
		}
    	return false;
	});
	
	    
	
	$(".addreview").click(function() {
		$("#newreview").slideToggle("slow");
		return false;
	});
	
	$("#advancedsearchbutton").click(function() {
		$("#advancedsearch").slideToggle("slow");
		return false;
	});
	
	$(".gamelist li").hover(function () {
		$(this).addClass("hover");
	}, function () {
		$(this).removeClass("hover");
	});
	
	$(".please-log-in").click(function() {
		$("#log-in-screen").children("p.registermessage").replaceWith('<p class="loginmessage validation-summary error">You will need to be logged in for this feature to work. If you are new to the site, <a href="' + root + '/members/register/">learn more</a> about becoming a member.</p>');
		$('#log-in-screen').lightbox_me({
			closeSelector: '.closer',
			centered: true,
			onClose: function() { 
            	$("#log-in-screen").children("p.loginmessage").replaceWith('<p class="registermessage">If you are new to the site, <a href="' + root + '/members/register/">learn more</a> about becoming a member.</p>');
            },
			onLoad: function() { 
            	$('#log-in-screen').find('input:first').focus();
            }
		});
		return false;
	});
	
	$(".loginpopup").click(function() {
		$('#log-in-screen').lightbox_me({
			closeSelector: '.closer',
			centered: true,
			onClose: function() { 
            	// nothing right now
            },
			onLoad: function() { 
            	$('#log-in-screen').find('input:first').focus();
            }
		});
		return false;
	});
	
	$("#editprofileform").submit(function() {
		var theCity = $("#fields-city").val();
		var theState = $("#fields-state").val();
		var theCountry = $("#fields-country").val();
		$("#fields-location").val(theCity + ", " + theState + ", " + theCountry);
	});
	
	$(".save-profile").click(function() {
		$(this).parents("form").submit();
		return false;
	});
	
	$(".changeavatar").click(function() {
		$(".filereplacement").html('<label>Profile Picture<br /><input name="fields[avatar]" id="fields-avatar" title="" class="afileinput" type="file" value=""/></label>');
		return false;
	});
	
	$("#log-in-screen form").submit(function() {
		var theValue = $(this).find("#fields-username").val();
		var valuePart = theValue.indexOf("@");
		if (valuePart != -1) {
			$(this).find("#fields-username").attr("name", "fields[email]");
		}
	});
	
	$("#log-in-screen #fields-username").prev("label").text("Username or Email");
	
	
	
	//$("#fields-username").clearingInput();
	//$("#fields-password").clearingInput();
	
	//$(".submit").html('<div><a href="#" class="button submitform">Log In</a></div>');
	//$(".submitform").click(function() {
	//	$(this).parents("form").submit();
	//	return false;
	//});
	
	$(".theslider").slider({
		value:0,
		min: -10,
		max: 10,
		step: 2,
		slide: function( event, ui ) {
			$(this).next("input").val( ui.value );
		}
	});
	
	$("#rating").slider({
		value:0,
		min: 0,
		max: 10,
		step: 1,
		slide: function( event, ui ) {
			$(this).next("input").val( ui.value );
			$( "#ratingresult" ).html( ui.value );
		}
	});
	
	$(".theslidersearch").slider({
		value:0,
		min: -10,
		max: 10,
		step: 2,
		slide: function( event, ui ) {
			$(this).next("input").val( (ui.value-2) + "," + (ui.value-1) + "," + ui.value + "," + (ui.value+1) + "," + (ui.value+2) );
		}
	});
	
	
	var theFilledLuckStrategy = $("#filled-luck-strategy").attr("title");
	$("#filled-luck-strategy").slider({
		value:theFilledLuckStrategy,
		disabled: true,
		min: -10,
		max: 10,
		step: 2,
	});
	
	var theFilledCasualIntense = $("#filled-casual-intense").attr("title");
	$("#filled-casual-intense").slider({
		value:theFilledCasualIntense,
		disabled: true,
		min: -10,
		max: 10,
		step: 2,
	});
	
	var theFilledComplexSimple = $("#filled-complex-simple").attr("title");
	$("#filled-complex-simple").slider({
		value:theFilledComplexSimple,
		disabled: true,
		min: -10,
		max: 10,
		step: 2,
	});
	
	var theFilledRating = $("#filled-rating").attr("title");
	$("#filled-rating").slider({
		value: theFilledRating,
		disabled: true,
		min: 0,
		max: 10,
		step: 1
	});
	
});
