/**********************************************************************************
 * following code tested (and approved) on  ie6, ie7, opera 9.24, firefox2 and firefox3
 ***********************************************************************************/
$(document).ready(function(){
	$('.starline').click( function(event) {  
		if($(event.target).is('a.rater'))
		{
		
			var obj=$(event.target);
			var url=$(obj).attr('href');
			var ref_id = $(obj).parents("div[@id^='unit_long']").attr('id');
			
			init_infowindow();
			/* fenetre d'attente */
			var infoWindow = $('#dialog');
			var waitingMessage = $(infoWindow).find('.message_info').html();
			positioneAreaInMiddle( infoWindow, false);
			$(infoWindow).jqm({overlay:0
			
				,
			  /*  onShow: function(h) { 
					//h.o.fadeIn(888); // show overlay       si overlay:0 => ne pas mettre de fonction qui essaie de toucher un overlay, sinon erreur
					h.w.fadeIn(500); // show window
				},	*/
				onHide : //fade the window out, kill overlay after fade.
					function(hash) { 
						hash.w.fadeOut(1,
										function(){
											//hash.o.remove();
											$(infoWindow).find('.message_info').html(waitingMessage);
											}
									); 
								}				
				
			
			}).jqmShow().jqDrag('.jqDrag');
			$(infoWindow).find('.message_info').html(waitingMessage);
		
			$.ajax({
				type: "POST",
				url: url,
				data: "access=ajax",
				error: function( msg2 ){
							userNotification('error');
						},
				success: function(msg){
										
										var res = msg.split(":");
										var resType= res[0];
										var objectId = res[1]
										
										userNotification(resType);
										updateAverageVoteBar(objectId);
										updateUserVoteBar(objectId);										
									}
			 });
				return false;
			}else{
				return false;
			}
		});
});

function updateAverageVoteBar(obj_id){
	$('#averagevotebar').addClass('loading').children().animate({ opacity: 'toggle'}, "slow"); 
	var urlShow = "/laborange/objectrating/show/"+obj_id;
	$.get(urlShow+'/true/small/false', function(msg){ 
														$('#averagevotebar').html(msg);
													}
		);
	$('#averagevotebar').removeClass('loading');
}
			
function updateUserVoteBar(obj_id){
	$('#uservotebar').addClass('loading').children().animate({ opacity: 'toggle'}, "slow"); 
	var urlShow = "/laborange/objectrating/show/"+obj_id;
	$.get(urlShow+'/false/big/true',  function(msg){ 
														$('#uservotebar').css({visibility:"hidden"}).html(msg).css({visibility:"visible"});
												   }
		);
	$('#uservotebar').removeClass('loading');
}			


function userNotification( resCode ){
	var message = '';
	var alt= '';
	switch(resCode)
	{
		case "insert": message = "Votre vote a été pris en compte.";
			break;
			
		case "update": message = "Votre vote a été modifié.";
			break;
	
		case "error": message = "Une erreur est survenue.<br/>Veuillez contacter le <a href='/laborange/basdepage/contactez_nous'>webmaster</a>.";
			break;
		
		default:  message = "Une erreur est survenue.<br/>Veuillez contacter le <a href='/laborange/basdepage/contactez_nous'>webmaster</a>.";
	}
	var infoWindow = $('#dialog');
	var waitingMessage = $(infoWindow).find('.message_info').html();
	$(infoWindow).find('.message_info').html('<img src="/extension/ezobjectrating/design/standard/images/infowindow/orange_info.gif" width="35" height="45" alt="'+alt+'" />'+message);
	
}

/* détermine l'état des barres de scroll de la page */
this.getScrollPosition = function getScrollPosition(){
	var topPos, leftPos;
	try{
		topPos = document.documentElement.scrollTop;
		leftPos = document.documentElement.scrollLeft;
	}catch(e){
		topPos = document.body.scrollTop;
		leftPos = document.body.scrollLeft;
	}
	return {topPos:topPos,leftPos:leftPos};
 }; 

 /* positione un élément au centre de la partie visible de la   page */
 this.positioneAreaInMiddle = function positioneAreaInMiddle(areaObj, showItNow){
	 if($(areaObj))
	 {
		 var pos = this.getScrollPosition();
		 
		 if(!showItNow)
		 {
			$(areaObj).hide();
		 }
		 
		 var w_left = document.body.clientWidth/2 - $(areaObj).width()/2 + pos.leftPos;
		
		 w_top = window.innerHeight;
		
		 if(!isset(w_top))
		 {
			w_top = document.documentElement.clientHeight;
		 }
		 w_top = w_top/2 - $(areaObj).height()/2 + pos.topPos;
		 $(areaObj).css("top",w_top-40).css("left",w_left);
	 }
 }; 
 
 // famous isset function
function isset(str){
	 // general test
	 if (typeof str == "undefined" || str == null)
	 {
		return false;
	 }

	 // specific string test
	 try{
		 if(str == '' || str == false)
		 {
			return false
		 }
	 }catch(ex){}

	 return true;
}

function init_infowindow(){
	// Close Button Highlighting. IE doesn't support :hover. Surprise?
  $('input.jqmdX')
  .hover(
    function(){ $(this).addClass('jqmdXFocus'); }, 
    function(){ $(this).removeClass('jqmdXFocus'); })
  .focus( 
    function(){ this.hideFocus=true; $(this).addClass('jqmdXFocus'); })
  .blur( 
    function(){ $(this).removeClass('jqmdXFocus'); });
}