$(document).ready(function(){
    $(".tombol, #dialog_link, ul#icons li, input").hover(
            function() {$(this).addClass('ui-state-hover');},
            function() {$(this).removeClass('ui-state-hover');}
    );
    $("#clientLogin").click(function(){
        $.ajax({
            type: "GET",
            url: BASE_URL+'home/frm_login',
            beforeSend: function(){
              loading('#msgBox');
            },
            success:function(data){
              remLoading();
              $("#msgBox").html(data);
            }
        });
        $("#msgBox").attr('title','Login Form');
        $("#msgBox").dialog({
            autoRun: false,
            modal: true,
            width: '400',
            buttons:{
                "Login":function(){
                    $.ajax({
                        type: "POST",
                        url: BASE_URL+'home/do_login',
                        data: $(".frmLogin").serialize(),
                        beforeSend:function(){
                            loading(".frmLogin");
                        },
                        success:function(data){
                            remLoading();
                            if($.trim(data)=='Success'){
                                location.href = BASE_URL+'member/dashboard';
                            }else{
                                warning(".frmLogin",data);
                            }
                        }
                    });
                },
                "Cancel":function(){
                    $("#msgBox").dialog("close");
                }
            }
        })
        $("#msgBox").dialog("open");
    });
    $("#logout").click(function(){
      $("#msgBox").attr('title','?')
      .html("Are you sure want to logout ?")
      .dialog({
        autoRun: false,
        modal: true,
        buttons:{
          "OK":function(){
            $.ajax({
              type: "GET",
              url: BASE_URL+"logout",
              success:function(data){
                location.href= BASE_URL;
              }
            })
          },
          'Cancel':function(){
            $("#msgBox").dialog("close");
          }
        }
      })
      .dialog("open");
    });
})
function additional(target,data){
  $(target).before(data);
}
function remAdditional(){
  $(".divAdditional").hide("slow")
                     .remove();
  $(".divInformation").hide("slow")
                      .remove();
  $("input:[name='imgFile']").remove();
}
function loading(target){
  $(target).before("<span id='ajaxBusy'>"+"<img src='"+BASE_URL+"asset/images/tpl/ajax-loader.gif'>"+"</span>");
  return true;
}
function remLoading(){
  $("#ajaxBusy").remove();
}
function loadingInner(target){
  $(target).before("<span id='ajaxBusy'>"+"<img src='"+BASE_URL+"asset/images/tpl/ajax-loader.gif'>"+"</span>")
           .hide("slow");
  return true;
}
function remLoadingInner(target){
  $("#ajaxBusy").remove();
  $(target).show("slow");
}
function autoHide(target){
    setTimeout(function(){$(target).hide("slow")}, 8000);
}
function resetForm(target){
    $(target)[0].reset();
}
function warning(target,warning){
  var divWarning = '\n\
              <div class="ui-state-error ui-corner-all divWarning dividing_10">\n\
              <span class="ui-icon ui-icon-alert" style="float: left;"></span>\n\
              <span id="txtWarning"></span>\n\
              </div>\n\
              \n\
  ';
  if(!$(".divWarning").size()){
    $(target).before(divWarning);
  }
  $("#txtWarning").html('<strong>Alert: </strong>'+warning);
  $(".divWarning").show("slow");
  setTimeout(function(){
    $(".divWarning").hide("slow");
    setTimeout(function(){
      $(".divWarning").remove()
    },3000);
  },
  8000)
}
function info(target,warning){
  var divWarning = '\n\
              <div class="ui-state-highlight ui-corner-all divWarning dividing_10">\n\
              <span class="ui-icon ui-icon-alert" style="float: left;"></span>\n\
              <span id="txtWarning"></span>\n\
              </div>\n\
              \n\
  ';
  if(!$(".divWarning").size()){
    $(target).before(divWarning);
  }
  $("#txtWarning").html('<strong>Alert: </strong>'+warning);
  $(".divWarning").show("slow");
  setTimeout(function(){
    $(".divWarning").hide("slow");
    setTimeout(function(){
      $(".divWarning").remove()
    },3000);
  },
  8000)
}
function success(target,warning){
  var divWarning = '\n\
              <div class="ui-state-success ui-corner-all divWarning dividing_10">\n\
              <span class="ui-icon ui-icon-alert" style="float: left;"></span>\n\
              <span id="txtWarning"></span>\n\
              </div>\n\
              \n\
  ';
  if(!$(".divWarning").size()){
    $(target).before(divWarning);
  }
  $("#txtWarning").html('<strong>Alert: </strong>'+warning);
  $(".divWarning").show("slow");
  setTimeout(function(){
    $(".divWarning").hide("slow");
    setTimeout(function(){
      $(".divWarning").remove()
    },3000);
  },
  8000)
}
function setTinymceContent(id){
  var content = tinyMCE.get(id).getContent();
  $('#'+id).val(content);
}
function switchLang(id,url){
  $.ajax({
    type: "POST",
    url: BASE_URL+'language/set',
    data: "id="+id+"&url="+url,
    beforeSend: function(){
      loading("#switchLang");
    },
    success: function(){
      location.href = BASE_URL+url;
    }
  });
}
function hideMain(){
  $(".divflexiGrid").hide("slow");
}
function showMain(){
  remAdditional();
  $(".divflexiGrid").show("slow");
}
function closeMsgBox(){
  $("#msgBox").dialog("close")
              .attr('title','')
              .html("");
}
function reloadGrid(){
  $(".pReload").trigger('click');
}
function breadcrumb(urlx){
  $.ajax({
      type: "GET",
      url: urlx,
      beforeSend: function(){
        loading('#bcrumb');
      },
      success: function(data){
        remLoading();
        if($.trim(data) != ''){
          $('#bcrumb').hide()
                      .html(data)
                      .show();
        }
      }
  });
}
function checkAll(name,target){
//  alert($(name).attr('checked'));
  if($(name).attr('checked') == false){
    $("input[id='"+target+"']").attr('checked',false);
  }else{
    $("input[id='"+target+"']").attr('checked',true);
  }
}
function checkAllByName(name,target){
//  alert($(name).attr('checked'));
  if($(name).attr('checked') == false){
    $("input[name='"+target+"']").attr('checked',false);
  }else{
    $("input[name='"+target+"']").attr('checked',true);
  }
}
function recovery(){
  $.ajax({
      type: "GET",
      url: BASE_URL+'home/frm_recovery',
      beforeSend: function(){
        loading("#msgBox");
      },
      success: function(data){
        remLoading();
        closeMsgBox();
        $("#msgBox").attr('title','Recovery Password')
                    .html(data)
                    .dialog({
                      autoRun: true,
                      modal: true,
                      buttons: {
                        "Recovery": function(){
                          $.ajax({
                            type: "POST",
                            url: BASE_URL+"home/do_recovery",
                            data: $(".frmRecovery").serialize(),
                            beforeSend: function(){
                              loading("#msgBox");
                            },
                            success: function(data){
                              remLoading();
                              if($.trim(data) == 'Success'){
                                closeMsgBox();
                                $("#msgBox").attr('title','Success')
                                            .html('To validate your password recovery request, please check your email and follow the instuction.')
                                            .dialog({
                                              autoRun: true,
                                              modal: true,
                                              buttons: {
                                                "OK": function(){
                                                  closeMsgBox();
                                                }
                                              }
                                            })
                              }else{
                                warning(".frmRecovery",data);
                              }
                            }
                          })
                        },
                        "Cancel": function(){
                          closeMsgBox();
                        }
                      }
                    });
      }
  });
}
