$(function()
  {
		$('a[ajax]').live('click', function(e)
			{
				e.preventDefault();

				$('#ajax_content').addClass('loading');
				$('#ajax_content').fadeTo(100, 0.3);

				$(this).oneTime(100, function()
					{
						$.ajax(
							{
								url: $(this).attr('ajax'),
								cache: true,
								type: 'POST',
								data: 'ajax=1',
								success: function(html)
									{
										$('#ajax_content').html(html);
										$('#ajax_content').fadeTo(400, 1);
										$('#ajax_content').removeClass('loading');

										$.scrollTo(0, 500);

										common();
									}
							});
					});
			});

    /* COMMON: BEGIN */
    var common = function()
      {
				$('div.goto_top div').removeClass('hidden');
				$('div.goto_middle div').removeClass('hidden');
				$('div.goto_bottom div').removeClass('hidden');

				$('a.goto_top').click(function(e)
				  {
					  e.preventDefault();
					  $.scrollTo($('#goto_top').offset().top-55, 500);
					});

				$('a.goto_middle').click(function(e)
				  {
					  e.preventDefault();
					  $.scrollTo($('#goto_middle').offset().top-55, 500);
					});

				$('a.goto_bottom').click(function(e)
				  {
					  e.preventDefault();
					  $.scrollTo($('#goto_bottom').offset().top-55, 500);
					});

        $('#ajax_content').livequery(function()
          {
            $(window).resize();
          });

        $('.gallery').each(function()
          {
            gallery(this);
          });

				$('.control .prev').css('left', ($('.control .prev').parent().width() * 0.15 / 4 - 13) + 'px');
				$('.control .next').css('right', ($('.control .next').parent().width() * 0.15 / 4 - 13) + 'px');

				$(window).resize(function(e)
				  {
			      $('.control .prev').css('left', ($('.control .prev').parent().width() * 0.15 / 4 - 13) + 'px');
				    $('.control .next').css('right', ($('.control .next').parent().width() * 0.15 / 4 - 13) + 'px');
					});

				$('.columnize').columnize({ width: 300 });

        $('.lazyload').find('img').lazyload();
      }

    common();
    /* COMMON: END */

    /* COMMENTS: BEGIN */
    $('#comments .fb_tab').live('click', function(e)
      {
        e.preventDefault();

        $(this).find('div').removeClass('deselected').addClass('selected');
        $('#comments .vk_tab').find('div').removeClass('selected').addClass('deselected');

        $('#comments .content .vk_comments').fadeOut(500);

        $('#comments .content .fb_comments').oneTime(500, function()
          {
            $(this).fadeIn(500);
          });
      });

    $('#comments .vk_tab').live('click', function(e)
      {
        e.preventDefault();

        $(this).find('div').removeClass('deselected').addClass('selected');
        $('#comments .fb_tab').find('div').removeClass('selected').addClass('deselected');

        $('#comments .content .fb_comments').fadeOut(500);

        $('#comments .content .vk_comments').oneTime(500, function()
          {
            $(this).fadeIn(500);
          });
      });
    /* COMMENTS: END */

    /* SLICE LINKS: BEGIN */
    $('.slice_link div').live('click', function(e)
      {
        e.preventDefault();

        if ($(this).parent().hasClass('opened'))
          {
            $(this).parent().animate({ width:20 }, 'fast').removeClass('opened');
          }
        else
          {
            $(this).parent().animate( { width:$(this).parent().find('a').width()+30 }, 'fast').addClass('opened');
          }
      });
    /* SLICE LINKS: END */

    /* EXPANDER: BEGIN */
    var expanderHeaderHeight = $('div#wrap_top').height();
    var expanderBarHeight = $('#expander').height();

    $(window).scroll(function()
      {
        var scrollDistance = $(this).scrollTop();
        var isExpanded = $('div#wrap_top').hasClass('navfixed');

        if (expanderHeaderHeight < scrollDistance)
          {
            $('#expander').fadeIn(400);
          }

        if (expanderHeaderHeight > scrollDistance)
          {
            $('#expander').fadeOut(400);
          }

        if (isExpanded == 1)
          {
            $('div#wrap_top').removeClass('navfixed');
            $('div#wrap_content').removeClass('navfixed');
            $('div#wrap_footer').removeClass('navfixed');

            $('div#wrap_top').fadeOut(300, function()
              {
                $(this).show();
              });
          }
      });

    $('#expander').hover(function(e)
		//$('#expander').click(function(e)
      {
        e.preventDefault();

        $(this).fadeOut(400);

        $('div#wrap_top').addClass('navfixed');
        $('div#wrap_content').addClass('navfixed');
        $('div#wrap_footer').addClass('navfixed');

        $('div#wrap_top').fadeIn(400);
      });

    $('#expander a').click(function(e)
      {
        e.preventDefault();
      });
    /* EXPANDER: END */

    return true;
  });

(function($)
  {
    /* MULTISWAP: BEGIN */
    $.fn.multiswap = function(options)
      {
	      var defaults =
          {
		        period: 100
	        };

        var options = $.extend(defaults, options),
            timer = null,
            index = 0,
            defSrc = this.attr('src'),
            imgData = [];

        for (var i=0, j=0; i < options.images.length; i++, j++)
          {
            imgData[j] = new Image();
            str = options.images[i];
            imgData[j].src = str.replace(/\&amp;/g,'&');
          }

        this.each(function()
          {
            $(this).mouseout(function()
              {
                $(this).stopTime(timer);

                this.src = defSrc;
                index = 0;
              });

            $(this).blur(function()
              {
                $(this).stopTime(timer);

                this.src = defSrc;
                index = 0;
              });

            $(this).mouseover(function()
              {
                $(this).mouseout();

                $(this).everyTime(options.period, timer, function()
                  {
                    if (index == imgData.length) index = 0;
                    $(this).attr('src', imgData[index++].src);
                  });
              });

            $(this).focus(function()
              {
                $(this).mouseout();

                $(this).everyTime(options.period, timer, function()
                  {
                    if (index == imgData.length) index = 0;
                    $(this).attr('src', imgData[index++].src);
                  });
              });
          });
      }
    /* MULTISWAP: END */
  })(jQuery);
