$(document).ready(function(){
  // Cleanup fields for search when page reload
  $("#searchfilterfield").val(search_string); 
  $("#top-search-text").val(top_search_string); 
  // Init png Fix script
  $(document).pngFix();
  // Arrangementliste click function
  $(".arrangementliste").click(function() {
      $.ajax({
        type: "POST",
        url: "/frontpage/showeventdetailed",
        data: ({
                eventid : $(this).attr('id')
              }),
        async: false,
        success: function (data, textStatus){
          if(textStatus == 'success'){
            $('#free_container').hide();  
            $('#bigtop_container').show();
            $('#small1_container').hide();
            $('#small2_container').hide();
            $('.box_news').hide();
            $('#bigtop_container').html(data);
            $('#bottom_container').show();
            $('div').removeClass('top-position-k');
            $('div').removeClass('top-position-subscribe');
            $('.subscribe').show();
            $('.newest-k').show();
            $("html").scrollTop(0);
          }
        }
      });
  });
  // Index page click in menu
  $(".index").click(function() {
      $("#main-container-for-all").load("/frontpage/returnmain");
  });
  // Eventlink click function
  $(".eventlink").live('click', function() {
      $.ajax({
        type: "POST",
        url: "/frontpage/showeventdetailed",
        data: ({
                eventid : $(this).attr('eventid')
              }),
        async: false,
        success: function (data, textStatus){
          if(textStatus == 'success'){
            $('#free_container').hide();
            $('#bigtop_container').show();
            $('#small1_container').hide();
            $('#small2_container').hide();
            $('.box_news').hide();
            $('#bigtop_container').html(data);
            $('div').removeClass('top-position-k');
            $('div').removeClass('top-position-subscribe');
          }
        }
      });
  });
  // Gallery click function
  $(".gallerylink").live("click", function() {
      $.ajax({
        type: "POST",
        url: "/gallery/index",
        data: ({
                eventid : $(this).attr('eventid')
              }),
        async: false,
        success: function (data, textStatus){
          if(textStatus == 'success'){
            //$('.main_events').html(data);
            $('#free_container').html(data);
            $('#free_container').show('slow');
            $('#small1_container').hide();
            $('#small2_container').hide();
            $('#bigtop_container').hide();
            $('#bottom_container').hide();
            $('.box_news').hide();
          }
        }
      });
  });

  $("#searchfilterfield").click(
    function () {
      $(this).val("");
    }).mouseout(function(){
    if (!$("#searchfilterfield").val()) {
      $("#searchfilterfield").val(search_string); 
    }
  });
  //
  $(".genre-button").hover(
    function () {
      $(this).css("background", "#00a7e4");
    },
    function () {
      if ($(this).hasClass('active')) {
       $(this).css("background","#00a7e4");
      } else {
       $(this).css("background", "#006D93");
      }
   });
  //
  $(".arrangementliste").hover(
    function () {
      $(this).css("background", "#009FE3");
    },
    function () {
      $(this).css("background","#0182AA")
   });
   // targetgroupo hover
   $(".target-button").hover(
    function () {
      $(this).css("background", "#009FE3");
    },
    function () {
      if ($(this).hasClass('active')) {
       $(this).css("background","#00a7e4");
      } else {
       $(this).css("background", "#006D93");
      }
   });
   $("div.gallerycap").bind("mouseenter",function(){
      $("div",this).css("background","#00a7e4");
      $(this).css("background","#00a7e4");
     }).bind("mouseleave",function(){
      $(this).css("background","#00c8e4");
     });

  // Months sorting function
  $(".date-button").live('click', function () {
     var searchtextfilter = $(this).attr("month");
     var A = Array();
     A['JM'] = ["01", "02", "03"]; 
     A['AJ'] = ["04", "05", "06"];
     A['JS'] = ["07", "08", "09"];
     A['OD'] = ["10", "11", "12"];
     $(".arrangementliste").hide();
     $(".arrangementliste").each(function () {
       var m = $(this).attr('months');
       for(var i in A[searchtextfilter]){
         if(A[searchtextfilter][i] == m){
           $(this).show();
         }
       }
     });
  });
  // DO DELETE!
  $("#searchfilterbutton_all").click(function () {
    $('.genre-button').css('background-color', '#006D93').removeClass('active');
    $('.target-button').css('background-color', '#006D93').removeClass('active');
    $(".arrangementliste").hide();
    $(".arrangementliste").each(function() {
      var pages = $(this).attr("plist");
      if (pages == 1) {
        $(this).show();
      }
    });
  });
  // target group button click
  $(".target-button").click(function () {
    //var searchtextfilter = $(this).val();
    $('.genre-button').css('background-color', '#006D93').removeClass('active');
    $('.target-button').css('background-color', '#006D93').removeClass('active');
    $(this).css('background-color', '#0182AA').addClass('active');
    $(".arrangementliste").hide();
    $(".arrangementliste:contains('"+$(this).attr('wa').toUpperCase()+"')").show();
  });
  // Genre cloud buttons function
  $(".genre-button").click(function () {
    var searchtextfilter = $(this).val();
    $('.target-button').css('background-color', '#006D93').removeClass('active');
    $('.genre-button').css('background-color', '#006D93').removeClass('active');
    $(this).css('background-color', '#0182AA').addClass('active');
    $(".arrangementliste").hide();
    $(".arrangementliste:contains('"+searchtextfilter.toUpperCase()+"')").show();
  });
  // Search field autocomplete
  $("#searchfilterfield").keyup(function () {
    var searchtext = $("#searchfilterfield").val();
    if (searchtext.length >= 2) {
      $(".arrangementliste").hide();
      $(".arrangementliste:contains('"+searchtext.toUpperCase()+"')").show();
    } else {
      $(".arrangementliste").hide();
      $(".arrangementliste").each(function() {
         var pages = $(this).attr("plist");
         if (pages == 1) {
            $(this).show();
         }
      });
      return false;
    }
  });
  // Search field filter
  $("#searchfilterbutton").click(function () {
    var searchtext = $("#searchfilterfield").val();
    if (searchtext.lenght < 2) {
      $("#searchfilterfield").text("value", "min 3 chars!");
      return false;
    } else {
    $(".arrangementliste").hide();
    $(".arrangementliste:contains('"+searchtext.toUpperCase()+"')").show();
    }
  });
  // Show next function;
    $("#linknext").click(function () {
        var maxpage = $(this).attr("amax");
        var targetpage = parseInt ($(this).attr("apage"))+1;
        if (targetpage > maxpage) {
           var targetpage = 1;
        }
        $(".arrangementliste").show();
        $(".arrangementliste").each(function () {
           var page = $(this).attr('plist'); 
           if(targetpage == page){
              $(this).show(); 
           }
        }); 
        $("#linknext").attr("apage", targetpage);
    });
    // Main menu link click function;
    $(".mainmenu").click(function() {
      //$(this).css('color','#ffffff');
      $.ajax({
        type: "POST",
        url: "/newsreader/page",
        data: ({
                page : $(this).attr('link')
              }),
        async: false,
        success: function (data, textStatus){
          if(textStatus == 'success'){
            $('#free_container').html(data);
            $('#bigtop_container').hide();
            $('#small1_container').hide();
            $('#small2_container').hide();
            $('#free_container').show();
            $('.box_news').show();
            $('.box_news').addClass('top-position-news');
            $('.newest-k').addClass('top-position-k');
            $('.subscribe').addClass('top-position-subscribe');
          }
        }
      });
  });
  // trick for enter use in top search field
  $("#top-search-text").keyup(function(e) {
    if (e.keyCode == 13){
      $('#top-search-button').click();
    }
  });
  // Top search click and proceed;
    $("#top-search-button").click(function() {
      var top_search_text = $('#top-search-text').val();
      if (top_search_text.length >= 5) {
      $.ajax({
        type: "POST",
        url: "/searchmodule/index",
        data: ({
                searchtext : $('#top-search-text').val()
              }),
        async: false,
        success: function (data, textStatus){
          if(textStatus == 'success'){
            $('#free_container').html(data);
            $('#bigtop_container').hide();
            $('#small1_container').hide();
            $('#small2_container').hide();
            $('#free_container').show();
            $('.box_news').hide();
            $('.newest-k').hide();
            $('.subscribe').hide();
            $('#bottom_container').hide();
            $('#free_container').highlight($('#top-search-text').val());
          }
        }
      });
      } else {
        $('#top-search-text').val('min 5 chars');
        return false;
      }
    });
    // All news click button;
    $(".allnews").live("click", function() {
      $.ajax({
        type: "POST",
        url: "/newsreader/index",
        data: ({
                fulllist : 'true'
              }),
        async: false,
        success: function (data, textStatus){
          if(textStatus == 'success'){
            $('#free_container').html(data);
            $('#bigtop_container').hide();
            $('#small1_container').hide();
            $('#small2_container').hide();
            $('#free_container').show();
            $('.box_news').hide();
            $('.newest-k').hide();
            $('.subscribe').hide();
            $('#bottom_container').hide();
          }
        }
      });
    }); // All news click button event end;

  $("#comp_email").click(
    function () {
      $(this).val("");
  }).mouseout(function(){
    if (!$("#comp_email").val()) {
      $("#comp_email").val(enter_email);
    }
  });
  $("#comp_name1").click(
    function () {
      $(this).val("");
  }).mouseout(function(){
    if (!$("#comp_name1").val()) {
      $("#comp_name1").val(enter_firstname);
    }
  });
  $("#comp_name2").click(
    function () {
      $(this).val("");
  }).mouseout(function(){
    if (!$("#comp_name2").val()) {
      $("#comp_name2").val(enter_lastname);
    }
  });

});

