//Rating javascript

	var arrPlaceRating = [];
	var arrNewRatedPlace = [];
	var stars = [];
	var arrSeed = [];
	var arrLogin = [];
	var currRating = 0;
	var starClicked = 0;
	var isLoggedIn = false;
	var hasSeed = false;

	function savePlaceRating(placeID)
	{
		var myAjax = new Ajax.Request('../saveCat.php?job=saveRating&placeID=' + placeID + '&rating=' + currRating + '&comments=' + document.getElementById('txtComment').value.replace(/\n/g,"<br />"),{method: 'post', 
			parameters: "",onComplete: function(transport){
						        var response = transport.responseText;
								if (response != "Nothing")
								{
									//response
									arrNewRatedPlace = [];
									splitRespToArr(arrNewRatedPlace,4,response);
									
									var infoHtml = ""; //ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
									infoHtml += "<div style='font-size: 12px; font-weight: bold; text-align: left;'>" + arrNewRatedPlace[0][1] + "</div>";
									infoHtml += "<div style='font-size: 11px; text-align: left;'>" + arrNewRatedPlace[0][2] + "</div>";
									if (arrNewRatedPlace[0][3] != ""){				
										for (var j=0; j<arrNewRatedPlace[0][3]; j++){
											infoHtml += "<img src='images/rating/filledStar.png' width='15' height='15' border='0' align='left'/>";
										}
										for (var j=0; j<(5-arrNewRatedPlace[0][3]); j++){
											infoHtml += "<img src='images/rating/blankStar.png' width='15' height='15' border='0' align='left'/>";
										}
									}
									infoHtml += "<br/><div style='text-align: left;'><span style='font-size: 11px; cursor: pointer;'>View</span>&nbsp;&nbsp;<span style='font-size: 11px; cursor: pointer;' onclick='showRatingBox(&quot;" + arrNewRatedPlace[0][0] +  "&quot;);'>Rate</span></div>";									
									var found = false;
									var i = 0;
									while ((i < gMarkers.length) && (found == false)){
										if (gMarkers[i][3] == arrNewRatedPlace[0][0]){
											found = true;
											gMarkers[i][0].bindInfoWindowHtml(infoHtml);
										}
										else{
											i++;
										}
									}
									//update mostRated List
									loadMostRated(0);
									
								} 
						}
			}
		);
	}
	function loadPlaceRating(placeID)
	{
		//show progress
		document.getElementById("ratingBox").innerHTML = "<div style='background-image: url(../images/rating/loadingBlack.gif); position: absolute; top: 27px; left: 117px; width: 16px; height: 16px;'></div>";
		var myAjax = new Ajax.Request('../getCat.php?job=loadRating&placeID=' + placeID,{method: 'post', 
			parameters: "",onComplete: function(transport){
						        var response = transport.responseText;
								if (response != "Nothing")
								{
									//arrLoc = [placeID, rateTotal, Name, userRating, userComment]
									arrPlaceRating = [];
									splitRespToArr(arrPlaceRating,5,response);
									showPlaceRating();	
								}
						}
			}
		);
	}

	function checkForUserRating(placeID){
		isLoggedIn = false;
		var myAjax = new Ajax.Request('../getCat.php?job=checkUser',{method: 'post',
									  parameters: "",onComplete: function(transport){
						        		var response = transport.responseText;
										if (response == "true"){
											isLoggedIn = true;
										}
										else{
											isLoggedIn = false;
										}
										if (isLoggedIn == true){
											map.closeInfoWindow();
											showGrey();
											document.getElementById('ratingBox').style.display = 'block';
											fade('ratingBox','in',1000,100);
											
											//AJAX load of rating for current Place
											currRating = 0;
											starClicked = 0;
											loadPlaceRating(placeID);
											
										}
										else{
											//showLoginScreen instead of the rating
											showLoginBox("You must be logged in to rate!");
										}
								}
					});
	}
	
	function logoutUser(){
		var myAjax = new Ajax.Request('../getCat.php?job=logoutUser',{method: 'post',
										parameters: "",onComplete: function(transport){
											isLoggedIn = false;	
											document.getElementById('usersName').innerHTML = "<span id='lnkLoginLogout' onclick='showLoginBox(&quot;Enter your user/password.&quot;);' style='cursor: pointer;'>Login</span> &nbsp;&nbsp;| ";
									}
				
					});
	}
	
	function checkForUser(){
		isLoggedIn = false;
		var myAjax = new Ajax.Request('../getCat.php?job=checkUser',{method: 'post',
									  parameters: "",onComplete: function(transport){
						        		var response = transport.responseText;
										if (response == "true"){
											isLoggedIn = true;
										}
										else{
											isLoggedIn = false;
										}
								}
					});
	}
	
	function showLoginBox(title){
		map.closeInfoWindow();
		showGrey();
		document.getElementById('ratingBox').style.display = 'block';
		fade('ratingBox','in',1000,100);
		showLogin(title);
	}
	
	function showLogout(){
		logoutUser();
	}
	
	function getSeed(){
		var myAjax = new Ajax.Request('../getCat.php?job=getSeed',{method:'post',
									  parameters: "", onComplete: function(transport){
										  var response = transport.responseText;
										  if (response != "Nothing"){
											  splitRespToArr(arrSeed,2,response);
											  var hash = hex_md5(document.getElementById('pass').value + arrSeed[0][1]);
											  tryLogin(document.getElementById('user').value,arrSeed[0][0],hash);
										  }
								}
					});
	}
	
	
	function tryLogin(username,id,hash){
		//show progress
		document.getElementById("ratingBox").innerHTML = "<div style='background-image: url(../images/rating/loadingBlack.gif); position: absolute; top: 52px; left: 142px; width: 16px; height: 16px;'></div>";
		var myAjax = new Ajax.Request('../getCat.php?job=checkLogin&username=' + username + '&id=' + id +'&hash=' + hash,{method:'get',
									  parameters: "", onComplete: function(transport){
										  var response = transport.responseText;
										  if (response != "Nothing"){
											  //login succeded
											  splitRespToArr(arrLogin,2,response);
											  //alert('Welcome ' + arrLogin[0][1]);
											  document.getElementById('usersName').innerHTML = "Welcome " + arrLogin[0][1] + " &nbsp;&nbsp;|&nbsp;&nbsp; <span id='lnkLoginLogout' onclick='showLogout();' style='cursor: pointer;'>Logout</span> &nbsp;&nbsp;| ";
											  //document.getElementById('lnkLoginLogout').innerHTML = 'Login';
											  closeRatingBox();
										  }
										  else{
											 //login failed
											 //show error message, and login box
											 showLogin('Invalid username or password!');
										  }
									  
									  }
									  
					});
	}
	
	

	function fade(id,irany,time,maxOp)
	{
		var timer = 0;
		var speed = Math.round(time / 100);
		if (irany == "in")
		{
			for (var i = 0; i <= maxOp; i++)
			{
				setTimeout("opacity('"+ id + "'," + i + ")",(timer * speed));
				timer++;
			}
		}
		else
		{
			for (var i = maxOp; i >= 0; i--)
			{
				setTimeout("opacity('"+ id + "'," + i + ")",(timer * speed));
				timer++;
			}
		}
	}

//opacity allitas a fade effect-hez

	function opacity(id,op)
	{
		var subject = document.getElementById(id);
		subject.style.opacity = (op / 100); 
		subject.style.MozOpacity = (op / 100); 
		subject.style.KhtmlOpacity = (op / 100); 
		subject.style.filter = "alpha(opacity=" + op + ")"; 
	}
	
	function showGrey(){
		if (window.ActiveXObject){
			//IE
			window.scrollTo(0, 0);
			var htmlHeight = document.body.parentNode.clientHeight;
        	var htmlWidth = document.body.parentNode.clientWidth;
			document.getElementById('greyBox').style.width = htmlWidth;
			document.getElementById('greyBox').style.height = htmlHeight;
			bod = document.getElementsByTagName('body')[0];
			bod.style.height = "100%";
			bod.style.overflow = "hidden";
	  
			htm = document.getElementsByTagName('html')[0];
			htm.style.height = "100%";
			htm.style.overflow = "hidden"; 
		}
		else{
			window.scrollTo(0, 0);
			document.getElementsByTagName("body")[0].style.overflow = "hidden";
		}
		document.getElementById('greyBox').style.display = 'block'; 
		document.getElementById('greyBox').style.zIndex = 110;
		fade('greyBox','in',1000,80);
	}
	
	function hideGrey(){
		opacity('greyBox',0);
		document.getElementById('greyBox').style.display = 'none'; 
		document.getElementById('greyBox').style.zIndex = 90;
		if (window.ActiveXObject){
			bod = document.getElementsByTagName('body')[0];
			bod.style.height = "auto";
			bod.style.overflow = "auto";
	  
			htm = document.getElementsByTagName('html')[0];
			htm.style.height = "auto";
			htm.style.overflow = "auto"; 
		}
		else{
			document.getElementsByTagName("body")[0].style.overflow = "scroll";
		}
	}
	
	
	function showRatingBox(placeID){
		//check if user is logged in
		//check for the value of $_SESSION['ocwUser']
		checkForUserRating(placeID);
	}
	
	function showLogin(title){
		document.getElementById("ratingBox").style.height = "120px";
		document.getElementById("ratingBox").style.width = "300px";
		document.getElementById("ratingBox").style.marginLeft = "-150px";
		document.getElementById("ratingBox").style.marginTop = "-60px";
		var inHTML = "";
		inHTML += "<div id='loginToRate' style='position: absolute; top: 7px; left:7px; font-size: 9px; color: #FF0000;'>" + title + "</div>";
		inHTML += "<div id='userPass' style='position: absolute; top: 25px; left: 20px;'><table><tr><td>username</td><td><input style='border: 1px solid grey;' id='user' type='text' onchange='document.getElementById(&quot;user&quot;).style.backgroundColor = &quot;#FFFFFF&quot;'/></td></tr><tr><td>password</td><td><input style='border: 1px solid grey;' id='pass' type='password' onchange='document.getElementById(&quot;pass&quot;).style.backgroundColor = &quot;#FFFFFF&quot;'/></td></tr></table></div>";
		inHTML += "<div style='position: absolute; bottom: 10px; right: 10px;'><span id='btnOK' style='cursor: pointer;' onclick='okLoginBox();'>OK</span>&nbsp;&nbsp;&nbsp;&nbsp;";
		inHTML += "<span id='btnCancel' style='cursor: pointer;' onclick='closeRatingBox();'>CANCEL</span></div>";
		inHTML += "<div id='createAcc' style='cursor: pointer; position: absolute; top: 85px; left: 15px; font-size: 9px; color: #ce7e05;'>Don't have an account? Get one here!</div>";
		inHTML += "<div id='forgotPass' style='cursor: pointer; position: absolute; top: 98px; left: 15px; font-size: 9px; color: #ce7e05;'>Forgot your Password?</div>";
		document.getElementById("ratingBox").innerHTML = inHTML;
	}
	
	function okLoginBox(){
		if ((document.getElementById('user').value == '') || (document.getElementById('pass').value == '')){
			//user or pass is blank
			//show error
			if (document.getElementById('user').value == ''){
				document.getElementById('user').style.backgroundColor = "#F0A4A4";
			}
			if (document.getElementById('pass').value == ''){
				document.getElementById('pass').style.backgroundColor = "#F0A4A4";
			}
		}
		else{
			//check for user
			getSeed();
			
		}

	}
	
	function okRatingBox(placeID){
		if (currRating > 0){
			savePlaceRating(placeID);
			closeRatingBox();
		}
		else{
			alert('Rollover To rate!');
		}
	}
	
	function closeRatingBox(){
		opacity('ratingBox',0);
		document.getElementById('ratingBox').style.display = 'none';
		hideGrey();
		document.getElementById("ratingBox").innerHTML = "";
		document.getElementById("ratingBox").style.height = "70px";
		document.getElementById("ratingBox").style.width = "250px";
		document.getElementById("ratingBox").style.marginLeft = "-125px";
		yourRatingOpen = false;
	}
	
	function starOver(starNo){
		for (var i=0; i<starNo+1; i++){
			document.getElementById("rateStar" + i).src = "../images/rating/rateStar.png";
		}
	}
	
	function starOut(starNo){
		if (starClicked == 0){
			for (var i=0; i<starNo+1; i++){
				document.getElementById("rateStar" + i).src = "../images/rating/" + stars[i] + "Star.png";
			}
		}
		else{
			for (var i=starClicked; i<5; i++){
				document.getElementById("rateStar" + i).src = "../images/rating/" + stars[i] + "Star.png";
			}
		}
	}
	
	function starClick(starNo){
		currRating = starNo + 1;
		starClicked = currRating;
		for (var i=0; i<starNo+1; i++){
			document.getElementById("rateStar" + i).src = "../images/rating/rateStar.png";
		}
		for (var i=starNo+1; i<5; i++){
			document.getElementById("rateStar" + i).src = "../images/rating/" + stars[i] + "Star.png";
		}
		
		if (document.getElementById('ratingBox').style.height == "70px"){
			//alert(document.getElementById('ratingBox').style.height);
			grow('ratingBox','out','height',70,130,1000);
			setTimeout("document.getElementById('commentDiv').style.display = 'block'",800);
			document.getElementById('rollover').innerHTML = "Comment if you would like.";
			//setTimeout("document.getElementById('rollover').innerHTML = 'Comment if you would like.'",1000);
		}
	}
	
	
	
	function showPlaceRating(){
		var inHTML = "<div id='ratingStarsDiv' style='position: absolute; top: 10px; left: 10px;'>";
		stars = [];
		//alert(arrPlaceRating);
		if (arrPlaceRating[0][3] != ""){
			//the user has rated the item before
			currRating = arrPlaceRating[0][3];
			for (var j=0; j<arrPlaceRating[0][3]; j++){
				inHTML += "<img onmouseover='starOver(" + j + ");' onmouseout='starOut(" + j + ");' onclick='starClick(" + j + ");' id='rateStar" + j + "' src='../images/rating/filledStar.png' width='15' height='15' border='0'/>";
				stars.push("filled");
			}
			for (var j=0; j<(5-arrPlaceRating[0][3]); j++){
				inHTML += "<img onmouseover='starOver(" + (j*1 + arrPlaceRating[0][3]*1) + ");' onmouseout='starOut(" + (j*1 + arrPlaceRating[0][3]*1) + ");' onclick='starClick(" + (j*1 + arrPlaceRating[0][3]*1) + ");' id='rateStar" + (j*1 + arrPlaceRating[0][3]*1) + "' src='../images/rating/blankStar.png' width='15' height='15' border='0'/>";
				stars.push("blank");
			}
			inHTML += "<br/><span style='font-size: 11px;'>Your Rating: <b>" + arrPlaceRating[0][3] + "/5</b></span>"
		}
		else{
			//the user has not rated the item before (show default 3 rating)
			for (var j=0; j<3; j++){
				inHTML += "<img onmouseover='starOver(" + j + ");' onmouseout='starOut(" + j + ");' onclick='starClick(" + j + ");' id='rateStar" + j + "' src='../images/rating/filledStar.png' width='15' height='15' border='0'/>";
				stars.push("filled");
			}
			for (var j=0; j<(5-3); j++){
				inHTML += "<img onmouseover='starOver(" + (j*1 + 3*1) + ");' onmouseout='starOut(" + (j*1 + 3*1) + ");' onclick='starClick(" + (j*1 + 3*1) + ");' id='rateStar" + (j*1 + 3*1) + "' src='../images/rating/blankStar.png' width='15' height='15' border='0'/>";
				stars.push("blank");
			}
		}
		inHTML += "</div>";
		inHTML += "<div id='rollover' style='position: absolute; top: 10px; left: 100px; font-size: 11px;'>Rollover stars to rate!</div>";
		inHTML += "<div id='commentDiv' style='position: absolute; top: 50px; left: 20px; display: none; font-size: 8px;'><textarea id='txtComment' style='width: 205px; height: 40px;' font-size: 10px;></textarea></div>";
		inHTML += "<div style='position: absolute; bottom: 10px; right: 10px;'><span id='btnOK' style='cursor: pointer;' onclick='okRatingBox(&quot;" + arrPlaceRating[0][0] + "&quot;);'>OK</span>&nbsp;&nbsp;&nbsp;&nbsp;";
		inHTML += "<span id='btnCancel' style='cursor: pointer;' onclick='closeRatingBox();'>CANCEL</span></div>";
		document.getElementById("ratingBox").innerHTML = inHTML;
		
		
		if ((arrPlaceRating[0][4] != "") && (arrPlaceRating[0][4] != null)) {
			//the user has commented the current obj, show the text in the commentDiv
			document.getElementById('txtComment').value = arrPlaceRating[0][4].replace(/<br \/>/g,"\n");
			grow('ratingBox','out','height',70,130,1000);
			setTimeout("document.getElementById('commentDiv').style.display = 'block'",800);
			document.getElementById('rollover').innerHTML = "Comment if you would like.";
		}
		else{
			if (arrPlaceRating[0][3] != ""){
				//the user has not commented the current item before, but has rated it
				grow('ratingBox','out','height',70,130,1000);
				setTimeout("document.getElementById('commentDiv').style.display = 'block'",800);
				document.getElementById('rollover').innerHTML = "Comment if you would like.";
			}
		}
	}
	
	
	yourRatingOpen = false;
	function showYourRating(){		
		//the user has rated this item before
		//show his rating, and comment if has any.
		if (yourRatingOpen == false){
			yourRatingOpen = true;
			document.getElementById('watchYourRating').innerHTML = "&lt;";
			grow('ratingBox','out','width',250,350,800);
			setTimeout("document.getElementById('persRating').style.display = 'block'",800);
		}
		else{
			yourRatingOpen = false;
			grow('ratingBox','in','width',250,350,800);
			document.getElementById('persRating').style.display = 'none';
			document.getElementById('watchYourRating').innerHTML = "&gt;";
		}
	}
	
	function grow(id,irany,mit,kezd,hossz,time)
	{
		var subject = document.getElementById(id);
		var timer = 0;
		var speed = Math.round(time / 100);
		if (irany == "out") 
		{
			for (i = kezd; i <= hossz; i++)
			{
				size = i;
				setTimeout("resize('"+ id + "'," + size + ",'" + mit + "')",(timer * speed));
				timer++;
			}
		}
		if (irany == "in") 
		{
			for (i = hossz; i >= kezd; i--)
			{
				size = i;
				setTimeout("resize('"+ id + "'," + size + ",'" + mit + "')",(timer * speed));
				timer++;
			}
		}
	}
			
	function resize(id,size,mit)
	{
		if (mit == "height"){
			document.getElementById(id).style.height = size + "px";
		}
		else if (mit == "width"){
			document.getElementById(id).style.width = size + "px";
		}
	}
