/**
 * nxsOxStars functions
 * @author Mike Bertram
 * @copyright NEXUS Netsoft GmbH
 * @version 0.1
 * 
 * Example for TPL:
 * <div class="nxsOxRatingStars" staron="[{$oViewConf->getImageUrl()}]star_on.jpg" staroff="[{$oViewConf->getImageUrl()}]star_off.jpg" ffield="nxsRating"></div>
 * 
 * class: musst be "nxsOxRatingStars" (more then one uses per page is possible)
 * staron: Image for active star
 * staroff: Image for inactive star
 * ffield: ID from form-field for rating
 */
$(document).ready(function(){	
	
	var $iCntStarBoxes = 0;
	
	window.deselectAllStars = function(boxId){		
		$sStarOn = $(".nxsstarboxroot_"+boxId).attr('staron');	
		$sStarOff = $(".nxsstarboxroot_"+boxId).attr('staroff');
		$sClickState = $(".nxsstarboxroot_"+boxId).attr('clickstate');
		$(".nxsstarboxroot_"+boxId).attr('actstate', $sClickState);
		
		for (var i = 0; i <= 5; i++) {
			$("#nxsstarimg_"+boxId+"_"+i).attr('src', $sStarOff);
		}
		for (var i = 1; i <= $sClickState; i++) {
			$("#nxsstarimg_"+boxId+"_"+i).attr('src', $sStarOn);
		}
		
	}
		
	window.nxsActiveStar = function(boxId, starNr){		
		$sStarOn = $(".nxsstarboxroot_"+boxId).attr('staron');		
		$sStarOff = $(".nxsstarboxroot_"+boxId).attr('staroff');
		for (var i = 0; i <= 5; i++) {
			$("#nxsstarimg_"+boxId+"_"+i).attr('src', $sStarOff);
		}		
		$(".nxsstarboxroot_"+boxId).attr('actstate', starNr);
		for (var i = 1; i <= starNr; i++)
		{
			$("#nxsstarimg_"+boxId+"_"+i).attr('src', $sStarOn);
		}
	}
	
	window.nxsClickStar = function(boxId, starNr){
		nxsActiveStar(boxId, starNr);
		$(".nxsstarboxroot_"+boxId).attr('clickstate', starNr);
		$sFieldName = $(".nxsstarboxroot_"+boxId).attr('ffield');
		$("#"+$sFieldName).val(starNr);
	}
	
	$(".nxsOxRatingStars").each(function (){		
		/**
		 * Für jedes Rating-Stars div das selbe
		 */
		$sStarOn = $(this).attr('staron');
		$sStarOff = $(this).attr('staroff');
		$(this).addClass("nxsstarboxroot_"+$iCntStarBoxes);
		$(this).attr('actstate', 0);
		$(this).attr('clickstate', 0);
		
		$(this).append('<div id="nxsstarbox_'+$iCntStarBoxes+'_1" style="float: left;" boxid="'+$iCntStarBoxes+'" starnr="1"><img src="'+ $sStarOff +'" id="nxsstarimg_'+$iCntStarBoxes+'_1" boxid="'+$iCntStarBoxes+'" starnr="1" /></div>');
		$(this).append('<div id="nxsstarbox_'+$iCntStarBoxes+'_2" style="float: left;" boxid="'+$iCntStarBoxes+'" starnr="2"><img src="'+ $sStarOff +'" id="nxsstarimg_'+$iCntStarBoxes+'_2" boxid="'+$iCntStarBoxes+'" starnr="2" /></div>');
		$(this).append('<div id="nxsstarbox_'+$iCntStarBoxes+'_3" style="float: left;" boxid="'+$iCntStarBoxes+'" starnr="3"><img src="'+ $sStarOff +'" id="nxsstarimg_'+$iCntStarBoxes+'_3" boxid="'+$iCntStarBoxes+'" starnr="3" /></div>');
		$(this).append('<div id="nxsstarbox_'+$iCntStarBoxes+'_4" style="float: left;" boxid="'+$iCntStarBoxes+'" starnr="4"><img src="'+ $sStarOff +'" id="nxsstarimg_'+$iCntStarBoxes+'_4" boxid="'+$iCntStarBoxes+'" starnr="4" /></div>');
		$(this).append('<div id="nxsstarbox_'+$iCntStarBoxes+'_5" style="float: left;" boxid="'+$iCntStarBoxes+'" starnr="5"><img src="'+ $sStarOff +'" id="nxsstarimg_'+$iCntStarBoxes+'_5" boxid="'+$iCntStarBoxes+'" starnr="5" /></div>');
		$(this).append('<div style="clear: both;"></div>');
		
		for (var i = 1; i <= 5; i++) {
			$("#nxsstarbox_" + $iCntStarBoxes + "_" + i).hover(function(){
				nxsActiveStar($(this).attr('boxid'), $(this).attr('starnr'));
			}, function(){
				deselectAllStars($(this).attr('boxid'));
			});
			
			$("#nxsstarbox_" + $iCntStarBoxes + "_" + i).click(function(){
				nxsClickStar($(this).attr('boxid'), $(this).attr('starnr'));
			});
		}
		
		$iCntStarBoxes++;
	});	
});
