
$(document).ready(function() {
  //trasparenza png per IE6
  $(document).pngFix();
  //
  menuCustom();
  //
  $('.buttonStyle').button();
  //
  // gestione dei tabs
  //var tmp01 = $('.jQueryUI_TabsAuto');
  //alert($('ul:first li.selected', tmp01).index());
  try {
    var tabsBlock = $('.jQueryUI_TabsAuto');
    var my_tabs = tabsBlock.tabs({
      collapsible: true,
      selected: $('ul:first li.selected', tabsBlock).index(),
      //event: 'mouseover',
      ajaxOptions: {
        error: function(xhr, status, index, anchor) {
          $(anchor.hash).html("AJAX ERROR.");
        }
      }
    });
    //my_tabs.find(".ui-tabs-nav").sortable({ axis: 'x' });
  } catch (e) { }
  //
  $('.autoWatermark').each(function(i) { $(this).watermark($(this).attr('watermark')); });
  //
  $('.clickableBlock:has(a.clickableBlockLink)')
    .live('click', function() {
      //e.preventDefault();
      //e.stopPropagation();
      window.location = $(this).find('a.clickableBlockLink').attr('href');
    })
    .live('mouseover', function() { $(this).addClass('hover'); })
    .live('mouseout', function() { $(this).removeClass('hover'); });
  //
  // json forms management functions
  //
  $('.formSubmitWithJsonReturn').unbind('click').click(function(e) {
    e.preventDefault();
    e.stopPropagation();
    //
    var successUrl = $(this).closest('a').attr('href') || document.referrer;
    $(this).closest('form').ajaxSubmit({
      //beforeSubmit: function() { alert('beforeSubmit'); },
      data: { successUrl: successUrl },
      dataType: 'json',
      success: function(result) {
        var tmpFn = function() {
          if (result.hasError == false && typeof (result.successUrl) == 'string')
            window.location = result.successUrl;
          else if (result.hasError == true && typeof (result.errorUrl) == 'string')
            window.location = result.errorUrl;
        };
        if (typeof (result.message) == 'string') {
          messageBox(result.message, result.hasError ? 'Errore' : 'Messaggio', tmpFn);
        }
        else {
          tmpFn();
        }
        //
      }
    });
  });
  //
  $('.teaser .scroll').each(function(i) {
    var teaser = $(this);
    setTimeout(function() { teaser.newsScroller(1, 5000, 1000); }, i * 500);
  });
  $('.teaser .item:has(a)')
      .live('click', function() {
        //e.preventDefault();
        //e.stopPropagation();
        window.location = $(this).closest('.item').find('a').attr('href');
      })
      .live('mouseover', function() { $(this).closest('.item').addClass('hover'); })
      .live('mouseout', function() { $(this).closest('.item').removeClass('hover'); });
  //
});


var menuCustom = function() {
  //----------------MENU' PRINCIPALE----------------------------------------------
  var menu = $('ul.sf-menu').superfish({
    delay: 300
  });
  //elimina la riga verticale a destra dell'ultima voce di 1. livello
  $('li:last-child', menu).css('background-image', 'none');
  //elimina la riga orizzontale sotto l'ultima voce dal 2. livello in poi
  $('li li:last-child', menu).css('border-bottom', '0');
  //
  //---tendina ultima voce ( li 1. livello con class="ultimo") si apre verso sinistra (2. livello di menu)
  $('li.ultimo ul', menu).css('left', $('ul.sf-menu li.ultimo').width() - $('ul.sf-menu li.ultimo ul').width());
  //---tendina ultima voce si apre verso sinistra (3. livello di menu)
  $('li.ultimo ul ul', menu).css('left', -$('ul.sf-menu li.ultimo ul').width());
  //---in alternativa tendina ultima voce si apre verso destra (3. livello di menu)
  //$('li:last-child ul ul', menu).css('left',$('ul.sf-menu li.ultimo ul').width());
}


var messageBox = function(messageHtml, title, callbackOK) {
  var markup = $('<div></div>').html(messageHtml);
  title = title || window.document.title;
  markup.dialog({
    autoOpen: true,
    title: title,
    bgiframe: true,
    modal: true,
    closeText: 'Chiudi',
    close: function() { $(this).dialog('destroy') },
    buttons: {
      Ok: function() {
        if (callbackOK && typeof (callbackOK) === "function")
          callbackOK.call($(this));
        $(this).dialog('destroy');
      }
    }
  });  //dialog
}  //messageBox


var messageBox2 = function(messageHtml, title, callbackOK, callbackCancel) {
  var markup = $('<div></div>').html(messageHtml);
  title = title || window.document.title;
  markup.dialog({
    autoOpen: true,
    title: title,
    bgiframe: true,
    modal: true,
    closeText: 'Chiudi',
    close: function() { $(this).dialog('destroy') },
    buttons: {
      Ok: function() {
        if (callbackOK && typeof (callbackOK) === "function")
          callbackOK.call($(this));
        $(this).dialog('destroy');
      },
      Annulla: function() {
        if (callbackCancel && typeof (callbackCancel) === "function")
          callbackCancel.call($(this));
        $(this).dialog('destroy');
      }
    }
  });  //dialog
}  //messageBox2


var messageBoxUrl = function(urlToLoad, title, _width, _height) {
  title = title || window.document.title;
  //$popup.dialog('option', 'width', $(popup).parent().width());
  var auxFrame = $('<div />').load(urlToLoad, null, function() {
    $(this).dialog({
      autoOpen: true,
      title: title,
      bgiframe: true,
      height: _height || 'auto',
      width: _width || 'auto',
      modal: true,
      closeText: 'Chiudi',
      close: function() { $(this).dialog('destroy') }
    });
  });
} //messageBoxUrl

