var searchInitVal = "Search flaggr...";

/* These must be set in view before script is called or they will be empty */
var basePath = "";

function setBasePath(bp) {
   basePath = bp;
}

function clickSearch(s) {
   if (s.value == searchInitVal) {
      s.value = "";
   }
}

function blurSearch(s) {
   if (s.value == "") {
      s.value=searchInitVal;
   }
}

function DisplayToolTip(o) {
    if (o.className=='ToolTipOn') {
        o.className='ToolTipOff';
    }
    else {
        o.className='ToolTipOn';
    }
}

function show(divid) { document.getElementById(divid).style.display = "block"; }


function hide(divid) { document.getElementById(divid).style.display = "none"; }

function toggle(div) {
    if (div.style.display=="none") {
       div.style.display = "block";
    }
    else {
       div.style.display = "none";
    }
}

function rate(linkid, plusorminus) {

	if (document.getElementById("ratingForm")) {
		document.getElementById("ratingForm").rating.value=plusorminus;
		document.getElementById("ratingForm").link_id.value=linkid;
		ajaxRate();
	}

}

var whoDisplayed = false;
function who() {
   if (!whoDisplayed) {
      ajaxWho();
      whoDisplayed = true;
   }
   else {
      var container_div = $('who_wrapper');
      toggle(container_div);
   }
}

/* Used for "Who else rated this?" section of link.php */
function createHTMLList(jsonArray) {

   if (jsonArray.length) {
	   var html = '<div class="activity"><ul id="who-rated">';
	   for (var ii = 0; ii < jsonArray.length; ii++) {
	      html +='<li class="' + jsonArray[ii].action + '"><a href="' + basePath + 'profile/' + jsonArray[ii].username + '">'+jsonArray[ii].username+'</a></li>';
	   }
	   html += "</ul>";
	   html += '</div><div style="clear:left;"></div>';
   	   return html;
   }
   else {
   	   return '<div class="activity"><ul><li style="padding-left: 0px;">This link has not been rated yet.</li></ul><div>';
   }
}

function deleteLink(linkid) {
	if (document.getElementById("deleteForm")) {
		if (confirm("This link will be permanently deleted!  Are you sure you want to continue?")) {
			document.getElementById("deleteForm").link_id.value=linkid;
			ajaxDelete(linkid);
		}
	}
}

function ajaxDelete(linkid) {
    // This is the url we submit to - change it as needed
    var url = basePath + 'ajax/delete';
    
    // Grab all the info in the form
    var form_data = $('deleteForm').serialize();    
    
    // Here we make the request
    new Ajax.Request(url, {
        method: 'post',
        parameters: form_data,
        onSuccess: function(transport) {
        	var rt = transport.responseText;

        	if (rt == "success") {
        		linkDiv = document.getElementById("link"+linkid);
        		linkDiv.style.opacity = ".5";
        		linkDiv.style.filter = 'alpha(opacity=50)';
        		linkDiv.innerHTML = '<div style="position: absolute; font-size: 300%; top: 15px; left: 60px; font-weight:bold;">This link has been deleted</div>' 
        					+ linkDiv.innerHTML;

        		//alert("Link deleted successfully!");
        	}
        	else if (rt == "error1"){
        		alert("Link can only be deleted by user that created it.");
        	}
        	else {
        		alert("Link could not be deleted: invalid/non-existent link.");
        	}
        }
    });    
}


// This 'observes' our form submit - sort of like onsubmit
function ajaxRate() {

    // This is the url we submit to - change it as needed
    var url = basePath + 'ajax/rate';

    // Grab all the info in the form
    var form_data = $('ratingForm').serialize();

    // Here we make the request
    new Ajax.Request(url, {
        method: 'post',
        parameters: form_data,
        onSuccess: function(transport) {
        	var rt = transport.responseText;
        	var json = eval("("+rt+")");

        	if (json.error) {
        		alert("You can only rate a link once!");
        	}
        	else if (json.timeout) { 
        		// Not logged in
        		document.location.href=basePath+"signin";
        	}
        	else if (json.rating=="positive") {
				var score = json.score;
				if (score>0) score="+"+score;
        		        $('link_rating'+json.id).update(score);

				ratinghtmlup = '<a href="javascript:rate('+json.id+", '+'"+');"><img src="' + basePath + 'images/thumbsupgreen.gif" style="border:0;" /></a>';
				ratinghtmldown = '<a href="javascript:rate('+json.id+", '-'"+');"><img src="' + basePath + 'images/thumbsdown.gif" style="border:0; opacity: .5;" /></a>';

				$('thumbs_up'+json.id).update(ratinghtmlup);
				$('thumbs_down'+json.id).update(ratinghtmldown);
				$('note').update('<div class="success">Note: Your rating has been saved.  Thanks for rating!</div><br />');

        	}
        	else if (json.rating=="negative") {
				var score = json.score;
				if (score>0) score="+"+score;
        		        $('link_rating'+json.id).update(score);

				ratinghtmlup = '<a href="javascript:rate('+json.id+", '+'"+');"><img src="' + basePath + 'images/thumbsup.gif" style="border:0; opacity: .5;" /></a>';
				ratinghtmldown = '<a href="javascript:rate('+json.id+", '-'"+');"><img src="' + basePath + 'images/thumbsdowngreen.gif" style="border:0; " /></a>';

				$('thumbs_up'+json.id).update(ratinghtmlup);
				$('thumbs_down'+json.id).update(ratinghtmldown);
				$('note').update('<div class="success">Note: Your rating has been saved.  Thanks for rating!</div><br />');
        	}
        	else if (json.rating=="none") {
				
				var score = json.score;
				if (score>0) score="+"+score;
        		        $('link_rating'+json.id).update(score);
                        
				ratinghtmlup = '<a href="javascript:rate('+json.id+", '+'"+');"><img src="' + basePath + 'images/thumbsup.gif" style="border:0; opacity: .5;" /></a>';
				ratinghtmldown = '<a href="javascript:rate('+json.id+", '-'"+');"><img src="' + basePath + 'images/thumbsdown.gif" style="border:0; opacity: .5;" /></a>';

				$('thumbs_up'+json.id).update(ratinghtmlup);
				$('thumbs_down'+json.id).update(ratinghtmldown);
				$('note').update('<div class="success">Note: Your rating has been removed.</div><br />');
        	}

        }
    });
}

// This 'observes' our form submit - sort of like onsubmit
function ajaxWho() {

    // This is the url we submit to - change it as needed
    var url = basePath + 'ajax/who';

    //This will be the div we update, in our case it's form_wrapper
    var container_div = $('who_wrapper');

    // Grab all the info in the form
    var form_data = $('whoForm').serialize();

    // Here we make the request
    new Ajax.Request(url, {
        method: 'post',
        parameters: form_data,
        onCreate: function() {
            container_div.update('Loading....');
        },
        onSuccess: function(transport) {
        	var rt = transport.responseText;
        	var json = eval("("+rt+")");
        	if (json.timeout) { 
        		// Not logged in
        		document.location.href=basePath+"signin";
        	}
        	else {
			    container_div.update(createHTMLList(json));
        	}

        }
    });
}

/* */
function commentVote(commentid, plusorminus) {

    // This is the url we submit to - change it as needed
    var url = basePath + 'ajax/comment_vote';

    // Grab all the info in the form
    //var form_data = $('ratingForm').serialize();

    // Here we make the request
    new Ajax.Request(url, {
        method: 'post',
        parameters: 'comment_id='+commentid+'&rating='+encodeURIComponent(plusorminus),
        onSuccess: function(transport) {
        	var rt = transport.responseText;
        	var json = eval("("+rt+")");

        	if (json.error) {
        		alert("You can only rate a link once!");
        	}
        	else if (json.timeout) { 
        		// Not logged in
        		document.location.href=basePath+"signin";
        	}
        	else if (json.rating=="positive") {

				var score = json.score;
				if (score==1) score = score + " point";
				else score = score + " points";
        		$('c_pts'+json.id).update(score);

				ratinghtmlup = '<a href="javascript:void(0);" onclick="commentVote('+json.id+", '+'"+');"><img src="' + basePath + 'images/vote_up.png" /></a>';
				ratinghtmldown = '<a href="javascript:void(0);" onclick="commentVote('+json.id+", '-'"+');"><img src="' + basePath + 'images/vote_down_off.png" /></a>';

				$('c_upvote'+json.id).update(ratinghtmlup);
				$('c_downvote'+json.id).update(ratinghtmldown);
				//$('note').update('<div class="success">Note: Your rating has been saved.  Thanks for rating!</div><br />');

        	}
        	else if (json.rating=="negative") {

				var score = json.score;
				if (score==1) score = score + " point";
				else score = score + " points";				
        		$('c_pts'+json.id).update(score);

				ratinghtmlup = '<a href="javascript:void(0);" onclick="commentVote('+json.id+", '+'"+');"><img src="' + basePath + 'images/vote_up_off.png" /></a>';
				ratinghtmldown = '<a href="javascript:void(0);" onclick="commentVote('+json.id+", '-'"+');"><img src="' + basePath + 'images/vote_down.png" /></a>';

				$('c_upvote'+json.id).update(ratinghtmlup);
				$('c_downvote'+json.id).update(ratinghtmldown);
				//$('note').update('<div class="success">Note: Your rating has been saved.  Thanks for rating!</div><br />');
        	}
        	else if (json.rating=="none") {

				var score = json.score;
				if (score==1) score = score + " point";
				else score = score + " points";
        		$('c_pts'+json.id).update(score);
                        
				ratinghtmlup = '<a href="javascript:void(0);" onclick="commentVote('+json.id+", '+'"+');"><img src="' + basePath + 'images/vote_up_off.png" /></a>';
				ratinghtmldown = '<a href="javascript:void(0);" onclick="commentVote('+json.id+", '-'"+');"><img src="' + basePath + 'images/vote_down_off.png" /></a>';

				$('c_upvote'+json.id).update(ratinghtmlup);
				$('c_downvote'+json.id).update(ratinghtmldown);
				//$('note').update('<div class="success">Note: Your rating has been removed.</div><br />');
        	}

        }
    });
}



/** Used in home.php view **/
function minimizeSection(sectionId) {
	document.getElementById(sectionId).style.display = "none";
}

function maximizeSection(sectionId) {
	document.getElementById(sectionId).style.display = "block";
}

// This 'observes' our form submit - sort of like onsubmit
function ajaxGetMetaData() {

    // This is the url we submit to - change it as needed
    var url = basePath + 'ajax/metadata';

    //This will be the div we update, in our case it's form_wrapper
    var container_div = $('form_wrapper');
    var loading_div = $('loading_wrapper');

    // Grab all the info in the form
    var form_data = $('addForm').serialize();

	loading_div.style.display = "block";
	container_div.style.display = "none";

    // Here we make the request
    new Ajax.Request(url, {
        method: 'post',
        parameters: form_data,
        onSuccess: function(transport) {



        	var rt = transport.responseText;
        	var json = eval("("+rt+")");
        	if (json.timeout) { 
        		// Not logged in
        		document.location.href=basePath+"signin";
        	}
        	else {
	        	loading_div.style.display = "none";
	        	container_div.style.display = "block";        	
				document.getElementById("addForm").title.value=json.title;
				document.getElementById("addForm").description.value=json.description;
				document.getElementById("addForm").tags.value=json.tags;
        	}
        }
    });
}

