$(function() {
   drawCategories();
   $('.blur').each(function() {
      $(this).focus(function() {
         if ($(this).val() == $(this).attr('rel')) {
            $(this).val('');
         }
      });
      $(this).blur(function() {
         if ($(this).val() == '') {
            $(this).val($(this).attr('rel'));
         }
      });
      $(this).blur();
   });
   $('#open_rss').fancybox({
      modal: true,
      padding: 0,
      scrolling: 'no',
      autoDimensions: false,
      autoScale: false,
      width: 891,
      height: 520,
      ajax: {
         cache: false
      }
   });
   $('#img-twitter').click(function() {
      $(this).attr('href', 'post.php?engine=twitter&title=' + urlencode(document.title) + '&link=' + urlencode(location.href));
   });
   $('#img-fb').click(function() {
      $(this).attr('href', 'post.php?engine=facebook&title=' + urlencode(document.title) + '&link=' + urlencode(location.href));
   });
   $('#img-email').fancybox({
      modal: true,
      padding: 0,
      scrolling: 'no',
      autoDimensions: false,
      autoScale: false,
      width: 500,
      height: 580,
      ajax: {
         cache: false
      }
   });
   $('#query-form').submit(function() {
      query = $("#query").val();
      url = '';
      if ("" != query) {
         url = $('base').attr('href') + urlencode($(this).attr('rel')) + '/' + urlencode(query.substr(0, 1000)) + '.html';
      }
      location.href = url;
      return false;
   });
});

function itemPrevAction() {
   if (topCatsOffset > 0) {
      topCatsOffset--;
      drawCategories();
   }
}

function itemNextAction() {
   if (topCats.length > topCatsOffset + $('.item:visible', '#top-cats-container').size()) {
      topCatsOffset++;
      drawCategories();
   }
}

function drawCategories() {
   var useNav = false;
   var maxWidth = $('#topLine').width() - 380;
   $('#top-cats-container').empty();
   var allLink = $('<a class="show-form"/>').attr('href', 'form.php?cat=-1').html('Όλες οι κατηγορίες');
   var all = $('<span class="top-cats-item all" />').append(allLink);
   $('#top-cats-container').append(all);
   var prev = $('<span class="top-cats-item prev" />').html("&lt;&lt;");
   $('#top-cats-container').append(prev);
   $.each(topCats, function(i, elem) {
      var spanLink = $('<a class="show-form"/>').attr('href', 'form.php?cat=' + elem.id).html(elem.title);
      var span = $('<span class="top-cats-item item show-form" />').attr('rel', elem.id).css('color', elem.text).css('background-color', elem.color).html(spanLink);
      if (i < topCatsOffset) {
         span.hide();
         useNav = true;
      }
      $('#top-cats-container').append(span);
      if ($('#top-cats-container').width() > maxWidth) {
         span.remove();
         useNav = true;
         return false;
      }
   });
   var next = $('<span class="top-cats-item next" />').html("&gt;&gt;");
   $('#top-cats-container').append(next);
   if (!useNav) {
      prev.hide();
      next.hide();
   }
   // Events
   prev.click(function() {
      itemPrevAction();
   });
   next.click(function() {
      itemNextAction();
   });
   $('a.show-form').fancybox({
      modal: true,
      padding: 0,
      scrolling: 'no',
      autoDimensions: false,
      autoScale: false,
      width: 891,
      height: 550,
      ajax: {
         cache: false
      }
   });
}

function urlencode(str) {
   return encodeURIComponent(str.replace(/\//g, '%2F').replace(/\+/g, '%2B').replace(/&/g, '%26')).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A');
}

function drawMap() {
   $('.tip').remove();
   $('#newsDiv').html('<div id="loaderDiv"><div><img src="images/loader.gif" alt="" /></div></div>');
   url = $('base').attr('href') + 'node.php?_=' + Math.random();
   browser = '';
   if ($.browser.webkit || $.browser.safari) {
      browser = 'webkit';
   } else if ($.browser.opera) {
      browser = 'opera';
   } else if ($.browser.msie) {
      browser = 'msie';
   } else if ($.browser.mozilla) {
      browser = 'mozilla';
   }
   if (navigator.userAgent.match(/linux/i)) {
      browser += '_linux';
   }
   $.post(url, {type: type, page: page, cats: choosedCategories, cat: currentCategory, topic: currentTopic, width: $('#newsDiv').width(), height: $('#newsDiv').height(), browser: browser, query: query}, function(data) {
      if ('' != data) {
         $('#newsDiv').html(data);
         adjustBlocks();
         $('.inner-content').tooltip({speed: 150, delay: 300});
      } else {
         $('#newsDiv').html('<div id="loaderDiv"><div>Δεν υπάρχουν νέα...</div></div>');
      }
   });
}

function resizePage() {
   width = $('#newsDiv').width();
   height = $('#newsDiv').height();
   newsheight = $(window).height() - 154;
   adheight = $(window).width();
   elems = new Array(
      $('#newsDiv'),
      $('#altNews'),
      $('#altNewsCover')
   );
   elems[0].css('height', newsheight + 'px');
   elems[1].css('height', newsheight + 'px').hide();
   elems[2].hide();
   $('#adcontainer').css('top', (newsheight + 90) + 'px');
   $('#adcontainer').css('width', (adheight - 4) + 'px');
   $('#telia').css('left', (adheight / 2 - 40) + 'px');
   $('#adcontainer').show();
   if (width != $('#newsDiv').width() || height != $('#newsDiv').height()) {
      drawMap();
   }
}
function adjustBlocks() {
   $('.inner-content').each(function() {
      $this = $(this);
      blockWidth = $this.width();
      blockHeight = $this.height();
      width = $('span', $this).width();
      height = $('span', $this).height();
      size = parseInt($this.css('font-size'));
      while (size > 1 && (width > blockWidth || height > blockHeight)) {
         $this.css('font-size', (--size) + 'px');
         width = $('span', $this).width();
         height = $('span', $this).height();
      }
   });
}
function setCookie(name, value, expire) {
   var exdate = new Date();
   if (null != expire) {
      exdate.setDate(exdate.getDate() + expire);
   }
   document.cookie = name + "=" + escape(value) + (expire == null ? "" : ";expires="+exdate.toGMTString()+";path=/");
}
