$(window).load(function() {
  var l = 0;

  $('div.socialne_siete').children('a.icon_fb').delay(500).animate({ marginTop: '27px', height: '74px' }, { duration: 1000, easing: 'easeOutExpo' });
  $('div.socialne_siete').children('a.icon_twitter').delay(650).animate({ marginTop: '27px', height: '74px' }, { duration: 1000, easing: 'easeOutExpo' });
  $('div.socialne_siete').children('a.icon_rss').delay(750).animate({ marginTop: '27px', height: '74px' }, { duration: 1000, easing: 'easeOutExpo' });

  $('span.corners-top, span.corners-bottom').remove();

  $('a', 'div.socialne_siete').hover(function () {
    $(this).stop().animate({ marginTop: 0 }, 200);
  }, function () {
    $(this).animate({ marginTop: '27px' }, { duration: 1000, easing: 'easeOutBounce' });
  });
  
  $('div.cpanel').each(function() {
    var sirka = $(this).width() - 46,
        vyska = $(this).children('div').height();

    if (vyska < 147)
    {
      $('span.ciara_left, span.ciara_right', this).remove();
    }

	  if (vyska > 147 && $(this).parent().is('.sidebar'))
	  {
      var ciary = true;
      $(this).css({ height: $(this).children('div').height() + 49 });
    } else if (vyska > 147 && $(this).parent().is('.page_content')) {
      var ciary = true,
          pozicia = $(this).position();
      l++;

      $(this).css({ height: $(this).children('div').height() + 49 });
      if (parseInt(l) == 3)
      {
        $(this).css({ height: $(this).children('div').height() + 49 });
        $(this).children('span.cpanel_nadpis').css({ top: pozicia.top - 18, marginTop: 0 });
      }
    } else if (vyska < 147 && $(this).parent().not('.sidebar')) {
      var ciary = false,
          pozicia = $(this).position();

      l++;

      $(this).css({ height: $(this).children('div').height() + 49 });
      if (parseInt(l) == 3)
      {
        $(this).css({ height: $(this).children('div').height() + 49 });
        $(this).children('span.cpanel_nadpis').css({ top: pozicia.top - 18, marginTop: 0 });
      }
    } else {
      var ciary = false;
      $(this).css({ height: $(this).children('div').height() + 49 });
    }

    var vyska = $(this).height() - 46,
        obj = {
		      'class': 'btop',
		      css: {
            width: sirka
		      }
	      },
        obj2 = {
		      'class': 'bright',
		      css: {
            height: vyska + 2
		      }
	      },
        obj3 = {
		      'class': 'bleft',
		      css: {
            height: vyska + 1
		      }
	      },
        obj4 = {
		      'class': 'bbottom',
		      css: {
            width: sirka + 1
		      }
	      };

	  $('<span/>', obj).prependTo(this);
	  $('<span class="ctleft"></span>').prependTo(this);
	  $('<span class="ctright"></span>').prependTo(this);
	  $('<span/>', obj2).prependTo(this);
	  $('<span/>', obj3).prependTo(this);
	  $('<span class="cbleft"></span>').appendTo(this);
	  $('<span class="cbright"></span>').appendTo(this);
	  $('<span/>', obj4).appendTo(this);

	  if (ciary)
	  {
	    $(this).addClass('_small', 'ciary');
    } else {
	    $(this).addClass('_small');
    }
  });

  $('ul.submenu').each(function() {
    $(this).css('minWidth', $(this).parent().width());
  });

  $('li', 'ul.menu_data').hover(function() {
    if ($(this).children('ul.submenu').length > 0)
    {
      $(this).children('ul.submenu').not(':animated').slideDown();
    }
  }, function() {
    if ($(this).children('ul.submenu').length > 0)
    {
      $(this).children('ul.submenu').slideUp();
    }
  });

  $(window).resize(function() {
    spracuj_render();
  });

  $('ul.menu_data', 'div.menu').spasticNav();

  spracuj_render();

  $('div.page_loader').fadeOut(300);

  /*OSTATNE BIND FUNKCIE*/

  $('a').live('click', function()
  {
    var href = $(this).attr('href');

    if (!$(this).hasClass('nojq') && !$(this).hasClass('highslide') && !$(this).hasClass('highslide-full-expand'))
    {
      $('div.page_loader').fadeIn(300, function()
      {
        window.parent.location = href;
      });

      return false;
    }
  });

  $('form').live('submit', function()
  {
    if ($(this).attr('id') != 'mChatForm')
    {
      $('div.page_loader').fadeIn(300);
    }
  });
});

function spracuj_render() {
  var sirka_okna = $(window).width(),
      prebytocna_sirka = (sirka_okna - 954) / 2;

  $('div.render_left, div.render_right').hide();

  $('div.render_left').each(function() {
    var pozicia_left = $(this).width(),
        pozicia_left = pozicia_left - prebytocna_sirka,
        pozicia_left = pozicia_left - 2*pozicia_left - 20;

    $(this).css({ left: pozicia_left });
  });

  $('div.render_right').each(function() {
    var sirka = prebytocna_sirka,
        vyska = $(this).children().height(),
        pozicia_right = sirka_okna - prebytocna_sirka,
        src = $(this).children().attr('src');

    $(this).css({ left: pozicia_right, height: vyska, width: sirka, 'background': 'url('+src+') 20px top no-repeat' }).children().hide();
  });
}
