﻿
// --------------------------------------
// Function Name  : fn_go_child_a_link
// Author         : neowindwiz
// Create Date    : 2009-08-11
// Description    : div tag 등 배경으로 image를 사용하며
//                  click event로 a tag와 같은 동작이 필요한 경우
//                  child로 a tag를 추가하고 child a tag 속성을 사용한다.
// --------------------------------------
function fn_go_child_a_link(obj) {
  var kids = $(obj).children("a:first");
  if (null != kids && kids.length > 0) {
    $(kids[0]).each(function() {
      var href = $(this).attr("href");
      var target = $(this).attr("target");

      if (null == href && "" == href)
        return;

      if (null != target && "" != target) {
        open_wnd = window.open(href, target, '', '');
        if (null != open_wnd)
          open_wnd.focus();
      }
      else {
        location.href = href;
      }

    });
  }
}

function fn_valid_text_length(obj, limit, is_cut, is_focus, msg_limit) {
  var text = $(obj).val();
  var is_limit = false;

  if (null != text && "" != text && text.length > limit) { 
    is_limit = true;
  }

  if (true == is_limit) {
    if (true == is_limit && null != msg_limit && "" != msg_limit) {
      alert(msg_limit);
      
      if (true == is_cut) {
        $(obj).val(text.substring(0, limit));
      }
    }

    if (true == is_focus) {
      $(obj).focus();
    }
  }

  if (null == text || "" == text)
    return 0;
  else
    return text.length;
}

function fn_valid_text_char(e, key, msg) {
  if (e.keyCode == key) {
    if (undefined != msg)
      alert(msg);
    return false;
  }
  return true;
}

function fn_enabled_contrl(id, bgcolor, add_function) {
  var jqID = "#" + id;

  if (undefined == bgcolor)
    var bgcolor = "";

  if (null != bgcolor && "" != bgcolor)
    $(jqID).css("background-color", bgcolor).removeAttr("disabled");
  else
    $(jqID).css("background-color", "#ffffff").removeAttr("disabled");
}

function fn_disabled_control(id, bgcolor, add_function) {
  var jqID = "#" + id;

  if (undefined == bgcolor);
  var bgcolor = "#ece9d8";

  if (null == bgcolor || "" == bgcolor)
    bgcolor == "#ece9d8";

  $(jqID).css("background-color", bgcolor).attr("disabled", "disabled");
}

function fn_show_control(id, add_function) {
  var jqID = "#" + id;
  $(jqID).show();
}

function fn_hide_control(id, add_function) {
  var jqID = "#" + id;
  $(jqID).hide();
}

function fn_get_valid_value(value, chgValue)
{
  if(undefined == value || null == value || "" == value)
    return chgValue;
  return value;
}

function fn_get_value(id) {
  var jqID = "#" + id;
  try {
    if ($(jqID).size() <= 0)
      throw "'" + id + "' is not exist.";
    return $(jqID).val();
  }
  catch (ex) {
    neoDebug.print(ex);
  }
}

// Debug class 구현

function neoDebugClass() {
  this.isDebug = false;

  neoDebugClass.prototype.start = function() {
  ///	<summary>
  ///	1 :	neoDebug 시작
  ///	</summary>

    if (this.isDebug == false) {
      this.isDebug = true;
      $("body").prepend("<div><input type='button' value='log초기화' onclick='$(\"#neoDebug\").empty();'/></div><div id=\"neoDebug\"></div>");
    }
  }

  neoDebugClass.prototype.end = function() {
    this.isDebug = false;
    $("#neoDebug").remove();
  }

  neoDebugClass.prototype.print = function(msg, type) { // 1 : 모두출력, 2 : 문자만, 3 : Object만
    if (undefined == type)
      var type = 1;

    if (true == this.isDebug) {
      if (undefined != msg.description && (1 == type || 3 == type))
        neoDebug.print("error -- " + arguments.callee.caller + " <span style=\"margin-left:10px;color:red\">" + msg.description + "</span>");
      else if(1 == type || 2 == type)
        $("#neoDebug").append(msg + " &nbsp;");
    }
  }

  neoDebugClass.prototype.printLine = function(msg, type) {
    this.print(msg, type);
    $("#neoDebug").append("<br/>");
  }

  neoDebugClass.prototype.clear = function() {
    $("#neoDebug").empty();
  }
}

var neoDebug = new neoDebugClass();

// UI class 구현
function neoUI() {
  this._welModalLayer = null;

  neoUI.prototype.lightbox = function(selector, baseStyle, lightStyle) {
    var v_selector, v_baseStyle, v_lightStyle;

    v_selector = fn_get_valid_value(selector, "input:text");
    v_baseStyle = fn_get_valid_value(baseStyle, { "border": "1px solid rgb(204, 204, 204)", "margin": "1px" });
    v_lightStyle = fn_get_valid_value(lightStyle, { "border": "2px solid rgb(68, 139, 219)", "margin": "0px" });

    $(v_selector).each(function(i) {
      $(this).css(v_baseStyle);
      $(this).focus(function() {
        $(this).css(v_lightStyle);
      });
      $(this).blur(function() {
        $(this).css(v_baseStyle);
      });
    });
  }

  neoUI.prototype.showModalLayer = function(bgcolor) {
    if (undefined == bgcolor)
      var bgcolor = "#FFFFFF";

    if (null == this._welModalLayer) {
      this._welModalLayer = document.createElement("div");
      $(this._welModalLayer).css({ zIndex: 9999999, position: "absolute", top: "0px", left: "0px", background: bgcolor, opacity: 0 });
      $(this._welModalLayer).appendTo(document.body);


      //       $Document().scrollSize(); this._welEventBlocker.css({ width: b.width + "px", height: b.height + "px" }); this._welEventBlocker.show()
    }

    var b = xWindowSize(window);
    alert(b);
    //    $(this._welModalLayer).css({ width: b.width() + "px", height: b.height() + "px" });
    //    $(this._welModalLayer).show();
    //    showEventBlocker:function(a){if(this._welEventBlocker==null){this._welEventBlocker=$Element("<DIV>");this._welEventBlocker.css({zIndex:9999999,position:"absolute",top:"0px",left:"0px",background:"#FFFFFF",opacity: 0});this._welEventBlocker.appendTo(document.body);this._welEventBlocker.opacity(0);$Fn(function(d){d.stop()}).attach(this._welEventBlocker,"mousedown")}var b=$Document().scrollSize();this._welEventBlocker.css({width:b.width+"px",height:b.height+"px"});this._welEventBlocker.show()},hideEventBlocker:function(){if(this._welEventBlocker){this._welEventBlocker.hide()}}
  }

  function xWindowSize(f) {
    if (!f) { f = window }
    var d = 0;
    if (f.innerWidth) {
      d = f.innerWidth;
      if (typeof (f.innerWidth) == "number") {
        var g = getScrollBarWidth();
        d = f.innerWidth - g
      }
    } else {
      if (document.documentElement && document.documentElement.clientWidth) {
        d = document.documentElement.clientWidth
      } else {
        if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
          d = document.body.clientWidth
        }
      }
    } return d
  }

  function getScrollBarWidth() { var g = document.createElement("p"); g.style.width = "200px"; g.style.height = "200px"; var f = document.createElement("div"); f.style.position = "absolute"; f.style.top = "0px"; f.style.left = "0px"; f.style.visibility = "hidden"; f.style.width = "200px"; f.style.height = "150px"; f.style.overflow = "hidden"; f.appendChild(g); document.body.appendChild(f); var j = g.offsetWidth; f.style.overflow = "scroll"; var h = g.offsetWidth; if (j == h) { h = f.clientWidth } document.body.removeChild(f); return (j - h) }
}

// validation class 구현
function validator(isFocus) {
  this.arrCondition = [];
  this.isFocus = isFocus;

  validator.prototype.add = function(selector, msg, type, value1, value2) {
    var index = this.arrCondition.length;
    var isfocus = this.isFocus;

    this.arrCondition[index] = function() { return fn_chk_base(type, selector, msg, isfocus, value1, value2); }
  }

  validator.prototype.valid = function() {
    var index = this.arrCondition.length;
    for (var idx = 0; idx < index; idx++) {
      if (false == this.arrCondition[idx]()) {
        return false;
      }
    }

    return true;
  }

  function fn_chk_base(type, selector, msg, isfocus, value1, value2) {
    try {
      if ("exist" == type)
        return fn_chk_exists(selector, msg);

      fn_chk_exists(selector);

      if ("empty" == type)
        return fn_chk_empty_value(selector, msg);
      else if ("attr" == type)
        return fn_chk_attribute_equal(selector, msg, value1, value2); // original : value1, target : value2
      else if ("size" == type)
        return fn_chk_size_value(selector, msg, value1, value2);  // min : value1, max : value2
      else if ("re" == type)
        return fn_chk_regExp(selector, msg, value1, value2);
    }
    catch (ex) {
      neoDebug.print(ex, 3);
      alert(ex);
    }

    fn_onfocus(selector, isfocus);
    return false;
  }

  function fn_chk_regExp(selector, msg, regExp, need) {
    var re = eval(regExp);
    var val = $(selector).val();

    if (null == val || (val == '' && true == need))
      throw msg;
    if (false == re.test(val))
      throw msg;
    return true;
  }

  function fn_chk_size_value(selector, msg, min, max) {
    if (undefined != min && undefined != max && (min > max))
      throw "error : 조건 max값이 min값보다 작습니다.";

    if (min > 0)
      fn_chk_empty_value(selector, msg);

    var value = $(selector).val();
    var size = value.length;

    if (min > size)
      throw msg + " [현재:" + size + "]";
    if (undefined != max && max < size)
      throw msg + " [현재:" + size + "]";

    return true;
  }

  function fn_chk_empty_value(selector, msg) {
    var val = $(selector).val();

    if (null == val || "" == val)
      throw msg;

    return true;
  }

  function fn_chk_attribute_equal(selector, msg, name, value) {
    var isEqual = false;
    $(selector).each(function() {
      if (value == $(this).attr(name)) {
        isEqual = true;
      }
    });

    if (false == isEqual)
      throw msg;
    return true;
  }

  function fn_chk_style_equal(selector, name, value) {
    var val = $(selector).css(name);
    if (val != value)
      throw msg;
    return true;
  }

  function fn_onfocus(selector, isfocus) {
    if (true == isfocus) {
      $(selector).eq(0).focus();
    }
  }

  function fn_chk_exists(selecter, msg) {
    if ($(selecter).size() <= 0) {

      if (undefined == msg || null == msg || "" == msg)
        var msg = "'" + selecter + "' is not exists";

      throw msg;
    }

    return true;
  }
}

// arrList Class 구현
function neoList() {
  this.keys = [];
  this.values = [];
  this.count = 0;

  neoList.prototype.add = function(key, value) {
    try {
      if (0 <= fn_find_array_exist(this.keys, key)) {
        throw "'" + key + "' is aleady added";
      }
      else
      { 
        this.keys[this.count] = key;
        this.values[this.count] = value;
        this.count++;
      }
    }
    catch (ex) {
      neoDebug.print(ex);
    }
  }

  neoList.prototype.remove = function(key) {
    try {
      if (0 <= fn_find_array_exist(this.keys, key)) {
        var key_tmp = [];
        var value_tmp = [];
        var index = 0;
        for (var idx = 0; idx < this.count; idx++) {
          if (this.keys[idx] != key) {
            key_tmp[index] = this.keys[idx];
            value_tmp[index] = this.values[idx];
            index++;
          }
        }

        this.keys = key_tmp;
        this.values = value_tmp;
        this.count = index;
      }
    }
    catch (ex) {
      neoDebug.print(ex);
    }
  }

  neoList.prototype.toJSON = function() {
    try {
      var strJSON = this.toSerialize();

      neoDebug.print(strJSON);
      return eval("(" + strJSON + ")");
    }
    catch (ex) {
      neoDebug.print(ex);
    }
  }

  neoList.prototype.toSerialize = function() {
    try {
      if (0 >= this.count)
        throw "error : list is empty";

      var strJSON = "{";
      for (var idx = 0; idx < this.count; idx++) {
        strJSON += (strJSON != "{" && strJSON.length > 0) ? "," : "";
        strJSON += "'" + this.keys[idx];
        strJSON += "':'" + (typeof(this.values[idx]) == "string" ? this.values[idx].replace("\n", "") : this.values[idx]) + "'";
      }
      strJSON += "}";

      return strJSON;
    }
    catch (ex) {
      neoDebug.print(ex);
    }
  }
}

function fn_find_array_exist(arr, key) {
  try {
    var length = arr.length;
    for (var idx = 0; idx < length; idx++) {
      if (arr[idx] == key)
        return idx;
      return -1;
    }
  }
  catch (ex) {
    neoDebug.print(ex);
  }
}

function fn_img_resize(selector, max_value, type) {
  if (undefined == type)
    var type = "width";

  if (undefined == max_value)
    var max_value = 250;

  var imgWidth = $(selector).attr("width");
  var imgHeight = $(selector).attr("height");

  if ("width" == type && imgWidth > max_value) {
    var ratio = max_value / imgWidth;
    var newHeight = Math.round(imgHeight * ratio);
    $(selector).attr({ "width": max_value, "height": newHeight });

    return true;
  }
  else if ("height" == type && imgHeight > max_value) {
    var ratio = max_value / imgHeight;
    var newWidth = Math.round(imgWidth * ratio);
    $(selector).attr({ "width": newWidth, "height": max_value });

  return true;  
  }

  return false;
}

/* return 값이 '상태|메세지'로 넘어오는 경우 사용
   ex) 'Y|성공'
*/ 
function fn_chk_result(result, msg, necessary) {
  var arrResult = result.split('|');
  if (null == arrResult || arrResult.length < 2) {
    var v_msg = eval("msg.E");
    if (undefined != v_msg.msg && "" != v_msg.msg)
      alert(v_msg.msg);
    else  
      alert("error : 잘못된 결과값 입니다.");
    return false;
  }
  else {
    var result_msg = arrResult[1];
    var v_msg = eval("msg." + arrResult[0]);

    if (undefined == v_msg) {
      alert("error : 잘못된 결과값 입니다.");
      return false;
    }
    else {
      if ("" != v_msg.msg) {
        if (true == necessary) {
          alert(v_msg.msg);
        }
        else {
          if (undefined != result_msg && "" != result_msg)
            alert(result_msg);
          else
            alert(v_msg.msg);
        }

        if (undefined != v_msg.value)
          return v_msg.value;
        return false;
      }
      else {
        alert(result_msg);
        return false;
      }
    }
  }
}

function fn_show_tab(str_json, num) {
  try {
    var obj_json = eval(str_json);
    if (0 == obj_json.selected_index) {
      obj_json.selected_index = num;
    }
    else if (obj_json.selected_index == num)
      return;
    obj_json.selected_index = num;

    $(obj_json.tabs).each(function(i, obj) {
      i++; neoDebug.print(i);
      if (i == num) {
        if (undefined != obj.img_id) {
          $(obj.img_id).each(function(i, img_id) { obj_json.select_img(img_id); });
        }
        if (undefined != obj.div_id) {
          $(obj.div_id).each(function(i, div_id) { $("#" + div_id).show(); });
        }
      }
      else {
        if (undefined != obj.img_id) {
          $(obj.img_id).each(function(i, img_id) { obj_json.selected_img(img_id); });
        }
        if (undefined != obj.div_id) {
          $(obj.div_id).each(function(i, div_id) { $("#" + div_id).hide(); });
        }
      }
    });
  }
  catch (ex) {
  }
}

/*
 * description : div로 구현된 slide를 direction에 따라 변경한다.
*/
function fn_move_slide(group_name, direction) {
  var count = $("div[name='" + group_name + "']").size();
  var view_count = $("div[name='" + group_name + "']:visible").attr("id").replace(group_name + "_", "");

  $("#" + group_name + "_" + view_count).hide();
  if ("pre" == direction) {
    if (1 == view_count) {
      $("#" + group_name + "_" + count).show();
    }
    else {
      $("#" + group_name + "_" + (parseInt(view_count) - 1)).show();
    }
  }
  else if ("next" == direction) {
    if (view_count == count) {
      $("#" + group_name + "_" + "1").show();
    }
    else {
      $("#" + group_name + "_" + (parseInt(view_count) + 1)).show();
    }
  }
}

function fn_printFlashObject(selector, url, name, width, height) {
  var jq_selector = $(selector);
  if (jq_selector.size() < 1)
  {
    alert('Error : 1234');
    return false;
  }

  var id = "";
  var name = "";
  if (undefined != jq_selector.attr("id")) {
    id = 'id="flash_' + jq_selector.attr("id") + '"';
    name = 'name="flash_' + jq_selector.attr("id") + '"';
  }
  
  var strOut = '';
  if (navigator.appName.indexOf("Microsoft Internet") != -1) {
    strOut += '<object ' + id + ' width="' + width + '" height="' + height + '" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" type="application/x-shockwave-flash">';
    strOut += '<param name="allowScriptAccess" value="always" />';
    strOut += '<param name="movie" value="' + url + '" />';
    strOut += '<param name="quality" value="high" />';
    strOut += '<param name="bgcolor" value="#ffffff" />';
    strOut += '<param name="wmode" value="transparent" />';
    strOut += '<param name="swliveconnect" value="true" />';
    strOut += '</object>';
  }
  else {
    strOut += '<embed ' + name + ' src="' + url + '" quality="high" ';
    strOut += 'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" ';
    strOut += 'classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ';
    strOut += 'allowScriptAccess="always" quality="high" wmode="transparent" type="application/x-shockwave-flash"';
    strOut += ' width="' + width + '" height="' + height + '"/>';
  }

  jq_selector.empty();
  jq_selector.append(strOut);
}

function fn_init_selectbox(selector, d_name) {
  if (undefined == d_name)
    var d_name = $(selector + " option").eq(0).text();

  $(selector).each(function() {
    $(this).empty();
    $(this).append("<option value=''>" + d_name + "</option>");
  });
}

function fn_set_selectbox(selector, list, selectedvalue) {
  $(selector).each(function() {
    var item_count = list.length;
    if (null == item_count || undefined == item_count)
      return;
      
    for (var idx = 0; idx < item_count; idx++) {
      var item = list[idx];
      var selected = (undefined != selectedvalue && item.value == selectedvalue) ? " selected" : ""
      $(this).append("<option value='" + item.value + "'" + selected + ">" + item.name + "</option>");
    }
  });
}

function fn_change_selectbox(selector, key, data) {
  $(selector).each(function() {
    var item_count = data.list.length;
    if (null == item_count || undefined == item_count)
      return;

    for (var idx = 0; idx < item_count; idx++) {
      var item = data.list[idx];
      if (item.key == key)
        fn_set_selectbox(selector, item.list);
    }
  });
}

function fn_select_selecbox(selector, selectedvalue) {
  if ("number" == typeof (selectedvalue)) {
    $(selector + " option").each(function(i) {
      if (i == selectedvalue)
        $(this).attr("selected", true);
      else
        $(this).attr("selected", false);
    });
  }
  else if ("string" == typeof (selectedvalue)) {
    $(selector + " option").each(function(i) {
      if ($(this).val() == selectedvalue)
        $(this).attr("selected", true);
      else
        $(this).attr("selected", false);
    });
  }
}

// parameter : 키 이벤트와 함수 리터럴
function fn_check_enter_key(e, handler) {
  var charCode

  if (e && e.which) {
    charCode = e.which
  }
  else {
    e = event;
    charCode = e.keyCode;
  }

  if (charCode == 13) {
    handler();
  } 
}

/*
  -------------------------------------
  스터디매니아 메인 기본 높이 설정 함수s
  -------------------------------------
*/
function fn_default_height(i_height) {
  try {
    if (undefined == i_height)
      return;

    $("div.Sub_Contents_Right").each(function() {
      var quick_h = $("#divStayTopLeft").height();
      if (quick_h > i_height) {
        $(this).height(quick_h);
      }
      else
        $(this).height('');

//      alert("fn : " + quick_h + ' | ' + i_height);
    });
  }
  catch (ex)
    { }
}

function fn_default_height_v2() {
  $(document).ready(function() {
    var i_height = $("div.Sub_Contents_Right").height();
    fn_default_height(i_height);
  });
}

function fn_default_height_obj(obj) {
  try {
    var top = $("div.Sub_Contents_Right").offset().top;
    var height = $(obj).height() + $(obj).offset().top - top;

//    alert("height : " + $(obj).height() + ", height2 : " + height + ", offset.top : " + $(obj).offset().top + ", top : " + top);
    fn_default_height(height);
  }
  catch (e)
  { }
}