//<![CDATA[

//예산금액필드제어
    function budgetAmount(obj){
	//obj.value.replace(/[^0-9]/g, 0)
	obj.value = obj.value.numberFormat()
    }

	
	// top window 이동
	function redirect_to_top(url) {
		top.location.href = url;
	}
	
	// parent window 이동
	function redirect_to_parent(url) {
		parent.location.href = url;
	}
	
	// window 이동
	function redirect_to(url) {
		location.href = url;
	}
	
	// window 이동
	function reload(target) {
		if(target)
			target.location.reload();
		else
			location.reload();
	}
	
	// Ajax call
	function ajax_call(url, pars) {
		new Ajax.Request(url, {
			method: 'get',
			parameters: pars,
			asynchronous: true,
			evalScripts: true
		});
	}
	
	// 레이어 창 만들기(프로토타입 윈도우)
	function open_to_layer(layer_id, w, h, contents, close_callback, layout_name) {
		if(layout_name) var win = new Window(layer_id, {width: w, height: h, layout: layout_name} );
		else var win = new Window(layer_id, {width: w, height: h} );
		win.getContent().innerHTML = contents;
		win.setDestroyOnClose();
		win.showCenter();
		win.toFront();
		
		if(close_callback) {
			win.setCloseCallback(close_callback);
		}
	}
	
	// 레이어 창 만들기(프로토타입 윈도우) - ajax 호출
	function open_to_ajaxLayer(layer_id, w, h, ajax_url, close_callback) {
		var win = new Window(layer_id, {width: w, height: h} );
		win.setDestroyOnClose();
		win.setAjaxContent(ajax_url, {method: 'get', asynchronous: true, evalScripts: true}, true);
		if(close_callback) {
			win.setCloseCallback(close_callback);
		}
	}
	
	// 텍스트 필드 Info 텍스트 컨트롤
	function _set_info_text(input) {
		if(input.getAttribute('title') && (input.value.strip() == '' || input.value.strip() == input.getAttribute('title')) )
			input.value = input.getAttribute('title')
	}
	function _remove_info_text(input) {
		if(input.getAttribute('title') && (input.value.strip() == '' || input.value.strip() == input.getAttribute('title')) )
			input.value = ''
	}
	
	//달력 alias
	function wCalendar(event, obj, callback_func) {
		wcalendar.showCalendar({'cEvent' : event, 'inputName': obj, 'callback': callback_func} );
	}
	

//	document.observe("contextmenu", returnFalse.bindAsEventListener(this));
//	document.observe("selectstart", new Function ("return false"));
//	document.observe("dragstart", new Function ("return false"));
//	Event.observe(document.body, 'contextmenu', returnFalse.bindAsEventListener(this)); 
//	function returnFalse(e){
//	  e.returnValue = false;
//	  return false;
//	}
//  Event.observe(document.body, 'click', handler.bindAsEventListener(this)); 
//  document.onselectstart=new Function ("return false")
//  document.ondragstart=new Function ("return false")
//  document.oncontextmenu=new Function("return false");

var Application = {
  ajaxWin : null,
  id : "",
  popWindow : function(params){
    this.ajaxWin = new Window(this.id, params["win_options"]);
  },
  popWindowAjax : function(params){
    this.id = params["id"] || 'image_slide_preview';
    this.ajaxWin = new Window(this.id, params["win_options"]);
    this.ajaxWin.setAjaxContent(params["url"], params["ajax_options"], params["show_center"] || false, params["show_modal"] || false);
    this.ajaxWin.setLocation(document.viewport.getScrollOffsets()["top"] + 10, (document.viewport.getWidth() - $(this.id).getWidth()) / 2);
    return false;
  },
  closePop : function(layer_id){
    Windows.close(layer_id);
  },
  printPop : function(img_url){
    var printWin = window.open(img_url, 'map_print', 'top=0,left=0,width=100,height=100,menubars=no');
//    var printWin = window.open('/map.html', 'map_print', 'top='+screen.width+',left='+screen.height+',width=100,height=100,alwaysLowered=yes');
//    $('print_hidden').src = '/map.html';
//    $('print_hidden').src = '/map.html';
  },
  photoView : function(obj){
    this._photoView($(obj).getAttribute("image_id"));
  },
  photoViewById : function(id){
    this._photoView(id);
  },
  _photoView : function(id){
    new Ajax.Request("/wedding/common/photo/index", {
      asynchronous:true, 
      evalScripts:true, 
      parameters:{"image_id" : id},
      onComplete:function(args){
        eval("var tpl = " + args["responseText"].gsub("\r", "").gsub("\n","").gsub("\r\n", "") + ";");
        if(!$('photo_view_layer')) {
          var photoLayer = document.createElement('div');
          photoLayer.setAttribute('id', 'photo_view_layer');
          document.body.insertBefore(photoLayer, document.body.firstChild);
        }
        $('photo_view_layer').update(tpl.evaluate({"close_script":"Event.stopObserving('photo_view_layer', 'load');$('photo_view_layer').remove();"}));
        Event.observe($('photo_view_layer_img'), 'load', function(){
          $('photo_view_layer').setStyle(
            {
              top:(WindowUtilities.getWindowScroll()["top"]+(WindowUtilities.getPageSize()["windowHeight"] - ($('photo_view_layer_img').getHeight()))/2) - 15 + "px", 
              left:(WindowUtilities.getWindowScroll()["width"] - $('photo_view_layer_img').getWidth())/2 + "px", 
              width:($('photo_view_layer_img').getWidth())+"px",
              position:"absolute",
              display:"none",
              zIndex:999
            }
          );
//          Effect.Grow('photo_view_layer', {direction:"top-left", duration:0.8});  
          Effect.Grow('photo_view_layer', {duration:0.5});  
        });
      }
    });
  },
  toCenter : function(obj){
    $(obj).setStyle(
      {
        top:(WindowUtilities.getWindowScroll()["top"]+(WindowUtilities.getPageSize()["windowHeight"] - ($(obj).getHeight()))/2) - 15 + "px", 
        left:(WindowUtilities.getWindowScroll()["width"] - $(obj).getWidth())/2 + "px", 
        width:($(obj).getWidth())+"px",
        position:"absolute"
      }
    );
  }
};




//플로팅
//new Floating(적용할개체 , X축여백 , Y축여백 , 미끄러지는속도:작을수록빠름..기본20 , 빠르기:작을수록부드러움..기본10);
var Floating = Class.create({
	// 생성자(지도생성)
	initialize: function(FloatingObj,MarginTop,Percentage,setTime){
		this.FloatingObj = FloatingObj;
		this.MarginTop = (MarginTop) ? MarginTop : 120;
		this.Percentage = (Percentage) ? Percentage : 20;
		this.setTime = (setTime) ? setTime : 10;
		this.FloatingObj.style.position = "absolute";
		this.Body = null;
		this.setTimeOut = null;
		this.isRun = true;
		this.Run();
	},
	Run: function () {
		if ((document.documentElement.scrollLeft + document.documentElement.scrollTop) > (document.body.scrollLeft + document.body.scrollTop)) {
			this.Body = document.documentElement;
		} else {
			this.Body = document.body;
		}
	
		var This = this;
		var FloatingObjTop = (this.FloatingObj.style.top) ? parseInt(this.FloatingObj.style.top,10) : this.FloatingObj.offsetTop;
		var DocTop = this.Body.scrollTop + this.MarginTop;
	
		var MoveTop = Math.abs(FloatingObjTop - DocTop);
		MoveTop = Math.ceil(MoveTop / this.Percentage);
	
		if (FloatingObjTop < DocTop) {
			this.FloatingObj.style.top = FloatingObjTop + MoveTop + "px";
		} else {
			this.FloatingObj.style.top = FloatingObjTop - MoveTop + "px";
		}
	
		window.clearTimeout(this.setTimeOut);
		if(this.isRun) this.setTimeOut = window.setTimeout(function () { This.Run(); }, this.setTime);
		else {
			this.FloatingObj.style.top = this.MarginTop
		}
	},
	toggleScroll: function() {
		//끄기
		if(this.isRun) {
			this.isRun = false;
		}
		//켜기
		else {
			this.isRun = true;
			this.Run();
		}
	}});






// 일자관련 헬퍼
var DateHelper = {
	minutes: 1000 * 60,
	hours: 1000 * 60 * 60,
	days: 1000 * 60 * 60 * 24,
	years: 1000 * 60 * 60 * 24 * 365
};
DateHelper.parse = function(date_str) {
	try {
		return new Date(date_str.substr(0,4), parseInt(date_str.substr(5,2), 10)-1, date_str.substr(8,2));
	} catch(e) {
		alert('DateHelper.parse: ' + e.description);
		return;
	}
};
//기간구하기 - day
DateHelper.betweenDays = function(start_date_str, end_date_str) {
	return (this.parse(end_date_str) - this.parse(start_date_str)) / this.days;
};
//날짜구하기
//return: date_str
DateHelper.getDateByTerm = function(date_str, term_day_int) {
	var term_day = this.parse(date_str);
	term_day.setDate(term_day.getDate() + term_day_int)
	
	return term_day.getFullYear() + '.' + (term_day.getMonth() + 1).toPaddedString(2)  + '.' + term_day.getDate().toPaddedString(2);
};


// 레이어(디자인) 셀렉트박스 관련
var LayerSelect = {
	toggleList: function(dom_id) {
		$(dom_id+'_list').toggle();
	},
	select: function(dom_id, select_obj) {
		var sel = $(select_obj);
		$(dom_id + '_prompt').innerHTML = sel.innerHTML;
		$(dom_id + '_field').value = sel.getAttribute('value');
		this.toggleList(dom_id);
		
		$(dom_id + '_field').onchange();
	}
};



//텍스트 클립보드로 복사!
function CopyClip(contents, comment, options){
	var strOut = "<div style='text-align: center; padding: 15px;'><p style='line-height: 150%;'>" + comment + "</p><br/>";
	if (window.clipboardData) {
		// the IE-manier
		window.clipboardData.setData("Text", contents);
		strOut += contents;
		
	}
	else {
		//alert("아래 내용을 복사해서 사용하세요!\n\n" + text);
		strOut += "<textarea style='width:100%; height: 60px;margin-bottom: 5px;'>" + contents + "</textarea>"
		strOut += "※위 내용을 복사해서 사용하세요!"
	}
	strOut += "</div> <div style='text-align:center; width: 100%'><a href='#' onclick='Windows.close(\"clipboard_copy\", event); return false;'><img src='/images/common/b_layerok.gif' alt='확인'></a></div>"
	open_to_layer("clipboard_copy", options.width, options.height, strOut);
}

//즐겨찾기 추가
function BookmarkSite(title,url) {
  if (Prototype.Browser.Gecko){ // firefox
    window.sidebar.addPanel(title, url, "");
  }else if(Prototype.Browser.Opera){ // opera
    var elem = document.createElement('a');
    elem.setAttribute('href',url);
    elem.setAttribute('title',title);
    elem.setAttribute('rel','sidebar');
    elem.click();
  }else if(Prototype.Browser.IE){ // ie
    window.external.AddFavorite(url, title);
  }
}

//]]>
