/*
(c) GPK_UANIC 29.05.2009
*/

var koef;

function gpk_im_dc( ctx ){
  ctx.beginPath();
  ctx.moveTo(0,40);
  ctx.quadraticCurveTo(-1,-1,40,0);
  ctx.lineTo(0,0);
  ctx.moveTo(0,40);
  ctx.lineTo(0,0);
  ctx.fill();
}

jQuery.noConflict();
jQuery(document).ready(function($){
  koef = 30;

  $('img[src*="/uploads/posts/"]').each(function(){
    if( $.browser.msie )
      return;
    var the_id = Math.random().toString().substring(2);
    $(this).attr({'id':'im'+the_id});
    $(this).after('<canvas style="display:none;float:'+$(this).attr('align')+'" title="'+$(this).attr('title')+'" id="cv'+the_id+'"></canvas>');

    if( $(this).attr('offsetWidth') >= 100 ){

      var img = document.getElementById('im'+the_id);
      var canvas = document.getElementById('cv'+the_id);
      var ctx;

      canvas.setAttribute('height',$(this).attr('offsetHeight'));
      canvas.setAttribute('width',$(this).attr('offsetWidth'));

      if( !( ctx = canvas.getContext('2d') ) ){
        $('#cv'+the_id).remove();
        return;
      }

      ctx.drawImage( img, 0, 0, canvas.width, canvas.height, 0, 0, canvas.width, canvas.height-koef );
      ctx.fillStyle = 'rgb(0,0,0)';

      gpk_im_dc( ctx );
      ctx.scale(-1, 1);
      ctx.translate(-canvas.width, 0);
      gpk_im_dc( ctx );
      ctx.scale(1, -1);
      ctx.translate(0, koef-canvas.height);
      gpk_im_dc( ctx );
      ctx.scale(-1, 1);
      ctx.translate(-canvas.width, 0);
      gpk_im_dc( ctx );
      ctx.translate(0, -canvas.height);
      ctx.drawImage( canvas, 0, 0, canvas.width, canvas.height, 0, 1-canvas.height*.5+koef*1.5, canvas.width, canvas.height*1.5 );

      var grd = ctx.createLinearGradient(0,canvas.height-koef,0,canvas.height);
      grd.addColorStop(1,'rgba(0, 0, 0, .5)');
      grd.addColorStop(0,'rgba(0, 0, 0, 1)');

      ctx.fillStyle = grd;
      ctx.fillRect(0,0,canvas.width,canvas.height);





      $(this).remove();
      $('#cv'+the_id).show();
    }
    else
      $('#cv'+the_id).remove();
  });

  var theme_path = '/templates/gpkEastNetKtulhu';

  var cathegory = document.location.href.match(/ktulhu\.net\.ua\/(?:index\.php|([^\/]+)\/)?$/);
  if( !( cathegory ) )
    return;

  $.get(theme_path+'/random_news.tpl', {}, function(tpl){
    if( $.browser.msie )
      return;
    tpl = tpl.replace(/\{THEME\}/g, theme_path).replace(/\[\/[\w]+\-link\]/g, '</a>');
    $.post('/rand_news.php', {'cathegory':cathegory[1]}, function(data){
      var lines = data.split(/\|\|\|\|/);
      if( !( i = lines.length ) )
        return;
      while( i-- > 0 ){
        if( !( els = lines[i].split(/\|/) ) )
          continue;
        tpl = tpl.replace(/\{date\}/, els[0] )
        tpl = tpl.replace(/\{title\}/, '<b title="'+els[1]+'">'+els[1].substring(0,20)+'...</b>' )
        tpl = tpl.replace(/\{short\-story\}/, els[2] )
        tpl = tpl.replace(/\{category\}/, els[3] )
        tpl = tpl.replace(/\{image0\}/, els[5] )
        tpl = tpl.replace(/\{image\}/, els[5] )
        tpl = tpl.replace(/\[full\-link\]/, '<a href="http://ktulhu.net.ua/' + els[4] + '-.html">' )
      }
      $('#rand_news').html(tpl);
    });
  });
});










