jQuery.fn.checkMyBox = function()
{

  this.wrap('<span class="check_my_box"></span>');
  this.before('<img style="position: relative;" src="/media/images/site/misc/checkbox.jpg" alt="site/misc/checkbox.jpg" />');
  this.hide();

  this.parent().bind('click', function()
  {
    element = jQuery(this);
    if (element.hasClass('check_my_box_checked'))
    {
      element.children('img').css('top', '0px');
      element.children('input').attr('checked', false);
      element.removeClass('check_my_box_checked');
    }
    else
    {
      element.children('img').css('top', '-22px');
      element.children('input').attr('checked', true);
      element.addClass('check_my_box_checked');
    }
  }).css({
    position: 'relative',
    display: 'block',
    width: '22px',
    height: '22px',
    overflow: 'hidden',
    cursor: 'pointer'
  });

  this.bind('change', function()
  {
    if (jQuery(this).is(':checked') == jQuery(this).parent().hasClass('check_my_box_checked'))
    {
      return;
    }
    if (jQuery(this).is(':checked'))
    {
      jQuery(this).parent().addClass('check_my_box_checked');
    }
    else
    {
      jQuery(this).parent().removeClass('check_my_box_checked');
    }
  });

};

jQuery(document).ready(function()
{

  /**
   * Opens the links with class new_windows in a new tab/window
   */
  jQuery('.new_window').attr('target', '_blank');


  /**
   * Changes the default checkbox look with a more colorful one
   */
  jQuery('input[type="checkbox"]').checkMyBox();


  /**
   * Makes the "back to top" link nicer with a smooth scroll
   */
  jQuery('#button_open_comments').click(function(event)
  {
    jQuery.scrollTo('#comments', 1000, {axis: 'y'});
    event.preventDefault();
    return false;
  });
  jQuery('.footer_action_link_back_to_top').click(function(event)
  {
    jQuery.scrollTo('#site_top', 1000, {axis: 'y'});
    event.preventDefault();
    return false;
  });


  /**
   * Shows and hides the forms at the end of the pages
   */
  jQuery('.contact_modules_form').hide();
  jQuery('.button_action').click(function()
  {
    if (jQuery(this).hasClass('selected'))
    {
      return;
    }
    jQuery('.contact_modules_form').hide();
    jQuery('.contact_modules_form_' + jQuery(this).attr('id').replace('button_open_', '')).show();
    jQuery('.button_action').removeClass('selected');
    jQuery(this).addClass('selected');
  
  });


  /**
   * Prepares the privacy text toggle link
   */
  function bind_privacy_toggle(labelToggler, privacyTextSelector)
  {
    labelToggler = jQuery(labelToggler);
    if (labelToggler.size() === 0)
    {
      return;
    }
    id = labelToggler.attr('id').replace('privacy_open_', '');

    privacyText = jQuery('#' + privacyTextSelector + '_' + id);
    privacyText.hide();

    textLabel = '<span class="privacy_toggle" id="privacy_toggle_' + id + '"> - <a title="Visualizza di seguito il testo della normativa sulla privacy" href="javascript:void(0)">apri &darr;</a></span';
    labelToggler.after(textLabel);
    jQuery('#privacy_toggle_' + id + ' a').data('privacy_text', '#' + privacyTextSelector + '_' + id).click(function()
    {
      elem = jQuery(jQuery(this).data('privacy_text'));
      elem.toggleClass('privacy_text_visible');
      if (elem.hasClass('privacy_text_visible'))
      {
        jQuery(this).html('chiudi &uarr;');
        elem.fadeIn('slow');
      }
      else
      {
        jQuery(this).html('apri &darr;');
        elem.fadeOut('slow');
      }
    });
  }
  bind_privacy_toggle('#privacy_open_contact', 'privacy_text');
  bind_privacy_toggle('#privacy_open_newsletter', 'privacy_text');
  bind_privacy_toggle('#privacy_open_work', 'privacy_text');


  /**
   * Filters the elements in the portfolio page
   */
  jQuery('.portfolio_project a').each(function()
  {
    jQuery(this).data('real_url', jQuery(this).attr('href'));
  });
  var filter_visible = [];
  function update_filter(val, action)
  {
    if (action == 'add')
    {
      filter_visible[val] = true;
    }
    else
    {
      delete filter_visible[val];
    }
    view = '';
    var url_filter = []
    for (var filter in filter_visible)
    {
      view += '.service_' + filter + ', ';
      url_filter.push(filter);
    }
    url_filter = url_filter.join(',');

    view += '.not_existant';
    jQuery('.portfolio_project').hide();
    jQuery(view).each(function()
    {
      link = jQuery(this).show().find('a');
      href = link.data('real_url');
      link.attr('href', href + '?servizi=' + url_filter);
    });
    if (url_filter != '')
    {
      url_filter = '#' + url_filter;
    }
    window.location.hash = url_filter;
    jQuery(view).show();
  }
  jQuery('.portfolio_filter .check_my_box').click(function()
  {
    element = jQuery(this).children('input');
    val = element.val();
    if (element.attr('checked'))
    {
      update_filter(val, 'add');
    }
    else
    {
      update_filter(val, 'remove');
    }
  });
  jQuery('.portfolio_filter_all a').click(function()
  {
    filter_visible = [];
    jQuery('.portfolio_filter .check_my_box').each(function()
    {
      element = jQuery(this);
      element.children('img').css('top', '-22px');
      val = element.children('input').attr('checked', true).val();
      filter_visible[val] = true;
      element.addClass('check_my_box_checked');
    });
    jQuery('.portfolio_project').show().find('a').each(function()
    {
      jQuery(this).attr('href', jQuery(this).data('real_url'));
    });
  });
  if (jQuery('.portfolio_project').size() > 0)
  {
    var filter_hash = window.location.hash;
    if (filter_hash != '' && filter_hash != '#')
    {
      filters = filter_hash.substring(1).split(',');
      for (var i in filters)
      {
        jQuery('input#filter_' + filters[i]).parent().click();
      }
    }
  }


  /**
   * Creates the scroll effect in the portfolio page
   */
   var width = 130;
   var total_width = (jQuery('.portfolio_scrollbar_inside a').size() * width) + 10;
   var bar_width = jQuery('.portfolio_scrollbar_items').width();
   jQuery('.portfolio_scrollbar_go_right img').click(function()
   {
     left = parseInt(jQuery('.portfolio_scrollbar_inside').css('left')) - (width * 2);
     if (total_width + left < 0)
     {
       jQuery('.portfolio_scrollbar_inside').css({left: bar_width}).animate({left: bar_width - width});
     }
     else
     {
       jQuery('.portfolio_scrollbar_inside').animate({left: left});
     }
   });
   jQuery('.portfolio_scrollbar_go_left img').click(function()
   {
     left = parseInt(jQuery('.portfolio_scrollbar_inside').css('left')) + (width * 2);
     if (left > bar_width)
     {
       jQuery('.portfolio_scrollbar_inside').css({left: -total_width}).animate({left: -total_width + width});
     }
     else
     {
       jQuery('.portfolio_scrollbar_inside').animate({left: left});
     }
   });
   jQuery('.portfolio_scrollbar_inside').css('width', total_width);
   var number, position = 0;
   jQuery('.portfolio_scrollbar_inside a').each(function()
   {
     ++position;
     if ($(this).hasClass('selected'))
     {
       number = position;
     }
   });
   position = - (number * width) + 400;
   jQuery('.portfolio_scrollbar_inside').css({left: position});


   /**
    * Creates the tab effect in the portfolio project page
    */
    jQuery('.portfolio_project_tab_content').hide();
    jQuery('.portfolio_project_tab').click(function()
    {
      jQuery('.portfolio_project_tab_content').hide();
      jQuery('.content_' + jQuery(this).attr('id').replace('tab_', '')).show();
      jQuery('.portfolio_project_tab div').removeClass('selected');
      jQuery(this).children('div').addClass('selected');
    });
    jQuery('#tab_1').click();


  /**
   * Creates the overlay effect in the portfolio page
   */
  jQuery('.portfolio_project').hover(function()
  {
    jQuery(this).children('.image_overlay').fadeOut('fast');
  }, function()
  {
    jQuery(this).children('.image_overlay').fadeIn('fast');
  });


  /**
   * Cycles the images in the portfolio project page
   */
  jQuery('.portfolio_slideshow').cycle({
    timeout: 5000,
    fx: 'fade',
    speed: 'fast',
    pager: '.portfolio_slideshow_player .portfolio_info_content'
  });


  /**
   * Shows and hides text in the faq page
   */
  jQuery('.faq_body').hide();
  jQuery('.faq_title').click(function()
  {
    jQuery(this).toggleClass('faq_title_selected').next().slideToggle();
  });


  /**
   * Open the tweet links in a new window
   */
  jQuery('.homepage_news_tweet a').attr('target', '_blank');
  jQuery('.twitter_box a').attr('target', '_blank');


  /**
   * Pre-opens a tab at the bottom of the page
   */
  if (typeof(open_tab) != 'undefined')
  {
    jQuery('#button_open_' + open_tab).click();
  }

  /**
   * Creates the tab section in the "how we work" page
   */
  jQuery('.block_tab').hide();
  jQuery('.block_tab_nav span').click(function()
  {
    jQuery('.block_tab_nav span').removeClass('selected');
    jQuery(this).addClass('selected');
    val = '#block_tab_' + jQuery(this).text();
    jQuery('.block_tab').hide();
    jQuery(val).show();
  });
  jQuery('.block_tab_nav span:first').click();

});
