// $Id: dynosearcho.js,v 1.2.2.1.2.1 2009/03/26 08:11:45 nk Exp $

/**
 * Register DynoSearcho behavior 
*/
Drupal.behaviors.dynosearcho = function (context) { 
  $('input.dynosearcho-searchbox').each(function () {
    var delta = $(this).parent().siblings("input[name='dynosearcho_module']").val();
    var input = '#edit-dynosearcho-search-' + delta;
    var expres = '#edit-dynosearcho-exp-' + delta;
    var wrapper = '#dynosearcho-results-' + delta; 
    var searchon = $('#edit-dynosearcho-integrated-search-' + delta).val();
    $('#edit-dynosearcho-search-0-wrapper').css('float', 'left');
    
    Drupal.dynosearcho.check(input, wrapper);
    Drupal.dynosearcho.preventEnter (input, searchon);
    Drupal.dynosearcho.triggerAhah(input, expres, wrapper);
  });
}

Drupal.dynosearcho = new Object();

/**
 * Register DynoSearcho block configuration behavior
 */
Drupal.behaviors.dynosearchoAdmin = function (context) {
  //----- set the default state
  $('fieldset#dynosearcho-nodesearch').hide();
  $('fieldset#dynosearcho-usersearch').hide();
  switch($('input.dynosearcho-searchtype:checked').val()) {
    case 'node':
      $('fieldset#dynosearcho-nodesearch').show();
    break;
    case 'user':
      $('fieldset#dynosearcho-usersearch').show();
      break;
  }
  //----- update the boxes as radio buttons are clicked
  $('input.dynosearcho-searchtype').click(function () {
    if ($(this).val() == 'node') {
      $('fieldset#dynosearcho-usersearch').hide();
      $('fieldset#dynosearcho-nodesearch').slideDown();
    } 
    else {
      $('fieldset#dynosearcho-nodesearch').hide();
      $('fieldset#dynosearcho-usersearch').slideDown();
    } 
  });
}
/**
 * Helper function to prevent the enter key used to submit the form here, if integrated search is off
 */
Drupal.dynosearcho.preventEnter = function(input, searchon) {
  $(input).bind('keydown', function(e) {
    if (!e) {
      e = window.event;
    }
    switch (e.keyCode) {
      case 13: // enter
        if (searchon != 1) {
          e.preventDefault();
        }else{
          $(this).unbind('keyup');
        }
    }
  });
}

/**
 * Helper function - triggers blur() event on ahah form element to execute ahah search.
 */
Drupal.dynosearcho.triggerAhah = function(input, expres, wrapper) {
  var delay = 300;
  $(input).unbind('keyup').bind('keyup', function () {
    if (this.value.length == 0) {
      $(wrapper).hide();
      $(wrapper).empty();
    }else{
      $(wrapper).show();
      $('.throbber').remove();
      $(expres).val(this.value);
        if (this.timer) {
          clearTimeout(this.timer);
        }
        this.timer = setTimeout(function() {
          $(expres).blur();
        }, delay);
    }
    });
}

/**
 * Helper function - show wrapper when checkbox is checked.
 */
Drupal.dynosearcho.check = function(input, wrapper) {
  $('.dynosearcho-boxes div.form-item input').each(function() {
    $(this).change(function() {
      if($(input).val()) {
        $(wrapper).show();
      }
    });
  });
};
(function($) {
	//Drupal.behaviors.fivestar = {
		$(document).ready(function() {
			$('a#puntuaffi-button').hide();
			$('a#soddisfazione-button').hide();
			$('a#prezzo-button').hide();
			$('input#prezzo-voto').removeAttr('value');
			$('ul#sub-profilo').hide();
			//Gestione tab last-community home page
			$('div#community-last-accordion div#fragment-2').hide();
			$('div#community-last-accordion div#fragment-3').hide();
			$('div#community-last-accordion div#fragment-4').hide();
			$("#community-last-accordion").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 10000, true);
			//$('li.community-show').click(function(){
				
				/*value = $(this).attr('value');
				$('div#community-last-accordion div#last-question-1').hide('slow');
				$('div#community-last-accordion div#last-question-2').hide('slow');
				$('div#community-last-accordion div#last-question-3').hide('slow');
				$('div#community-last-accordion div#last-question-4').hide('slow'); 
				$('div#community-last-accordion div#last-question-'+value).show('slow');*/
			//});
			//ELIMINA IMMAGINI SALVATE
			$('a#elimina').click(function(){
				fid = $(this).attr('value');
				selection = confirm("Sei sicuro di voler eliminare la foto?");
				if(selection == true){
					url = '/immagine/elimina/' + fid ;
					$.ajax({
						type: "GET",
						url: url,
						async: true,
						contentType: 'application/x-www-form-urlencoded',
						success: function(new_nid){
					//	window.location = Drupal.settings.basePath + 'node/' + new_nid + '/edit' ;
							$('div#image-' + fid).hide('slow');
					}
					});
		
				}
			});
			//segnala feed
			$("a.segnala").click(function(){
				feid = $(this).attr("feid");
				 jQuery( "#popup" ).dialog({
						modal: true,
						buttons: {
							Segnala: function() {
								$( this ).dialog( "close" );
								url = '/feedback/segnala/' + feid ;
								$.ajax({
									type: "GET",
									url: url,
									async: true,
									contentType: 'application/x-www-form-urlencoded',
									success: function(result){
								//	window.location = Drupal.settings.basePath + 'node/' + new_nid + '/edit' ;
										//$('div#image-' + fid).hide('slow');
										
										if(result == true){
											alert("Segnalazione completata");
										}else{
											alert("Errore durante la segnalazione. Contattare il webmaster");
										}
								}
								});
							}
						}
				});
			});
			
			$("a.errore-404").click(function(){
				err = window.location.pathname;
				err = err.replace("http://", "");
				 jQuery( "#popup404" ).dialog({
						modal: true,
						buttons: {
							Segnala: function() {
								$( this ).dialog( "close" );
								url = '/errore-404/segnala' + err ;
								$.ajax({
									type: "GET",
									url: url,
									async: true,
									contentType: 'application/x-www-form-urlencoded',
									success: function(result){
								//	window.location = Drupal.settings.basePath + 'node/' + new_nid + '/edit' ;
										//$('div#image-' + fid).hide('slow');
										
										if(result == true){
											alert("Segnalazione completata");
										}else{
											alert("Errore durante la segnalazione. Contattare il webmaster");
										}
								}
								});
							}
						}
				});
			});

			$("a.community-categorie").click(function(){ 
				$('div#community-cat').slideToggle("fast");
			});
			$("a.leggi-categorie").click(function(){ 
				$('div#leggi-cat').slideToggle("fast");
			});
			$("a.close-cat").click(function(){ 
				$('div#community-cat').slideToggle("fast");
			});
			$("a.close-leggi-cat").click(function(){ 
				$('div#leggi-cat').slideToggle("fast");
			});
			
			$("a.topbar-registrati").click(function(){ 
				$('div#tendina-topbar').slideToggle("fast");
			});
			$('a.topbar-menu').cluetip({activation: 'click',closeText:'',local:true,width:200, sticky: true,arrows: true,cluetipClass: 'rounded',showTitle:false, mouseOutClose:true});
			$('a.topbar-abbonamento').cluetip({closeText:'',local:true, sticky: true,positionBy:'bottomTop',showTitle:false,cursor:'help', mouseOutClose:true});
			$('a.cluetip-button').cluetip({activation: 'click',closeText:'',width:200,arrows: true, sticky: true, splitTitle: '|', cluetipClass: 'rounded', showTitle: false});
			$('a.privacy_cluetip').cluetip({activation: 'click',closeText:'',local:true, sticky: true,showTitle:false,mouseOutClose:true,width:500, cluetipClass: 'rounded',arrows: true,});

			$('div#tendina-topbar').parent().click(function(){	
				$('div#tendina-topbar').hide("fast");
			});
			//hack per il menu a tendina
			$('div#whole-wrapper').click(function(){	
				$('div#tendina-topbar').hide("fast");
			});
			
			//chiedi assistenza
			$("a.assistenza").click(function(){
				var settore = $( "#settore" ), 
				oggetto = $( "#oggetto" ),
				richiesta = $( "#richiesta" ),
				allFields = $( [] ).add( settore ).add( oggetto ).add( richiesta ),
				tips = $( ".validateTips" );
				uid = $(this).attr("uid");
				function updateTips( t ) {
					tips
						.text( t )
						.addClass( "ui-state-highlight" );
					setTimeout(function() {
						tips.removeClass( "ui-state-highlight", 1500 );
					}, 500 );
				}
				function checkLength( o, n, min, max ) {
					if ( o.val().length > max || o.val().length < min ) {
						o.addClass( "ui-state-error" );
						updateTips( "La lunghezza del campo " + n + " deve essere compresa tra " +
							min + " e " + max + " caratteri." );
						return false;
					} else {
						return true;
					}
				}
				 jQuery( "#popup-support" ).dialog({
					 	height: 500,
						width: 650,
						modal: true,
						buttons: {
							"Invia la richiesta": function() {
					 			var bValid = true;
					 			allFields.removeClass( "ui-state-error" );
					 			bValid = bValid && checkLength( settore, "settore", 1, 20 );
								bValid = bValid && checkLength( oggetto, "oggetto", 1, 200 );
								bValid = bValid && checkLength( richiesta, "richiesta", 5, 20000 );
								if ( bValid ) {
								url = '/supporto/' + settore.val() + '/' + uid + '/' + oggetto.val() + '/' + richiesta.val() ;
								
								$.ajax({
									type: "GET",
									url: url,
									async: true,
									contentType: 'application/x-www-form-urlencoded',
									success: function(result){
								//	window.location = Drupal.settings.basePath + 'node/' + new_nid + '/edit' ;
										//$('div#image-' + fid).hide('slow');
										if(result == true){
											$("#popup-support").dialog("close");
											alert("Richiesta inviata.");
										}else{
											alert("Errore durante la richiesta. Ti preghiamo di riprovare piu' tardi.");
										}
									}
									
								});
								}
							}
				 			
						
				 	
						},
						close: function() {
							allFields.val( "" ).removeClass( "ui-state-error" );
						}
				 
				});
			});

			$('select#edit-dove').change(function(){
				var index = $(this).attr("value");
				var s = document.getElementById('edit-comuni');
				s.options.length = 0;
			    s.options[0] = new Option ("Tutti i comuni","Tutti i comuni");
			    s.options[0].selected="true" ;
				url = '/comuni/get/' + index ;
				$.getJSON(url, function(data) {
					  var items = [];

					  $.each(data, function(key, val) {
					    items[key] = val;
					    s.options[s.options.length]= new Option(val, key);
					  });
				});
			});
			$('body').delegate("a.paging-ricerca", "click", function() {
				$('div#nav-search').addClass('overlay');
				var range = $(this).attr("range");
				var categoria = $('select#edit-categoria').attr("value");
				var comuni = $('select#edit-comuni').attr("value");
				var chi = $('input#edit-chi').attr("value");
				var dove = $('select#edit-dove').attr("value");
				var sabato = $('input#edit-sabato').attr('checked');
				var domenica = $('input#edit-domenica').attr('checked');
				var serale = $('input#edit-serale').attr('checked');
				var all = $('input#edit-all').attr('checked');
				var post = 'categoria='+categoria+'&comuni='+comuni+'&chi='+chi+'&dove='+dove+'&sabato='+sabato+'&domenica='+domenica+'&serale='+serale+'&all='+all;
				
				url = '/ricerca/ajax/paging/' + range +'/'+post;
				$.ajax({
					type: "GET",
					url: url,
					async: true,
					contentType: 'html',
					success: function(result){
						$('div#search-result').html(result);
					}
					
				});
			});
			//GESTIONE GALLERY
			/*$('span#gallery-thumb').click(function(){
				image = $(this).attr('image');
				fid = $(this).attr('fid');
				$('div#wrapper-info').html('');
				$('div#wrapper-immagine').html('<img src="'+image+'">').hide();
				$('div#wrapper-immagine').show('slow');
				url = '/immagine/info/' + fid ;
				$.ajax({
					type: "GET",
					url: url,
					async: true,
					contentType: 'application/x-www-form-urlencoded',
					success: function(info){
						$('div#wrapper-info').html(info);
					}
				});
			});*/

		});

	//}
}(jQuery));;
/**
 * Create a degradeable star rating interface out of a simple form structure.
 *
 * Originally based on the Star Rating jQuery plugin by Wil Stuckey:
 * http://sandbox.wilstuckey.com/jquery-ratings/
 */
(function($){ // Create local scope.
Drupal.behaviors.fivestar = {
  attach: function (context) {
    $('div.fivestar-form-item').once('fivestar', function() {
      var $this = $(this);
      var $container = $('<div class="fivestar-widget clearfix"></div>');
      var $select = $('select', $this);
      $('a#puntuaffi-button').hide();
      var $options = $('option', $this).not('[value="-"], [value="0"]');
      var index = -1;
      $options.each(function(i, element) {
        var classes = 'star-' + (i+1);
        classes += (i + 1) % 2 == 0 ? ' even' : ' odd';
        classes += i == 0 ? ' star-first' : '';
        classes += i + 1 == $options.length ? ' star-last' : '';
        $('<div class="star"><a href="#' + element.value + '" title="' + element.text + '">' + element.text + '</a></div>')
          .addClass(classes)
          .appendTo($container);
        if (element.value == $select.val()) {
          index = i + 1;
        }
      });
      
      $container.find('.star:lt(' + index + ')').addClass('on');
      $container.addClass('fivestar-widget-' + ($options.length));
      $container.find('a')
        .bind('click', $this, Drupal.behaviors.fivestar.rate)
        .bind('mouseover', $this, Drupal.behaviors.fivestar.hover);

      $container.bind('mouseover mouseout', $this, Drupal.behaviors.fivestar.hover);

      // Attach the new widget and hide the existing widget.
      $select.after($container).css('display', 'none');
    });
  },
  rate: function(event) {
    var $this = $(this);
    var $widget = event.data;
    var value = this.hash.replace('#', '');
    $('select', $widget).val(value).change();
    var $this_star = $this.closest('.star');
    $this_star.prevAll('.star').andSelf().addClass('on');
    $this_star.nextAll('.star').removeClass('on');
    $('input#'+$this.parent().parent().parent().children('.form-select').attr('id')+'-voto').removeAttr('value').attr('value',$this.html());
    event.preventDefault();
  },
  hover: function(event) {
    var $this = $(this);
    var $widget = event.data;
    var $target = $(event.target);
    var $stars = $('.star', $this);

    if (event.type == 'mouseover') {
      var index = $stars.index($target.parent());
      $stars.each(function(i, element) {
        if (i <= index) {
          $(element).addClass('hover');
        } else {
          $(element).removeClass('hover');
        }
      });
    } else {
      $stars.removeClass('hover');
    }
  }
};

})(jQuery);
;
/**
 * jquery.readmore - Substring long paragraphs and make expandable with "more" link
 * @date 7 July 2010
 * @author Jake Trent  http://www.jtsnake.com/
 * @version 1.1
 */
(function ($) {
  $.fn.readmore = function (settings) {

    var opts =  $.extend({}, $.fn.readmore.defaults, settings);

    this.each(function () {
      $(this).data("opts", opts);
      if ($(this).html().length > opts.substr_len) {
        abridge($(this));
        linkage($(this));
      }else{
    	if($(this).parent().parent().children(".voti").html() != null){
    		linkage($(this));
    	}
      }
    });

    function linkage(elem,ret) {
      elem.append(elem.data("opts").more_link_down);
      elem.children(".more").click( function () {
        $(this).hide();
        $(this).siblings("span:not(.hidden)").hide().siblings("span.hidden").animate({'opacity' : 'toggle'},1000);
        $(this).parent().parent().parent().children(".voti").show("slow");
      });
    }

    function abridge(elem) {
      var opts = elem.data("opts");
      var txt = elem.html();
      var len = opts.substr_len;
      var dots = "<span>" + opts.ellipses + "</span>";
      var shown = txt.substring(0, len) + dots;
      var hidden = '<span class="hidden" style="display:none;">' + txt.substring(len, txt.length) + '</span>';
      elem.html(shown + hidden);
    }
    
    return this;
  };

  $.fn.readmorelink = function (settings) {

	    var opts =  $.extend({}, $.fn.readmore.defaults, settings);
	    
	    this.each(function () {
	      $(this).data("opts", opts);
	      url = $(this).parent().attr('url');
	      $(this).data("url", url);
	      if ($(this).html().length > opts.substr_len) {
	        abridgelink($(this));
	        linkagelink($(this));
	      }else{
	    	if($(this).parent().parent().children("#voti").html() != null){
	    		linkagelink($(this));
	    	}
	      }
	    });

	    function linkagelink(elem,ret) {
	      link = '<a href="'+elem.data("url")+'">Leggi tutto >></a>';
	      elem.append(link);
	    }

	    function abridgelink(elem) {
	      var opts = elem.data("opts");
	      var txt = elem.html();
	      var len = opts.substr_len;
	      var dots = "<span>" + opts.ellipses + "</span>";
	      var shown = txt.substring(0, len) + dots;
	      var hidden = '<span class="hidden" style="display:none;">' + txt.substring(len, txt.length) + '</span>';
	      elem.html(shown + hidden);
	    }
	    
	    return this;
	  };
  
  $.fn.readmore.defaults = {
    substr_len: 500,
    ellipses: '&#8230;',
    more_link: '<a class="more">Leggi tutto &raquo;</a>',
    more_link_down: '<br><a class="more">Leggi tutto &raquo;</a>'
  };

})(jQuery);
;

