﻿$.fn.ImageRollOver = function(options){
  settings = $.extend({
    mouseover : '_over.gif',
    mouseout : '.gif'
  }, options);

  this.hover(function(){
    if(this.src.indexOf(settings.mouseover) == -1)
    this.src = this.src.replace(settings.mouseout, settings.mouseover);
  }, function(){
    if(!$(this).hasClass('notRollChange')){
      if(this.src.indexOf(settings.mouseover) != -1)
        this.src = this.src.replace(settings.mouseover, settings.mouseout);
    }
  });
  return this;
};

// 화면 최 상단으로 스크롤을 이동합니다.
function GoTop(){
  $(window).scrollTop(0);
}

// DropDownList 컨트롤에 날짜정보를 추가합니다.
function insertCalendar(target, startNumber, endNumber){
  while(target.options.length > 0)
    target.options[target.options.length - 1] = null;

  for(var i = startNumber, cnt = 0; i <= endNumber; i++, cnt++)
    target.options[target.options.length] = new Option(i, i);
}

// DropDownList의 년도가 변경되었을 경우 날짜 정보를 다시 셋팅합니다.
function changeYear(yearObjID, monthObjID, dayObjID){
  var objYear = $('select#' + yearObjID).get(0);
  var objMonth = $('select#' + monthObjID).get(0);
  var today = new Date();

  insertCalendar(objMonth, 1, 12);    
  changeMonth(yearObjID, monthObjID, dayObjID);
}

// DropDownList의 월이 변경되었을 경우 일자 정보를 다시 셋팅합니다. (윤년)
function changeMonth(yearObjID, monthObjID, dayObjID){
  var objYear = $('select#' + yearObjID).get(0);
  var objMonth = $('select#' + monthObjID).get(0);
  var objDay = $('select#' + dayObjID).get(0);
  var today = new Date();

  var selectDay = new Date(objYear.value, objMonth.value - 1, 1);

  insertCalendar(objDay, 1, selectDay.getLastDate());
}

// 마우스 오버시 해당 셀의 Row에 배경색 효과를 부여하고 링크 커서로 변경합니다.
function SetMouseOverEffectTableRowLink(o){
  SetMouseOverEffectTableRow(o);
  $(o).css({
    'cursor': 'pointer'
  });
}

// 마우스 아웃시 해당 셀의 Row에 배경색 효과를 제거하고 링크커서를 제거합니다.
function SetMouseOutEffectTableRowLink(o){
  SetMouseOutEffectTableRow(o);
  $(o).css({
    'cursor': ''
  });
}

// 마우스 오버시 해당 셀의 Row에 배경색 효과를 부여합니다.
function SetMouseOverEffectTableRow(o){
  $(o).css({
    'background-color': '#333'
  });
}

// 마우스 아웃시 해당 셀의 Row에 배경색 효과를 제거합니다.
function SetMouseOutEffectTableRow(o){
  $(o).css({
    'background-color': ''
  });
}

// 이미지 로드 실패시 대체할 이미지를 설정합니다.
function OnLoadErrorImage(){
  $('img[_onerrorsrc]').each(function() {
    var instance = $(this);
    var errorImage = instance.attr('_onerrorsrc');
    
    if(!$.trim(errorImage).isNullorEmpty()){
      var img = new Image();
      $(img).error(function(){
        instance.attr('src', errorImage);
      });
      img.src = instance.attr('src');
    }
  });
}

// 관리자가 사용하는 웹 에디터를 렌더링 합니다.
function DoRender_Administrator(id, uploadCategory, uploadPath){
  $('textarea#' + id).tinymce({
    // 사용할 플러그인 목록
    plugins : "table,inlinepopups,noneditable,sdenternet_advimage,sdenternet_advflash",
    // 사용할 테마
    theme : 'advanced',
    // tinyMCE 코어 스크립트 위치
    script_url : '/Common/Js/editor/tiny_mce.js',

    // 렌더링할 툴바 버튼 목록
    theme_advanced_buttons1 : "fontsizeselect,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,forecolor,backcolor,|,outdent,indent,|,bullist,numlist",
    theme_advanced_buttons2 : "tablecontrols,|,hr,removeformat,visualaid,|,link,unlink,|,image,media",
    theme_advanced_buttons3 : "",
    theme_advanced_buttons4 : "",

    // 툴바 렌더링 위치
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",

    // 상태바 렌더링 위치
    theme_advanced_statusbar_location : "none",

    // 에디터 크기 조절 가능 여부
    theme_advanced_resizing : false,
    theme_advanced_source_editor_wrap : false,
    forced_root_block : false,
    force_br_newlines : true,
    force_p_newlines : false,
    nowrap : true,

    setup : function(ed) {
      ed.uploadCategory = uploadCategory;
      ed.uploadPath = uploadPath;
    }
  });
}

// 일반 회원이 사용할 웹 에디터를 렌더링 합니다.
function DoRender_Normal(id, uploadCategory, uploadPath){
$('textarea#' + id).tinymce({
    // 사용할 플러그인 목록
    plugins : "noneditable,inlinepopups,sdenternet_advimage,sdenternet_advflash",
    // 사용할 테마
    theme : 'advanced',
    // tinyMCE 코어 스크립트 위치
    script_url : '/Common/Js/editor/tiny_mce.js',

    // 렌더링할 툴바 버튼 목록
    theme_advanced_buttons1 : "mybutton,fontsizeselect,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,link,unlink,|,image,media",
    theme_advanced_buttons2 : "",
    theme_advanced_buttons3 : "",
    theme_advanced_buttons4 : "",

    // 툴바 렌더링 위치
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",

    // 상태바 렌더링 위치
    theme_advanced_statusbar_location : "none",

    // 에디터 크기 조절 가능 여부
    theme_advanced_resizing : false,
    theme_advanced_source_editor_wrap : false,
    forced_root_block : false,
    force_br_newlines : true,
    force_p_newlines : false,
    nowrap : true,

    setup : function(ed) {
      ed.uploadCategory = uploadCategory;
      ed.uploadPath = uploadPath;
    }
  });
}

$(function() {
  $('img.rollOverEffect').ImageRollOver({mouseover: '_over.gif', mouseout: '.gif'});
  
  $('.viewContents *, .viewContentsPop *').css('white-space', '');
  
  $('.viewContents img, .viewContents object, .viewContents embed').each(function(){
    var obj = $(this);
    var position = null;
    var objWidth = parseInt(obj.width());
    var objHeight = parseInt(obj.height());
    var maxWidth = parseInt(parseInt(obj.parent().width(), 10) / 2, 10);
    var maxHeight = parseInt(Math.round(objHeight * (maxWidth / objWidth)), 10);

    if(objWidth > 600) {
      obj.width(maxWidth).height(maxHeight);

      if(obj.attr('src')) {
        obj.wrap('<a class="imagebox" href="' + obj.attr('src') + '" title="' + obj.attr('alt') + '"/>');

        position = obj.position();
        $('<div class="imagebox_detailIcon"></div>').prependTo(obj.parent())
            .width(maxWidth)
            .height(maxHeight)
            .css({ 'top': position.top, 'left': position.left });
      }
    }
  });
  
  $('.viewContentsPop img').each(function(){
    var obj = $(this);
    var position = null;
    var objWidth = parseInt(obj.width());
    var objHeight = parseInt(obj.height());
    var maxWidth = parseInt(parseInt(obj.parent().width(), 10), 10);
    var maxHeight = parseInt(Math.round(objHeight * (maxWidth / objWidth)), 10);
    
    if(objWidth > maxWidth)
      obj.width(maxWidth).height(maxHeight);
  });

  if($('.imagebox').size() != 0){
    $('.imagebox').fancybox({
      padding: 1,
      overlayColor: '#000',
      overlayOpacity: 0.6,
      transitionIn: 'elastic',
      transitionOut: 'elastic',
      overlayShow: true
    });
  }
});
