
/*
 * jFastMenu
 * see http://code.google.com/p/jfastmenu/
 * */
jQuery.jFastMenu = function(id){

	$(id + ' ul li').mouseover(function(){
		$(this).find('ul:first').css('display', 'block');
	}).mouseout(function(){
		$(this).find('ul:first').css('display', 'none');
	});
};
/* jfastmenu end */



/* minitabs extended
 * see http://code.google.com/p/minitabs
 * param hide: hides the tabs when clicked again on the active tab
 */
jQuery.fn.minitabs = function(hide,speed,effect) {
  id = "#" + this.attr('id');
  $(id + ">DIV:gt(0)").hide();
  $(id + ">DIV:first").show();
  $(id + ">UL>LI>A:first").addClass("current");
  $(id + ">UL>LI>A").click(
    function(){
    	if ( $(this).hasClass("current") && hide) {
    		$(id + ">UL>LI>A").removeClass("current");
    		$(id + ">DIV").hide();
    		return false;
    	}
      $(id + ">UL>LI>A").removeClass("current");
      $(this).addClass("current");
      $(this).blur();
      var re = /([_\-\w]+$)/i;
      var target = $('#' + re.exec(this.href)[1]);
      var old = $(id + ">DIV");
      switch (effect) {
        case 'fade':
          old.fadeOut(speed).fadeOut(speed);
          target.fadeIn(speed);
          break;
        case 'slide':
          old.slideUp(speed);  
          target.fadeOut(speed).fadeIn(speed);
          break;
        case 'none': 
        	old.hide(); 
        	target.show(); 
        	break; 
        default : 
          old.hide(speed);
          target.show(speed);
      }
      return false;
    }
 );
};
/* minitabs end */



/**
 * this function registeres a one click event at element
 * a click at the element, loads the url and applies it to the element linked via href
 * 
 * if the optional loadingPicture is given, this image will be shown while loading
 */
function loadOnRequest( element, url, loadingPicture, target) {
	$(element).click(function(){return false;});
	$(element).one("click", function(){
		if (!target) {
			target = $(element).attr('href');
		}
		if (loadingPicture) {
			$(target).html( '<img src="'+loadingPicture+'" /> please wait ...');
		}
		else {
			$(target).html('<img src="/pic/loading.gif" /> please wait ...');
		}
		
	   	// call url and apply returned data
		$.get( url, 
	  	    function(data){
	  	      $(target).html(data);
	  	    }
	  	);
    });
}


/**
 * sends data of a form to an url
 * @param formName: name of the form to send
 * @param url: url to send data to
 * @param target: element id to place the return 
 * @return
 */
function cnSendForm( formName, url, target) {
	// post form data
	$.post( url,
			$( formName).serialize(), 
			    function(data){
			      $(target).html(data);
			    }
			);
	// show loading picture
	$( target).html('<img src=\'/pic/loading.gif\' /> please wait ...');
}


/* CNRating */

/**
* creates the rating object
*/
function CNRating(
	ratingElementId,
	maxStars,
	objectName,
	formName,
	ratingMessageId,
	componentSuffix,
	messages,
	starCount,
	callback)
{
	this.ratingElementId=ratingElementId;
	this.maxStars=maxStars;
	this.objectName=objectName;
	this.formName=formName;
	this.ratingMessageId=ratingMessageId
	this.componentSuffix=componentSuffix
	this.messages=messages;
	this.callback=callback;
	this.starTimer=null;
	this.starCount=0;
	this.enabled=true;
	if(starCount){
		this.starCount=starCount;
		var that=this;
		$(document).ready(function() {
			that.drawStars(that.starCount,true);
		});
	}
}

/**
* initialize object properties
*/
CNRating.prototype.ratingElementId=null;
CNRating.prototype.maxStars=null;
CNRating.prototype.objectName=null;
CNRating.prototype.formName=null;
CNRating.prototype.ratingMessageId=null;
CNRating.prototype.componentSuffix=null;
CNRating.prototype.messages=null;
CNRating.prototype.callback=null;
CNRating.prototype.starTimer=null;
CNRating.prototype.starCount=null;
CNRating.prototype.savedMessage=null;

/**
* shows number of stars 
*/
CNRating.prototype.showStars=function(starNum,skipMessageUpdate){
	if ( this.enabled==false) {
		return;
	}
	this.clearStarTimer();
	this.greyStars();
	this.colorStars(starNum);
	if(!skipMessageUpdate)
		this.setMessage(starNum,this.messages);
};

/**
* set message according to star
*/
CNRating.prototype.setMessage=function(starNum){

	if(starNum>0){
		if(!this.savedMessage){
			this.savedMessage=$('#'+this.ratingMessageId).html();
		}
		$('#'+this.ratingMessageId).html(this.messages[starNum-1]);
	}
  else if(this.savedMessage){
			$('#'+this.ratingMessageId).html(this.savedMessage);
	}
};

CNRating.prototype.colorStars=function(starNum){
	var fullStars=Math.floor(starNum+0.25);

	var halfStar=(starNum-fullStars>0.25);
	for(var i=0;i<fullStars;i++){
	   $('#'+'star_'+this.componentSuffix+"_"+(i+1)).removeClass(CNRating.ut_rating_img_half);
	   $('#'+'star_'+this.componentSuffix+"_"+(i+1)).removeClass(CNRating.ut_rating_img_bg);
	   $('#'+'star_'+this.componentSuffix+"_"+(i+1)).addClass(CNRating.ut_rating_img);
	}
	if(halfStar){
	   $('#'+'star_'+this.componentSuffix+"_"+(i+1)).removeClass(CNRating.ut_rating_img);
	   $('#'+'star_'+this.componentSuffix+"_"+(i+1)).removeClass(CNRating.ut_rating_img_bg);
	   $('#'+'star_'+this.componentSuffix+"_"+(i+1)).addClass(CNRating.ut_rating_img_half);
	}
};


CNRating.prototype.greyStars=function(){
	for(var i=0;i<this.maxStars;i++){
	   $('#'+'star_'+this.componentSuffix+"_"+(i+1)).removeClass(CNRating.ut_rating_img);
	   $('#'+'star_'+this.componentSuffix+"_"+(i+1)).removeClass(CNRating.ut_rating_img_half);
	   $('#'+'star_'+this.componentSuffix+"_"+(i+1)).addClass(CNRating.ut_rating_img_bg);
	}
};

/**
* sets the stars from the rating
*/
CNRating.prototype.setStars=function(starNum){
	if ( this.enabled==false) {
		return;
	}
	
	this.starCount=starNum;
	this.drawStars(starNum);
	
	var ratingMessageId=this.ratingMessageId;
	
	// add rating to the form
	document.forms[this.formName]['rating'].value=this.starCount;
	var form=document.forms[this.formName];
	
	// disable any further rating
	this.disable();

	// extract params
	var formData = $('#'+this.formName).serialize();
	
	// send form and install callback
	$.get( form.action+'?'+formData, 
	    function(data){
	      $('#'+ratingMessageId).html(data);
	    }
	);
};

CNRating.prototype.drawStars=function(starNum,skipMessageUpdate){
	this.starCount=starNum;
	this.showStars(starNum,skipMessageUpdate);
};

/**
* called from onmouseout
* clear the stars
*/
CNRating.prototype.clearStars=function(){
	if ( this.enabled==false) {
		return;
	}
	this.starTimer=window.setTimeout(this.objectName+".resetStars()",300);
};


CNRating.prototype.resetStars=function(){
	this.clearStarTimer();
	if(this.starCount)
		this.drawStars(this.starCount);
	else
		this.greyStars();
	this.setMessage(0);
};


CNRating.prototype.clearStarTimer=function(){
	if(this.starTimer){
		window.clearTimeout(this.starTimer);
		this.starTimer=null;
	}
};

/**
 * disables the rating component
 * called from within setStars 
 */
CNRating.prototype.disable=function(){
	this.enabled = false;

};

CNRating.prototype.enable=function(){
	this.enabled = true;
};

/**
 * class definitions
 */
CNRating.ut_rating_img='icn_star_full_large';
CNRating.ut_rating_img_half='icn_star_half_large';
CNRating.ut_rating_img_bg='icn_star_empty_large';

/* CNRating end */






