$( function()
{
  // JS SUPPORT BEGIN
  
  $(config.js_support).addClass('js');
  
  // JS SUPPORT END
  
  // TOGGLE BEGIN
  
  $('a.toggle').each( function()
  {
    if ( ! $(this).hasClass('hide'))
    {
      $(this).next('p').hide();
    } else
    {
      $(this).next('p').show();
    }
  });
  
  $('a.toggle').click( function()
  {
    $(this).toggleClass('hide');
    
    if ($(this).hasClass('hide'))
    {
      $(this).next('p').show();
    } else
    {
      $(this).next('p').hide();
    }
    
    return false;
  });
  
  // TOGGLE END
  
  // TWITTER SLIDER BEGIN
  
  $('ul#twitter_update_list').parent('div').each( function()
  {
    width = $(this).width();
    
    $(this).children('ul#twitter_update_list').children('li').css
    ({
      'width': width - 40,
      'float': 'left'
    });
  });
  
  if ($('#twitter_div').children('ul.slider-nav').length > 0)
  {
    $('#twitter_div').slider
    ({
      nav: 'ul.slider-nav',
      items: 'ul#twitter_update_list',
      wrapper_class: 'twiter_wrapper',
      visible: 1,
      slide: 1,
      speed: 500,
      fade: true,
      auto_height: false
    });
  }
  
  // TWITTER SLIDER END
  
  // DESCRIPTION FOR IMAGES FROM TITLE ATTRIBUTE BEGIN
  
  $('a.frame > img').each( function()
  {
    if ($(this).attr('title') != '' && $(this).parents('div.flickr-feed').length == 0)
    {
      title = $(this).attr('title');

      $(this).after
      (
        $('<span />').addClass('title-description').text(title)
      );
    }
  });
    
  // DESCRIPTION FOR IMAGES FROM TITLE ATTRIBUTE END
  
  // SCROLL TOP BEGIN
  
  $('a.back-to-top').click( function()
  {
    $('html,body').animate
    ({
      'scrollTop': 0
    }, 300);
    
    return false;
  });
  
  // SCROLL TOP END
  
  // TOOLTIPS BEGIN
  
  $(config.tooltips_standalone).tooltip
  ({
    tooltip: '.tooltip',
    x: 20,
    y: 25,
    standalone: true
  });
  
  $(config.tooltips).tooltip
  ({
    tooltip: '.tooltip',
    x: 20,
    y: 25
  });
  
  // TOOLTIPS END
  
  // TABLE FIX BEGIN
  /*
  $('table').wrap($('<div />').addClass('table-wrapper'));
  */
  // TABLE FIX END
  
  // TABLE TOOLTIPS BEGIN
  /*
  $('td').each( function()
  {
    if ($(this).attr('custom-description') != '')
    {
      desc = $(this).attr('custom-description');
      $(this).append($('<span />').hide().addClass('tooltip').html('<span>' + desc + '</span>'));
    }
  });
  
  $('td').tooltip
  ({
    tooltip: '.tooltip',
    x: 20,
    y: 25,
    offset: false
  });
  */
  // TABLE TOOLTIPS END
  
  // LIGHTBOX BEGIN
  
  $(config.lightbox).lightbox
  ({
    id: 'galleries',
    path: '',
    speed: 500
  });
  
  $('a.frame[rel=lightbox]').lightbox
  ({
    id: 'frame-images',
    path: '',
    speed: 500,
    single:true
  });

  // LIGHTBOX END
  
  /* CATEGORIES MENU BEGIN
    
  $('ul.categories-2 li[class!=current] ul').hide();
  
  $('ul.categories-2 li').click( function()
  {
    if ($(this).children('ul').length > 0)
    {
      $('ul.categories-2 li.current ul').slideUp(function()
      {
        $(this).parent('li').removeClass('current');  
      });
    } else
    {
      $('ul.categories-2 li.current ul').slideUp();
      $('ul.categories-2 li').removeClass('current');
    }
    
    $(this).addClass('current');
    $(this).children('ul:not(:animated)').slideDown();
    
    if ($(this).has('ul'))
    {  
      return false;
    }
  });
  
  */ //CATEGORIES MENU END
  
  // NAV BEGIN
  
  $(config.nav.elements).hover( function()
  {
    if (config.nav.effect == 'slide')
    {
      $(this).children('ul:not(:animated)').slideDown(config.nav.speed);
    } else if (config.nav.effect == 'fade')
    {
      $(this).children('ul:not(:animated)').fadeIn(config.nav.speed);
    }
    /*
    if ($(this).children('ul').offset().left + $(this).children('ul').width() > + $(document).width())
    {
      $(this).children('ul').css('left', -$(this).children('ul').width());
    }
    */
  }, function()
  {
    if (config.nav.effect == 'slide')
    {
      $(this).children('ul').slideUp(config.nav.speed);
    } else if (config.nav.effect == 'fade')
    {
      $(this).children('ul').fadeOut(config.nav.speed);
    }
  });
  
  // NAV END
  
  // FORM VALIDATION BEGIN
  
  $(config.form_validate).blur( function()
  {
    val = $(this).val();

    $(this).removeClass('valid');
      
    if (val == '' || val == $(this).data('tip'))
    {
      $(this).addClass('error');
    } else
    {
      if ($(this).attr('name') == 'email' || $(this).attr('id') == 'email' || $(this).hasClass('email'))
      {
        if (/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(val))
        {
          $(this).removeClass('error').addClass('valid');
        } else
        {
          $(this).addClass('error');
        }
      } else
      {
        $(this).removeClass('error').addClass('valid');
      }
    }
  });

  // FORM VALIDATION END
  
  // FORM TIPS BEGIN
  
  $(config.form_tips).each( function()
  {
    if ($(this).attr('name') != '' && $(this).attr('name') in config.formdata)
    {
      tip = config.formdata[$(this).attr('name')];
      $(this).data('tip', tip);
      
      if ($(this).val() == '')
      {
        $(this).val(tip);
      }
    } else
    {
      $(this).data('tip', '');
    }
  });

  $(config.form_tips).focus( function()
  {
    if ($(this).val() == $(this).data('tip'))
    {
      $(this).val('');
    }
  });
    
  $(config.form_tips).blur( function()
  {
    if ($(this).val() == '')
    {
      $(this).val($(this).data('tip'));
    }
  });

  $('form').submit( function()
  {
    $(this).find('input, textarea').each( function()
    {
      if ($(this).data('tip') != '')
      {
        if ($(this).val() == $(this).data('tip'))
        {
          $(this).val('');
        }
      }
      
      $(this).blur();
    });
    
    if ($(this).find('input.error, textarea.error').length > 0)
    {
      return false;
    }
  });
  
  // FORM TIPS END
  
  // PAGINATION POSITIONING BEGIN
  
  $pagination = $('ul.pagination');
  
  $pagination.each( function()
  {
    if (! $(this).hasClass('compact'))
    {      
      pagination_width = 0;
  
      $(this).children('li').each( function()
      {
        pagination_width += $(this).outerWidth(true);
      });
      
      $(this).width(pagination_width);
      $(this).css('margin-left', $(this).parent().width() / 2 - $(this).outerWidth(true) / 2 + 40);
    }
  });

  // PAGINATION POSITIONING END
  
  // BORDER FIX BEGIN
  
  if ($.browser.msie || ($.browser.mozilla && $.browser.version < '1.9') || $.browser.opera)
  {
    $(config.border_shadow).addClass('border');
  }
  
  // BORDER FIX END
  
  // DROP CAP BEGIN
  
  $('p.first-letter').each( function()
  {
    html = $(this).html();
    
    for (var i = 0; i < html.length; i++)
    {
      if (i == 0 && /^[a-z]+$/.test(html.substring(0, 1).toLowerCase()))
      {
        $(this).html('<span class="dropcap">' + html.substring(0, 1) + '</span>' + html.substring(i + 2, html.length));
        break;
      } else
      {
        if (html.substring(i, 1) == '>' && /^[a-z]+$/.test(html.substring(i + 1, 1).toLowerCase()))
        {
          
          $(this).html(html.substring(0, i) + '<span class="dropcap">' + html.substring(i + 1, 1) + '</span>' + html.substring(i + 2, html.length));
          break;
        }
      }
    }
  });
  
  // DROP CAP END
});

// NIVO SLIDER INITIALIZATION BEGIN

$(window).load( function()
{
  // image sliders should be initialized, when all images are loaded ($(window).load(), not $(document).ready())
  $(config.nivo.elements).nivoSlider(config.nivo.options);
  
  $nivo_control = $('div.nivo-controlNav');
  
  $nivo_control.each( function()
  {
    parent_width = $(this).parent().width() / 2;
    control_width = $(this).outerWidth(true) / 2;

    $(this).css('margin-left', parent_width - control_width);
  });
  
  if ($.browser.msie || ($.browser.mozilla && $.browser.version < '1.9') || $.browser.opera)
  {
    $(config.nivo.elements).wrap($('<div />').addClass('border'));
  }
  
  // SLIDERS BEGIN
  
  $('#partners').slider
  ({
    nav: 'ul.slider-nav',
    items: 'ul.items',
    visible: 5,
    slide: config.partners.slide,
    speed: config.partners.speed,
    fade: config.partners.fade,
    easing: config.partners.easing
  });
  
  
  $('div.success-stories').slider
  ({
    nav: 'ul.slider-nav',
    items: 'ul.items',
    wrapper_class: 'success-stories-wrapper',
    visible: 1,
    slide: 1,
    speed: config.success_stories.speed,
    fade: config.success_stories.fade,
    easing: config.success_stories.easing
  });
  
  $('div.success-stories-wrapper').css
  ({
    'padding-left': '1px',
    'padding-right': '1px'
  });
  
  $('div.testimonials').each( function()
  {
    width = $(this).width();
    
    $(this).children('ul.items').children('li').css('width', width - (width * 0.2));
  });
  
  $('div.testimonials').slider
  ({
    nav: 'ul.slider-nav',
    items: 'ul.items',
    visible: 1,
    slide: 1,
    speed: config.testimonials.speed,
    fade: config.testimonials.fade,
    easing: config.testimonials.easing,
    auto_height_parent: true
  });
  
  // SLIDERS END
  
  // DESCRIPTION FOR IMAGES FROM TITLE ATTRIBUTE BEGIN
  
  $('a.frame > img').each( function()
  {
    width = $(this).width();
      
    if ($(this).next('span.title-description').length > 0)
    {
      $(this).next('span.title-description').css('width', width);
    }
  });
    
  // DESCRIPTION FOR IMAGES FROM TITLE ATTRIBUTE END
});

// NIVO SLIDER INITALIZATION END

