var query = '';
$(document).ready(function() {
  var options = {'path' : '/'};
  if($("#fontchanger").length > 0) {
    if ( $.cookie('font_size', options)!==null ) {
      $("body").attr("class",  $.cookie('font_size', options));
    }
    $("#fontchanger a").click(function() {
      var size = this.className;
      var prefix = "font_size_";
      $("body").attr("class", prefix+size);
      $.cookie('font_size', prefix+size, options)
    });
  }
  
  $('input#query').focus(function() {
    query = $(this).val();
    $(this).val('');
  });

  $('input#query').blur(function() {
    if ($(this).val() == '')
      $(this).val(query);
  });


  $('input#newsletter').click(function() {
    val = '&name=' + $('input#newsletter_name').val();
    val += '&email=' + $('input#newsletter_email').val();
    val += '&action=' + $('input#newsletter_action').val();
    val += '&captcha=' + $('input#newsletter_captcha').val();
    resp = $.ajax({
      url: "ajax.php?type=newsletter" + val,
      async: false
    }).responseText;

    if(resp == 'saved') {
      document.getElementById('confirm').style.display='none';
      document.getElementById('message_added').style.display='block';
      if($("#newsletterurl").attr("href")) {
        window.location = $("#newsletterurl").attr("href");
      }
    }else if(resp == 'removed') {
      document.getElementById('confirm').style.display='none';
      document.getElementById('message_removed').style.display='block';
    }else {
      document.getElementById('newsletter_error').innerHTML = resp
      document.getElementById('newsletter_error').style.display='block';
      document.getElementById('nameandemail').style.display='block';
      document.getElementById('confirm').style.display='none';
      img = document.getElementById('captchaimg')
      img.src = img.src + '?' + (new Date()).getTime();
      $('input#newsletter_captcha').val('');

    }


  })

  var generatstyle = $('link[href="style/index.css"]');

  $("a.print").click(function() {
    $(generatstyle).attr({
      href : "style/print.css"
    });
    setTimeout(function(){
      window.print()
      }, 100);
    $("#print_dialog").show();

    $("div#print_dialog a.back").click(function() {
      $("#print_dialog").hide();
      $(generatstyle).attr({
        href : "style/index.css"
      });
      return false;
    })

    $("div#print_dialog a.cancel").click(function() {
      $("#print_dialog").hide();
      return false;
    })

    return false;

  });

  $('div.more a.cat_view, div.more a.cat_overview').click(function(){
    if ($(this).hasClass('cat_open')) {
      $(this).removeClass('cat_open');
      $(this).addClass('cat_close');
      $(this).parent().find('ul.hidden_links').attr('class', 'hidden_links_show');

    }else {

      $(this).removeClass('cat_close');
      $(this).addClass('cat_open');
      $(this).parent().find('ul.hidden_links_show').attr('class', 'hidden_links');

    }

    return false;

  });


  $('.frm input').focus(function() {
    $(this).parent().parent().parent().children('.note').slideDown('normal');
    return false;
  });
  $('.frm input').blur(function() {
    $(this).parent().parent().parent().children('.note').slideUp('normal');
    return false;
  });
  $('.frm textarea').focus(function() {
    $(this).parent().parent().parent().children('.note').slideDown('normal');
    return false;
  });
  $('.frm textarea').blur(function() {
    $(this).parent().parent().parent().children('.note').slideUp('normal');
    return false;
  });
  
  var basehref = location.href;
  if(basehref.indexOf("#") != -1) {
    basehref = basehref.substr(0,basehref.indexOf("#"));
  }
  $("a").each(function() {
    var url = $(this).attr("href");
    if(url) {
      if(url.indexOf("#") == 0) {
        $(this).attr("href",basehref+url);
      }
    }
  });
  
  var fileTypes = {
        doc: 'rtf',
        docx: 'rtf',
        rtf: 'rtf',
        xls: 'xls',
        xlsx: 'xls',
        xlsm: 'xls',
        pdf: 'pdf',
        odt: 'odt',
        odf: 'odt',
        sxw: 'sxw',
        uot: 'uot',
        ods: 'oocalc',
        sxc: 'sxc',
        dif: 'dif',
        dbf: 'dbf'
    };

    $('a.checkType').each(function() {

        var $a = $(this);
        var href = $a.attr('href');
        var linkClass;

        if (href.match(/^http/) && !href.match(document.domain))  {
            linkClass = 'link';
        } else {
            var hrefArray = href.split('.');
            var extension = hrefArray[hrefArray.length - 1];
            linkClass = fileTypes[extension];
            
            if(!linkClass) {
                linkClass = 'link-inner';
            } else {
                service = 'json.php?type=filesize'; 
                var el = $(this);
                $.getJSON(service, {
                    file: $(this).attr('href')
                    }, function(j) {
                    if (j.result == true && j.size) {
                        el.html(el.html() + '<span class="size"> (' + j.size + ')</span>');
                    }                       
                });
            }
        }

        if (linkClass) { 
            $a.addClass(linkClass);
        }

    });
});





