prompts = new Array();

var currentPopup = "";

jQuery.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }
  return this.each(function () {
    // get jQuery version of 'this'
    var $input = jQuery(this),
    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = jQuery(this.form),
      $win = jQuery(window);
    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }
    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title

      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};


$(document).ready(function(){
	$(".al").html('Anode Et Cathode Design');
    configureMenu();
    configurePopups();
    configureInputs();
    configureBlocks();
    configureCityMenu();
    configureVoting();
    
    if (getCookie("SCOPITOWN_AwaitingFriendRequestsPopupAlreadyClosed") != "1"){
	    openPopup("#awaitingFriendRequestsPopup", true);
	}
});


function closeAwaitingFriendRequestsPopup(){
	closePopup("#awaitingFriendRequestsPopup");
	setCookie("SCOPITOWN_AwaitingFriendRequestsPopupAlreadyClosed", "1");
}


function configureVoting(){
	$('.adresse .note').click(function(e){
    	var x = e.pageX - $(this).offset().left;
		var note = Math.ceil(x / 16);
		
		$(this).find("span").css('width', note*16)
		adresseID = $(this).parent().attr('id').substr(3);
		userID = $("input[name=userID]").val();
		
		$.get('content/externals/vote.php', {note: note, id: adresseID, type: "adresse", userID: userID}, function(data){
			$('#ad-' + adresseID).find('.note span').css('width', parseFloat(data) * 16);
		});
    })  
}

function configureCityMenu(){
	// Handle the close events of the city menu
	// escape pressed;
	
		$('.header .ville ul').hide();
	$(document).keyup(function(e) {
	  if (e.keyCode == 27) {
	  	if (currentPopup != "") closePopup(currentPopup);
		$('.header .ville ul').slideUp();
	  }
	});
	// click on the body
	$('body').click(function() {
		$('.header .ville ul').slideUp();
	});
	// click on the menu overrides the body click
	$('.header .ville').click(function(event){
		event.stopPropagation();
	});
}

function openCityMenu(){
	$('.header .ville ul').slideToggle();
	fleche = $('.header .ville > a');
	if (fleche.hasClass('opened')){
		fleche.removeClass('opened');
	} else {
		fleche.addClass('opened');
	}
}

function configureBlocks(){
	$('.block.relative').each(function(){
		relative = $(this).find("input[name=relativeTo]").val();
		$(this).css("height", $("#"+relative).height() + "px");
	});
}

function configureMenu(){
	$('#main-menu').find('.sub').hide();
    $('#main-menu a.active').next('.sub').show();
    
    if($('#main-menu a.active').next('.sub').length > 0){	
	   	$('#main-menu').css('height', '60px');	
    }
}

function setStarsOpacity(notepicker, note){
   	count = 1;
	notepicker.find('li').each(function(){
		value = (count <= note) ? 1 : 0.4;
		$(this).css('opacity', value);
		count++;
	})
}


function showSub(className){
   	$('#main-menu .sub').slideUp(100);
   	$('#main-menu .sub.'+className).slideDown(200);
   	$('#main-menu').animate({height: 60}, 200);
}



function setNote(){
	np = $('.notepicker');
	
	id = np.find('.id').attr('value');
	note = np.find('.note').attr('value');
	type = np.find('.type').attr('value');
	userID = np.find('.userID').attr('value');
	
	np.fadeOut('slow');	
	$('.modal').slideUp('fast');

	$.get('content/externals/vote.php', {note: note, id: id, type: type, userID: userID}, function(data){
		$('#ad-' + id).find('.note span').css('width', parseFloat(data) / 2 * 16);
		//alert(data)
	});
}

function closeNotepicker(){
	$('.modal').slideUp('fast');
	$('.notepicker').fadeOut('slow');
}

//COOKIES

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
	    	c_start=c_start + c_name.length+1;
	    	c_end=document.cookie.indexOf(";",c_start);
	    	if (c_end==-1) c_end=document.cookie.length;
	    	
	    	return unescape(document.cookie.substring(c_start,c_end));
	    }
	}
	return "";
}

