﻿/***************************************************************************************************/
var gs_flag_click_event_source_id = "";
var gs_flag_current_static_popup_id = "";

/***************************************************************************************************/
var utl_pe = new utility_popup_engine();

function utility_popup_engine() {

}

utility_popup_engine.prototype.event_source = function(ao_event_source_id) {
    gs_flag_click_event_source_id = ao_event_source_id;
}

utility_popup_engine.prototype.open_bottom = function(as_control_id, as_popup_id) {
    var lo_control_item;
	var lo_popup_item;
	
	var li_popup_height, li_popup_width;
	var li_control_height, li_control_width;
	var li_window_height, li_window_width;
	var li_control_top, li_control_left;
	var li_popup_pos_top, li_popup_pos_left;

    lo_control_item = document.getElementById(as_control_id);
	lo_popup_item = document.getElementById(as_popup_id);
	
	//Open Popup
	lo_popup_item.style.visibility = "visible";
	lo_popup_item.style.display = "block";

	//Compute Popup Position
	li_popup_height = lo_popup_item.clientHeight;
	li_popup_width = lo_popup_item.clientWidth;

	li_control_height = lo_control_item.offsetHeight;
	li_control_width = lo_control_item.offsetWidth;

	li_window_height = document.body.clientHeight;
	li_window_width = document.body.clientWidth;

	li_control_top = ut_bt.position_top_find(lo_control_item);
	li_control_left = ut_bt.position_left_find(lo_control_item);


	li_popup_pos_top = li_control_top + li_control_height;
	if ( ( li_control_height + li_control_top + li_popup_height ) > li_window_height ) {
		li_popup_pos_top = li_control_top - li_popup_height;
	}
	
	li_popup_pos_left = li_control_left;
	if ( ( li_control_left + li_popup_width ) > li_window_width ) {
	    li_popup_pos_left = ( li_window_width - li_popup_width - 10 );
	}


	//li_popup_pos_top = li_control_top + li_control_height + 0; //0 pixels allowance
	//li_popup_pos_left = li_control_left;
	
	//if ( ( li_control_left + li_popup_width ) > li_window_width ) {
	//    li_popup_pos_left = ( li_window_width - li_popup_width - 10 ); //10 pixels allowance
	//}
	
	

	//Set Popup Width
	if ((li_popup_width + 10) < li_control_width) {
	    li_popup_width = li_control_width
	    lo_popup_item.style.width = (li_popup_width - 10) + "px";
    }
	
	//Set Popup Position
	lo_popup_item.style.top = li_popup_pos_top + "px";
	lo_popup_item.style.left = li_popup_pos_left + "px";

	return;
}

utility_popup_engine.prototype.close = function() {
    var lo_popup_item;

    if ( (gs_flag_current_static_popup_id) != "" && (gs_flag_click_event_source_id != gs_flag_current_static_popup_id) ) {
        lo_popup_item = document.getElementById(gs_flag_current_static_popup_id);

        lo_popup_item.style.visibility = "hidden";
        lo_popup_item.style.display = "none";   
    }
    
    gs_flag_current_static_popup_id = gs_flag_click_event_source_id;
    gs_flag_click_event_source_id = "";

    return;
}

utility_popup_engine.prototype.open_file = function(as_file_path) {
    window.open(as_file_path, 'SpecialFeature', 'scrollbars=no, toolbar=no, status=yes, resizable=yes, height=600, width=900, left=60, top=60;');
    
    return false;
}
/***************************************************************************************************/
