var Messenger = {
  hide_error: null,
  hide_notice: null,
  error: function(message) {
    $('#flasherror').html(message);
    $('#flasherror').fadeIn(300);
    setTimeout(function(){
      $('#flasherror').fadeOut(300);
    }, 5000);
  },

  notice: function(message) {
    $('#flashnotice').html(message);
    $('#flashnotice').fadeIn(300);
    setTimeout(function(){
      $('#flashnotice').fadeOut(300);
    }, 5000);
  }
};
$(function(){
  $("body").append('<div id="flasherror"></div><div id="flashnotice"></div>');
});

var Utils = {
  dialog: function(title, text, options){
    var dialog = $("_dialog");
    if(dialog.length == 0){
      var dialog_window = document.createElement("div");
      dialog_window.id = "_dialog";
      $(document.body).append(dialog_window);
      dialog = $(dialog_window);
    }
    $(dialog_window).empty();
    var bd = document.createElement("div");
    $(bd).append(text);
    $(dialog_window).append(bd);
    options.title = title;
    options.bgiframe = true;
    if(!options.modal) options.modal = true;
    dialog.dialog(options);
  }
}

var Frontend = {
  fillPosition: function(url){
    $('#sport_id').change(function(e){
      $.ajax({
        type: "POST",
        url: url,
        data: "id=" + $('#sport_id').val(),
        success: function(msg){
          $('#position_id').replaceWith(msg);
          $('#position_id').trigger('onDataChange', {});
        }
      });
    });
  },

  selectEditWidget: function (el, url){
    this.init(el, url);
  },

  selectTableWidget: function (el, url){
    this.init(el, url);
  },

  workoutEditWidget: function (el, url){
    this.init(el, url);
  }
}

Frontend.selectEditWidget.prototype = {
  init: function(el, url){
    this.element = $('#' + el);
    this.element.data("data", "");
    this.btn_delete = $('#' + el + '_delete');
    this.btn_rename = $('#' + el + '_rename');
    this.btn_add = $('#' + el + '_add');
    this.text = $('#' + el + '_text');
    var self = this;

    var _getSelectedId = function(){
      var selected = $("option:selected", self.element);
      if(selected.length > 0){
        var id = selected[0].value;
      }else{
        var id = 0;
      }
      return id;
    }
    this.btn_add.click(function(e){
      var id = _getSelectedId();
      $.ajax({
        type: "POST", url: url, dataType: 'json',
        data: "value=" + self.text.val() + '&cmd=add&id='+id + "&" + self.element.data("data"),
        success: function(msg){
          self.text.val('');
          var option = document.createElement('option');
          option.value = msg.id;
          option.innerHTML = msg.title;
          var selected = $("option:selected", self.element);
          if(id > 0){
            $(selected[0]).after(option);
          }else{
            self.element.append(option);
          }
          self.element.trigger('onDataChange', {type: 'add'});
        }
      });
      return false;
    });

    this.btn_delete.click(function(e){
      var id = _getSelectedId();
      if(!id){
        Utils.dialog("Warning", "Please selest item to delete", {});
        return false;
      }
      Utils.dialog("Delete", "Are you sure you want to delete?", {
        buttons: {
          "Ok": function(){
            var dialog = $(this);
            $.ajax({
              type: "POST", url: url,
              data: 'cmd=delete&id='+id + "&" + self.element.data("data"),
              success: function(msg){
                dialog.dialog("close");
                var selected = $("option:selected", self.element);
                $(selected[0]).remove();
                self.element.trigger('onDataChange', {type: 'add'});
              }
            });
          },
          "Cancel": function(){$(this).dialog("close")}
        }
      });
      return false;
    });
    this.btn_rename.click(function(e){
      var id = _getSelectedId();
      if(!id){
        Utils.dialog("Warning", "Please selest item to rename", {});
        return false;
      }
      var selected = $("option:selected", self.element);
      $("#_rename_dialog").show();
      $("#_rename_dialog_text").val($(selected[0]).html());
      Utils.dialog("Rename", $("#_rename_dialog"), {
        buttons: {
          "Ok": function(){
            var dialog = $(this);
            $.ajax({
              type: "POST", url: url,
              data: "value=" + $("#_rename_dialog_text").val() + '&cmd=rename&id='+id + "&" + self.element.data("data"),
              success: function(msg){
                dialog.dialog("close");
                $(selected[0]).html($("#_rename_dialog_text").val());
                $("#_rename_dialog_text").val('');
                self.element.trigger('onDataChange', {type: 'add'});
              }
            });
          },
          "Cancel": function(){$(this).dialog("close")}
        }
      });
      return false;
    });
  }
}

Frontend.selectTableWidget.prototype = {
  init: function(el){
    var self = this;
    self.element = $('#' + el);
    self.element.data("_selected", 0);
    $("tr", self.element).live("click", function(e){
      $("tr", self.element).removeClass("_selected");
      $(this).addClass("_selected");
      self.element.data("_selected", $(this).attr("id").split("_")[1]);
    });
  }
}

Frontend.workoutEditWidget.prototype = {
  init: function(el, url){
    this.element = $('#' + el);
    var self = this;

    $('#' + el + ' div.droptitle a').click(function(e){
      $(this).blur();
      var img = $("img", $(this)).get(0);
      if(img.src.indexOf('plus.gif') > 0){
        img.src = img.src.replace('plus.gif', 'minus.gif');
      }else{
        img.src = img.src.replace('minus.gif', 'plus.gif');
      }
      $('#' + el + ' div.dropdown').toggle("slow");
      return false;
    });

    $('#' + el + ' a.add_exercise').click(function(e){
      $('#add_exercise_popup').css('top', $(window).scrollTop());
      $("#add_exercise_popup").data("section", self.element);
      $('#add_exercise_popup').show();
      return false;
    });
  }
}

$(document).ready(function(){
  $("div.mmenu2 ul li").each(function(i){
    var link = $("a", this);
    var img = $("img", link);
    $(this).removeClass("act");
    img.get(0).src = img.get(0).src.replace("_w.gif", ".gif");
    if(location.href.indexOf(link.get(0).href) == 0){
      $(this).addClass("act");
      img.get(0).src = img.get(0).src.replace(".gif", "_w.gif");
    }
  });

  var menus = {
    'content/tour1': 2, 'content/tour2': 2, 'content/tour3': 2, 'content/tour4': 2,
    'content/intelligentTraining': 3, 'content/ourSpecialists': 3, 'exerciseDemonstrations': 3,
    'content/features': 4, 'content/onlineTraining': 4, 'content/slowGameDown': 4, 'user/createTeam': 4,
    'user/questionnaire': 5, 'content/ourAthletes': 5, 'content/athleteComparison': 5,
    'content/about': 6, 'content/team': 6, 'content/pressRoom': 6, 'content/tos': 6, 'content/privacy': 6,
    'content/contact': 7,

    'coach/profile': 2,
    'coach/news': 3
  };
  for (index in menus){
    if(location.href.indexOf(index) != -1){
      $("div.mmenu ul li").each(function(i){
        var link = $("a", this);
        var img = $("img", link);
        $(this).removeClass("act");
        img.get(0).src = img.get(0).src.replace("_w.gif", ".gif");
      });

      var active = $("div.mmenu ul li:nth-child("+ menus[index] +")");
      active.addClass("act");
      var img = $("img", active);
      img.get(0).src = img.get(0).src.replace(".gif", "_w.gif");
      break;
    }
  }

  if($("ul.leftmenu2").hasClass("skip")) return;
  var selected = false;
  var black = $("ul.leftmenu2 li.black").length;
  $("ul.leftmenu2 > li").each(function(i){
    var link = $("a", this);
    if(!link.length){
       return;
    }
    $(this).removeClass("act");
    $(this).removeClass("black");
    if(!i) return;
    if(location.href.indexOf(link.get(0).href) == 0){
      $(this).addClass("act");
      if(black){
        $(this).addClass("black");
      }
      selected = true;
    }
  });
  if(!selected){
    $("ul.leftmenu2 li:first").addClass("act");
    if(black){
      $("ul.leftmenu2 li:first").addClass("black");
    }
  }
});

function simple_tooltip(target_items, name){
 $(target_items).each(function(i){
    $("body").append("<div class='"+name+"' id='"+name+i+"'><p>"+$(this).attr('title')+"</p></div>");
    var my_tooltip = $("#"+name+i);

    $(this).removeAttr("title").mouseover(function(){
        my_tooltip.css({opacity:0.9, display:"none"}).fadeIn(400);
    }).mousemove(function(kmouse){
        if(kmouse.pageX + my_tooltip.width() + 50 > $(window).width()){
          my_tooltip.css({left:kmouse.pageX-my_tooltip.width()-15, top:kmouse.pageY+15});
        }else{
          my_tooltip.css({left:kmouse.pageX+15, top:kmouse.pageY+15});
        }
    }).mouseout(function(){
        my_tooltip.fadeOut(400);
    });
  });
}


$(document).ready(function(){
   simple_tooltip("a.tip, th.tip","tooltip");
});