// JavaScript Document
// Author: WP, 
// Date: 20110804

    jQuery.fn.DisplaySubcategoriesOf = function(parentCategoryName) {

      var isInCategory = false;
      this.each(function() {

        if(this.text == parentCategoryName){isInCategory = true;}
		var isSubCategory = (this.text.substring(0,3) == '---'); //starts with '---'
        
		if(!isInCategory || !isSubCategory){
          $(this).detach(); //.hide() does not work for IE and Opera
        }
    	else {
          //remove leading '---'
          var text = this.text;
          text = text.replace('---', '');
          this.text = text;
          //check if this item is a last subcategory of parent category
		  var nextItem = $(this).next().text();
		  var isNewCategory = (nextItem.substring(0,3) != '---'); //if next item does not start with '---'
          if( isNewCategory ){isInCategory = false;}
		}
      });
	};
	
	//------------------------------------------------------------------------------------------//
	
	jQuery.fn.DisplayParentAndSubcategoriesOf = function(parentCategoryName) {

      var isInCategory = false;
      this.each(function() {
        
        if(this.text == parentCategoryName){isInCategory = true;}
		
        if(!isInCategory){
          $(this).detach(); //.hide() does not work for IE and Opera
        }
		else
		{
			//check if this item is a last subcategory of parent category
			var nextItem = $(this).next().text();
			var isNewCategory = (nextItem.substring(0,3) != '---'); //if next item does not start with '---'
			if( isNewCategory ){isInCategory = false;}
		}
      });
    };

	//-----------------------------------------------------------------------------------------//
	//source:
	//http://www.ajaxray.com/blog/2007/11/09/interactive-character-limit-for-textarea-using-jquery/
	
	 function limitChars(textid, limit, infodiv)
	 {
	   var text = $('#'+textid).val(); 
	   var textlength = text.length;
	   if(textlength > limit){
		$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	   }
	   else{
		$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
		return true;
	   }
	 }
	 
	 //----------------------------------------------------------------------------------//
	 //Auth: wp; date: 20110830
	 
	 jQuery.fn.addSortableDate = function(inputAttr, sortableAttrName) {
		 this.each(function(){
			var sDate = $(this).attr(inputAttr);
            $(this).attr(sortableAttrName, Date.parse(sDate).toString('yyyyMMdd'));
		})
    }

    //-----------------------------------------------------------------------------------//
    jQuery.fn.HideParentIfBroken = function () {
        this.each(function (index) {
            $(this).error(function () {
                $(this).parent().hide();
            });
            $(this).attr("src", $(this).attr("src"));
        });
    };

    jQuery.fn.HideParentParentIfBroken = function () {
        this.each(function (index) {
            $(this).error(function () {
                $(this).parent().parent().hide();
            });
            $(this).attr("src", $(this).attr("src"));
        });
    };
	 
