/**
 * 
 * Honey Profile functionality.
 * 
 */

function setFilterForProfile(currentIndex) {
	for (i=1; i<5; i++) {
		if(i==currentIndex) {
			$("#profileImage"+i).css("opacity","");
			$("#profileImage"+i).css("filter","");
		} else {
			$("#profileImage"+i).css("opacity","0.4");
			$("#profileImage"+i).css("filter","alpha(opacity=40)");
		}
	}
}

function setProfileStars(selectedId) {
	for(j=0; j<selectedId; j++) {
		$("#profileVote"+j).attr("src","images/honeySearch/starRed3.png");
	}
	for(j=selectedId; j<10; j++) {
		$("#profileVote"+j).attr("src","images/honeySearch/starGold3.png");
	}
}
/*function setCurrentRatingStars(selectedId) {
	for(j=0; j<selectedId; j++) {
		$("#currentRating"+j).attr("src","images/honeySearch/starRed.png");
	}
	for(j=selectedId; j<10; j++) {
		$("#currentRating"+j).attr("src","images/honeySearch/starGold.png");
	}
}*/

function setupUserRating() {
	for (i=0; i<10; i++) {
		$("#profileVote"+i).hover(function() {
			$(this).css('cursor', 'pointer');
			selectedId = parseInt(this.id.substring(11))+1;
			setProfileStars(selectedId);
		}, function() {
			setProfileStars(userRating);
		});
		$("#profileVote"+i).click(function(i) {
			selectedId = parseInt(this.id.substring(11))+1;
			
			// Post rating
			$.post("data/ratingHandler.php",{honeyId:honeyId, rating:selectedId},function (data) {
				$("#honeyProfileDescription").hide();
				var feedbackPosition = -12+  (22*selectedId);
				$("#profileFeedbackHolder").css("margin-left",feedbackPosition+"px");
				if(data.error == 0) {
					userRating = selectedId;					
					//setCurrentRatingStars(data.newRating);
					$("#honeyProfileThankYou").show();
				} else if(data.error == 1) {
					$("#honeyProfileLogin").show();
				} else if(data.error == 2) {
					$("#honeyProfileJudgeOnly").show();
				}
			},"json");
		});
	}
}

function setupMeter() {
	$('#skipThisHoney').hover(function() {
		$(this).css('cursor', 'pointer');
	});
	$('#skipThisHoney').click(function() {
		$.post("data/ratingHandler.php",{honeyId:honeyId, skipHoney:1},function (data) {
			window.location.reload();
		},"json");
	});

	$('#meterHolder').css('background','url(./images/honeyMeter/n'+userRating+'.png) no-repeat scroll 0 0 transparent');
	for (i=1; i<11; i++) {
		$("#meterHolder"+i).hover(function() {
			$(this).css('cursor', 'pointer');
			selectedId = parseInt(this.id.substring(11));
			$('#meterHolder').css('background','url(./images/honeyMeter/n'+selectedId+'.png) no-repeat scroll 0 0 transparent');
		}, function() {
			$('#meterHolder').css('background','url(./images/honeyMeter/n'+userRating+'.png) no-repeat scroll 0 0 transparent');
		});
		
		$("#meterHolder"+i).click(function() {
			selectedId = parseInt(this.id.substring(11));
			
			// Post rating
			$.post("data/ratingHandler.php",{honeyId:honeyId, rating:selectedId},function (data) {
				var meterPosition = 495 - (45*selectedId);
				$("#meterFeedbackHolder").css("margin-top",meterPosition+"px");
				if(data.error == 0) {
					userRating = selectedId;
					//currentRating = data.newRating;
					//$("#honeyMeterCurrentRatingHolder").text(data.newRating);
					$("#honeyMeterUserRatingHolder").attr('src','images/honeyMeter/t'+userRating+'.png');
					$("#honeyProfileThankYou").show();
					setTimeout("window.location.reload();",1000);
				} else if(data.error == 1) {
					$("#honeyProfileLogin").show();
				} else if(data.error == 2) {
					$("#honeyProfileJudgeOnly").show();
				}
			},"json");
		});
	}
}

var honeyBrowserScrollPosition = 0;
$(document).ready(function(){
	var isProfile = document.location.href.indexOf("honeyProfile.php") > -1;
	var isMeter = document.location.href.indexOf("honeyGlance.php") > -1;
	
	if(isProfile || isMeter) {
		
		// Setup thumbnails
		$("#profileImage1").hover(function() {
			$(this).css('cursor', 'pointer');
		});
		$("#profileImage2").hover(function() {
			$(this).css('cursor', 'pointer');
		});
		$("#profileImage3").hover(function() {
			$(this).css('cursor', 'pointer');
		});
		$("#profileImage4").hover(function() {
			$(this).css('cursor', 'pointer');
		});
		$("#profileImage1").click(function() {
			setFilterForProfile(1);
			$("#profileImageMain").attr('src','data/getImage.php?imageId='+imageId1+'&type=0');
		});
		$("#profileImage2").click(function() {
			setFilterForProfile(2);
			$("#profileImageMain").attr('src','data/getImage.php?imageId='+imageId2+'&type=0');
		});
		$("#profileImage3").click(function() {
			setFilterForProfile(3);
			$("#profileImageMain").attr('src','data/getImage.php?imageId='+imageId3+'&type=0');
		});
		$("#profileImage4").click(function() {
			setFilterForProfile(4);
			$("#profileImageMain").attr('src','data/getImage.php?imageId='+imageId4+'&type=0');
		});
		
		// Setup meters
		setFilterForProfile(1);
		if(isProfile) {
			setupUserRating();
		} else if(isMeter) {
			setupMeter();
		}
		
		// Report Form
		$("#honeySearchReportFormClose").hover(function() {
			$(this).css('cursor', 'pointer');
		});
		$("#honeySearchReportConfirmationClose").hover(function() {
			$(this).css('cursor', 'pointer');
		});
		$("#honeySearchReportFormClose").click(function() {
			$("#honeySearchReportPopup").hide();
			$("#blackoutHoneySearchHolder").hide();
		});
		$("#honeySearchReportConfirmationClose").click(function() {
			$("#honeySearchReportConfirmationPopup").hide();
			$("#blackoutHoneySearchHolder").hide();
		});
		$("#honeySearchReportConfirmationContinue").hover(function() {
			$(this).css('cursor', 'pointer');
		});
		$("#honeySearchReportConfirmationContinue").click(function() {
			$("#honeySearchReportConfirmationPopup").hide();
			$("#blackoutHoneySearchHolder").hide();
		});
		$("#honeySearchReportText").keyup(function(){
			limitProfileQuestion($("#honeySearchReportText"));
		});
		$("#honeySearchReportSubmit").hover(function() {
			$(this).css('cursor', 'pointer');
		});
		$("#honeySearchReportSubmit").click(function() {
			var locationId = $("#honeySearchReportLocation").val();
			var typeId = $("#honeySearchReportType").val();
			var additionalInformation = $("#honeySearchReportText").val();
			$.post("data/reportHandler.php",{honeyId:honeyId, locationId:locationId, typeId:typeId, additionalInformation:additionalInformation},function (data) {
				$("#honeySearchReportPopup").hide();
				$("#honeySearchReportConfirmationPopup").show();
			},"json");
		});
		
		// Honey Browser
		$("#honeyBrowser").scrollLeft(honeyBrowserScrollPosition);
		$("#honeyBrowserLeftArrow").click(function() {
			if(honeyBrowserScrollPosition>0){
				honeyBrowserScrollPosition -= 42;
				$("#honeyBrowser").scrollLeft(honeyBrowserScrollPosition);
			}
			return false;
		});
		$("#honeyBrowserRightArrow").click(function() {
			if(honeyBrowserScrollPosition<168){
				honeyBrowserScrollPosition+=42;
				$("#honeyBrowser").scrollLeft(honeyBrowserScrollPosition);
			}
			return false;
		});
	}
});
