/*
* FUNCTION:		Resize()
* DESC:			resizes images being displayed if they are too wide
*/
function Resize(anImage){
  var newWidth = 450;

  var oldWidth = anImage.width;
  var oldHeight = anImage.height;
  var proportion = oldHeight / oldWidth;

  if(oldWidth < newWidth)
	return true;

  var newHeight = Math.round(newWidth * proportion);

  anImage.width = newWidth;
  anImage.height = newHeight;
}


/*
* FUNCTION:		sm_resultMsg(type [tag/sm], result [true/false])
* DESC:			displays appropiate div with status results
*/
function resultMsg(type, result) {
	if(type == 'tag') {
		// hide the processing text
		$('#tag_process').hide();
		
		// show success or failure
		if(result) {
			$('#tag_success').fadeIn("normal").css("color", "#43AF20");
			setTimeout("$('#tag_success').fadeOut('slow')",5000);
		} else {
			$('#tag_fail').fadeIn("slow").css("color", "#BF2317");
			setTimeout("$('#tag_fail').fadeOut('slow')",5000);
		}
	} else if (type == 'login') {
		$('#widget_login').fadeIn("normal").css("color", "#BF2317");
		setTimeout("$('#widget_login').fadeOut('slow')",5000);
	} else if (type == 'widget') {
		// hide the processing text
		$('#widget_process').hide();
		
		// show success or failure
		if(result) {
			$('#widget_success').fadeIn("normal").css("color", "#43AF20");
			setTimeout("$('#widget_success').fadeOut('slow')",5000);
		} else {
			$('#widget_fail').fadeIn("slow").css("color", "#BF2317");
			setTimeout("$('#widget_fail').fadeOut('slow')",5000);
		}
	}
}
/*
* FUNCTION:		setAuthors()
* DESC:			puts the authors on page/inserts/etc
*/
function setAuthors(li) {
	
	if (li.extra) {
		theid = li.extra;
		thename = li.selectValue;

		var extra = $("input#ExtraInfo").val();
		var path = "/includes/ajax.html";
	
		if(theid) {
			var whattosend = escape(thename);
		} else {
			var whattosend = theid;	
		}
		
		$.get(path, {author: whattosend, extra:extra}, function(xml) {
			var total = $("totalauthors", xml).text();										  
			if(total > 0) {
				var comma = "";
				var orig = $("#AuthorsList").text();
				if(orig == '') { var first=0; } else { var first=1; }
				if(first) {comma = ", "; } else { first=1;	}
				$("#AuthorsList").append(comma + thename);
				// clear text input
				$("input#AuthorNames").val("");
				$("input#AuthorNames").html("");
			}
		});
	}
}



/*
* FUNCTION:		fadeMsg()
* DESC:			fades green msg box
*/
jQuery.fn.fadeMsg = function() {
    return this.each(function() {
        var theobj = this;
        $(theobj).show();
        setTimeout(function() { $(theobj).fadeOut(); }, 15000);
    });
};

/*
* jQUERY PROCESSES - 
* They fire only when DOM is ready or page has finished loading
*/
$(document).ready(function()
{
	$('#msgbox').fadeMsg();
	$('#layerTags').hide();
	$('#layerAddAuthors').hide();
	$('#tag_fail').hide();
	$('#tag_success').hide();
	$('#tag_process').hide();
	$('#widget_fail').hide();
	$('#widget_success').hide();
	$('#widget_process').hide();
	$('#widget_login').hide();

	
	/********** Widget Setup ************/
	var extra = $("input#ExtraInfo").val();
	var path = "/includes/ajax.html";

	if(extra) {
		var eachitem = extra.split("|");
		if(eachitem[2] == '') {
			var needslogin=true;
		}
	}

	/********* Widget Default Values *************/
	if(extra && path) {
		$.get(path, {getinfo: 1, extra:extra}, function(xml) {
			// rating
			var rating = $("rating", xml).text();	
			$('#widget_rating').rating(path, {extra:extra, maxvalue:5, curvalue:rating});
			
			// fav
			var fav = $("fav", xml).text();	
			if(fav == 1) {
				$('#btn_widget_fav').toggleClass("fav_active");
			}
			
			// flag
			var flag = $("flag", xml).text();	
			if(flag == 1) {
				$('#btn_widget_flag').toggleClass("flag_active");
			}


		});
	}

	/********* Fav *************/
	$('#btn_widget_fav').click(function() {
		if(needslogin) { resultMsg("login", true); return false; }
		$('#widget_process').fadeIn("normal").css("color", "#999");
		$.get(path, {widget_fav: 1, extra:extra}, function(xml) {
			var results = $("results", xml).text();	
			if(results > 0) {
				$('#btn_widget_fav').toggleClass("fav_active");
				resultMsg("widget", true);
			} else {
				resultMsg("widget", false);
			}
			
		});
		return false;
	});
	
	/********* Flag *************/
	$('#btn_widget_flag').click(function() {
		if(needslogin) { resultMsg("login", true); return false; }
		$('#widget_process').fadeIn("normal").css("color", "#999");
		$.get(path, {widget_flag: 1, extra:extra}, function(xml) {
			var results = $("results", xml).text();	
			if(results > 0) {
				$('#btn_widget_flag').toggleClass("flag_active");
				resultMsg("widget", true);
			} else {
				resultMsg("widget", false);
			}
			
		});
		return false;
	});


	/******** Delete Tag Function ******/
	var myDeleteFunc = function() { 
		$('#tag_process').fadeIn("normal").css("color", "#999");   
		var tag = $(this).parent().find("a.tags").text();    
		$.get(path, {tagdel: tag, extra:extra}, function(xml) {   
			var results = $("results", xml).text();    
			// del tag    
			if(results > 0) {    
				$('#tag_'+tag).fadeOut(); 
				resultMsg("tag", true);  
			} else {  
				resultMsg("tag", false); 
			}
		}); 
		return false; 
	}
	$("#TagList a[name='deleteButton']").click(myDeleteFunc);
	
	/******** Delete Tag (PROFILE INTERESTED IN - ID=8) Function ******/
	var myDeleteFunc2 = function() { 
		$('#tag_process').fadeIn("normal").css("color", "#999");   
		var tag = $(this).parent().find("a.tags").text();    
		var extrastuff = "8|" + "0|" + extra;
		$.get(path, {tagdel: tag, extra:extrastuff}, function(xml) {   
			var results = $("results", xml).text();    
			// del tag    
			if(results > 0) {    
				$('div#TagList2 #tag_'+tag).fadeOut(); 
				resultMsg("tag", true);  
			} else {  
				resultMsg("tag", false); 
			}
		}); 
		return false; 
	}
	$("#TagList2 a[name='deleteButton']").click(myDeleteFunc2);
	
	/******** Delete Tag (HELP OTHERS WITH - ID=10) Function ******/
	var myDeleteFunc3 = function() { 
		$('#tag_process').fadeIn("normal").css("color", "#999");   
		var tag = $(this).parent().find("a.tags").text();
		var extrastuff = "10|" + "0|" + extra;
		$.get(path, {tagdel: tag, extra:extrastuff}, function(xml) {   
			var results = $("results", xml).text();    
			// del tag    
			if(results > 0) {    
				$('div#TagList3 #tag_'+tag).fadeOut(); 
				resultMsg("tag", true);  
			} else {  
				resultMsg("tag", false); 
			}
		}); 
		return false; 
	}
	$("#TagList3 a[name='deleteButton']").click(myDeleteFunc3);
	
	/********* Add Tag *************/ 
	$("#AddTag").click(function() {  
		// show process msg 
		$('#tag_process').fadeIn("normal").css("color", "#999");  
		var tag = $("input#Tag").val();  
		$.get(path, {tag: tag, extra:extra}, function(xml) {   
			var total = $("totaltags", xml).text();               
			// add tag 
			if(total > 0) {    
				// for each, add link    
				var comma = "";    
				var orig = $("#TagList").text();    
				if(orig == '') { var first=0; } else { var first=1; }   
				var eachtag = tag.split(",");    
				for(i=0;i < eachtag.length;i++) {     
					var thetag = eachtag[i];     
					var final = ""; 
					// trim it     
					thetag = thetag.replace(/^\s+|\s+$/g, ''); 
					if(first) {comma = ", "; } else { first=1; }   
					final = comma + "<div id=\"tag_"+ thetag + "\" class=\"inline\"><a href=\"http://ymex.org/tags/?tag=" + thetag + "\" class=\"tags\">" + thetag + "</a> [<a href=\"#\" name=\"deleteButton\" class=\"red\">x</a>]</div>";    
					$("#TagList").append(final); 
					$("#TagList a[name='deleteButton']:last").click(myDeleteFunc);    
				} 
				// clear text input 
				$("input#Tag").val("");   
				$("input#Tag").html("");    
				resultMsg("tag", true);   
			} else { 
				resultMsg("tag", false);  
			}  
		});  
		return false;
	});	

	/********* Add Collaborators *************/
	$("#AuthorNames").autocomplete("/includes/ajax_members.html", {minChars:3, onItemSelect: setAuthors});
	
	/******** Delete Collaborators Function ******/
	var myDelColabFunc = function() { 
		$('#colab_process').fadeIn("normal").css("color", "#999");   

		var colab = $(this).parent().attr("id");

		$.get(path, {colabdel: colab, extra:extra}, function(xml) {   
			var results = $("results", xml).text();    
			// del colab    
			if(results > 0) {    
				$('#'+colab).fadeOut(); 
				resultMsg("colab", true);  
			} else {  
				resultMsg("colab", false); 
			}
		}); 
		return false; 
	}
	$("#ColabList a[name='delColabButton']").click(myDelColabFunc);

	/********* Show/Hide *************/
    /* Comments */
	$('a#postComment').toggle(
      function () {
        $("div#layerPost").fadeIn();
      },
      function () {
        $("div#layerPost").fadeOut();
      }
    );
	
    /* Task Post */
	$('a#postTask').toggle(
      function () {
        $("div#layerPostTask").fadeIn();
      },
      function () {
        $("div#layerPostTask").fadeOut();
      }
    );

	/* Tags */
    $('a#btn_addtags').click(function () {
        $("div#layerTags").show();
        $("a#btn_addtags").hide();
		return false;
    });
	/* Authors */
    $('a#btn_addauthors').click(function () {
        $("div#layerAddAuthors").show();
        $("a#btn_addauthors").hide();
		return false;
    });
	/* Pending Members vs Member in Groups */
    $('a#btn_pending').click(function () {
        $("div#members").hide();
		$("div#pending").show();
		return false;
    });
	
    $('a#btn_members').click(function () {
        $("div#pending").hide();
		$("div#members").show();
		return false;
    });
	
    $('a#btn_showsearch').click(function () {
        $("span#searchlink").hide();
		$("span#searchbox").fadeIn();
		return false;
    });
	
	/* HEADER SUB-MENUS */
    $("#slide_community").click(function () {
      $("#community_submenu").slideToggle("fast");
    });
    $("#slide_issues").click(function () {
      $("#issues_submenu").slideToggle("fast");
    });
	
	/********* Tab'd Buttons for Media Index Pages *************/
    $('a#btn_recent').click(function () {
        $("#popular").hide();
		$("#issues").hide();
		$("#share").hide();
		$("#create").hide();
		$("#recent").show();
		$(this).parent().parent().parent().find("h2 span").text("Recent");
		$(this).addClass("active");
		$("#btn_popular").removeClass("active");
		$("#btn_issues").removeClass("active");
		$("#btn_share").removeClass("active");
		$("#btn_create").removeClass("active");
		return false;
    });
	
    $('a#btn_popular').click(function () {
        $("#recent").hide();
        $("#issues").hide();
        $("#share").hide();
        $("#create").hide();
		$("#popular").show();
		$(this).parent().parent().parent().find("h2 span").text("Popular");
		$(this).addClass("active");
		$("#btn_recent").removeClass("active");
		$("#btn_issues").removeClass("active");
		$("#btn_share").removeClass("active");
		$("#btn_create").removeClass("active");
		return false;
    });
	
    $('a#btn_issues').click(function () {
        $("#recent").hide();
        $("#share").hide();
        $("#create").hide();
		$("#popular").hide();
        $("#issues").show();
		$(this).parent().parent().parent().find("h2 span").text("Global Issue");
		$(this).addClass("active");
		$("#btn_recent").removeClass("active");
		$("#btn_popular").removeClass("active");
		$("#btn_share").removeClass("active");
		$("#btn_create").removeClass("active");
		return false;
    });
	
    $('a#btn_share').click(function () {
        $("#recent").hide();
        $("#create").hide();
		$("#popular").hide();
        $("#issues").hide();
        $("#share").show();
		$(this).parent().parent().parent().find("h2 span").text("Sharing");
		$(this).addClass("active");
		$("#btn_recent").removeClass("active");
		$("#btn_popular").removeClass("active");
		$("#btn_issues").removeClass("active");
		$("#btn_create").removeClass("active");
		return false;
    });
	
    $('a#btn_create').click(function () {
        $("#recent").hide();
		$("#popular").hide();
        $("#issues").hide();
        $("#share").hide();
        $("#create").show();
		$(this).parent().parent().parent().find("h2 span").text("Creating");
		$(this).addClass("active");
		$("#btn_recent").removeClass("active");
		$("#btn_popular").removeClass("active");
		$("#btn_issues").removeClass("active");
		$("#btn_share").removeClass("active");
		return false;
    });
	
	
	/********* Tab'd Buttons for Resource Index Pages *************/
    $('a#btn_latest').click(function () {
        $("div#create").hide();
        $("div#share").hide();
        $("div#global").hide();
        $("div#popular").hide();
		$("div#latest").show();
		return false;
    });

    $('a#btn_create').click(function () {
        $("div#share").hide();
        $("div#global").hide();
        $("div#popular").hide();
		$("div#latest").hide();
		$("div#create").show();
		return false;
    });
	
    $('a#btn_share').click(function () {
        $("div#global").hide();
        $("div#popular").hide();
		$("div#latest").hide();
		$("div#create").hide();
		$("div#share").show();
		return false;
    });
	
    $('a#btn_global').click(function () {
        $("div#popular").hide();
		$("div#latest").hide();
		$("div#create").hide();
		$("div#share").hide();
		$("div#global").show();
		return false;
    });
	
    $('a#btn_popular').click(function () {
		$("div#latest").hide();
		$("div#create").hide();
		$("div#share").hide();
		$("div#global").hide();
		$("div#popular").show();
		return false;
    });

});




