// JavaScript Document
function check_all(parent_id){var f = $$('#'+parent_id+' input');for(var i=0; i<f.length; i++){if(f[i].visible()){f[i].checked = 'checked';}}}
function uncheck_all(parent_id){var f = $$('#'+parent_id+' input');for(var i=0; i<f.length; i++){if(f[i].visible()){f[i].checked = '';}}}
//cancel_w
var o_exit_ids = new Hash();
function confirm_exit() {
	if(!o_exit_ids.toQueryString().blank())
	{
		 return 'There is a window still open.';
	}
}

window.onbeforeunload=confirm_exit;

	function make_sortable(p_host_container_id,p_update_function,p_tag,p_params)
	{
		if(p_tag == undefined) p_tag = 'div';//onDrag:alert('onDrag')
		prep_sortable(p_host_container_id,p_tag);
		if(p_params)
		{
			if(p_params.constraint==true) b_constraint=true
		}
		else
		{
			b_constraint = false	
		}
			
		Sortable.create(p_host_container_id,{handle:'drag_handle',hoverclass:'dragging',scroll: window,onUpdate:eval(p_update_function),tag:p_tag,constraint:b_constraint,only:Array('draggable2','draggable','draggable_element')});
	}
	
	function prep_sortable(p_host_container_id,p_tag)
	{
		//Add drag handle
		$A($$('#'+p_host_container_id+' '+p_tag +'.draggable_element')).each(
			function(p_draggable_element)
			{
				$(p_draggable_element).observe('mouseout', function(event){
						$(p_draggable_element).down('.drag_handle').hide();
				 });
				$(p_draggable_element).observe('mouseover', function(event){
						$(p_draggable_element).down('.drag_handle').show();
				 });
				if($(p_draggable_element).down('.drag_handle_target'))
				{
					$(p_draggable_element).down('.drag_handle_target').update('<div title="Click and drag." class="drag_handle" style="position:absolute;display:none;" onclick="Event.stop(event);"></div>');
				}
			});
	}

	
winName = '';
function module_ajax()
	{
		/**
		* These variables are set via .prototype. in cms_module.php
		*/
		var module_name;
		var page_id;
		var instance_name;
		
		this.inspect = function()
			{
				alert('MODULE NAME\t:'+this.module_name + '\n\nPAGE ID\t:'+this.page_id + '\n\nADMIN URL\t:'+this.majax_admin_url);
			}
		this.open_popup = function(w_params,params)
			{
				w_params=$H(w_params).toObject();
				p = $H(params).toObject();
				p.m = this.module_name;
				p.page_id = this.page_id;
				if(w_params.window_id == undefined) w_params.window_id = 'w_m_window';
				if(w_params.scroll == undefined) w_params.scroll = 'yes';
				p.window_id = w_params.window_id;
				p.form_id = p.window_id + '_form';
				p.page_id = this.page_id;
				p.instance_name = this.instance_name;
				//(URL, winName, width, height, scroll,modal)
				if(popwin != null) popwin.close();
				//alert(winName);
				var winLeft = (screen.width - w_params.width) / 2;
				var winTop 	= (screen.height - w_params.height) / 2;
														
				winData = 'height='+w_params.height+',width='+w_params.width+',top='+winTop+',left='+winLeft+',scrollbars='+w_params.scroll+',resizable,modal='+w_params.modal+',status=false';
				URL = this.majax_admin_url + 'module.pop.php?' + $H(p).toQueryString();
				popwin = window.open(URL, winName, winData);
				popwin.focus(); 
			}
		this.close_popup = function(w_params)
			{
				if(popwin != null) popwin.close();
			}
		this.open_w = function(w_params,params)
			{
				
				w_params=$H(w_params).toObject();
				p = $H(params).toObject();
				p.page_id = this.page_id;
				if(w_params.window_id == undefined) w_params.window_id = 'w_majax_window';
				p.window_id = w_params.window_id; //So the window content can access this property.
				//Add object to window closing notice
				if(w_params.b_protect===true) o_exit_ids.set(p.window_id,'');
				p.page_id = this.page_id; //Maintain state with php
				p.instance_name = this.instance_name;
				pa_GET = $H(p).toQueryString();
				w = new ajax_window(p.window_id);
				w.create(w_params);//({title:w_params.title,width:w_params.width});
				majax(this.module_name,w_params.mf,w_params.window_id + '_content',pa_GET);
			}
		this.close_w = function(w_params,params)
			{
				w_p = $H(w_params).toObject();
				h_POST = $H(params).toObject();
				h_POST = $H(h_POST).merge($(w_p.window_id + '_form').serialize(true)).toObject();
				//Remove object from window closing notice
				o_exit_ids.unset(w_p.window_id);
				h_POST.m = this.module_name;
				h_POST.page_id = this.page_id; //Maintain state with php
				h_POST.instance_name = this.instance_name;
				//Set the height so it doesn't resize when doing AJAX update
				var current_height = $(w_p.target_id).getStyle('height');
				$(w_p.target_id).setStyle({height: current_height});
				
				Element.remove(w_p.window_id);
				majax_update({target_id:w_p.target_id,b_clear_height:true},h_POST);
			}
		this.cancel_w = function(w_params)
			{
				w_p = $H(w_params).toObject();
				//Remove object from window closing notice
				o_exit_ids.unset(w_p.window_id);
				Element.remove(w_p.window_id);
			}
		this.update = function(params,p_post)
			{
				p = $H(params).toObject();
				h_POST = $H(p_post).toObject();
				h_POST.mf = p.mf;
				h_POST.m = this.module_name;
				h_POST.page_id = this.page_id; //Maintain state with php
				h_POST.instance_name = this.instance_name;
				p.b_clear_height = p.b_clear_height==undefined ? true:p.b_clear_height;
				p.b_save_height = p.b_save_height==undefined ? true:p.b_save_height;
				if(p.b_save_height)
				{
					var current_height = $(p.target_id).getStyle('height');
					$(p.target_id).setStyle({height: current_height});
				}
				majax_update(p,h_POST);//{target_id:p.target_id,b_clear_height:true}
			}
	}

	function ajax_window (p_window_id)
	{
		this.window_id = p_window_id;
		this.content_id= this.window_id + '_content';
		this.title_id  = this.window_id + '_title';
		this.form_id = this.window_id + '_form';
		this.common_buttons_id = this.window_id + '_common_buttons';
		this.dragger_id= this.window_id + '_dragger';
	
		this.create = function(ph_params)
		{
			/**
			* Create an AJAX capable window.
			*/
			var window_id = this.window_id;
			var dragger_id = this.dragger_id;
			if($(this.window_id)) $(this.window_id).remove();
					
			var win = document.createElement('div');
			Element.extend(win);
			document.body.appendChild(win);
			
			win.addClassName('window');
			if(ph_params.w_body_class) win.addClassName(ph_params.w_body_class);
			
			if(ph_params.width == undefined) ph_params.width = '500px';
			if(ph_params.title_class == undefined) ph_params.title_class = '';
			
			win.setStyle({width: ph_params.width,position: 'absolute'});
			body_style = ph_params.height != undefined ? 'min-height:'+ph_params.height+'"':'';
			win.id = this.window_id;
			
		/**
		* Insert window into document.
		*/
			//window_modal_bg = '<div class="w_modal_bg"></div>';
			window_contents = '<div class="w_body" style="'+body_style+'"><div class="w_content"><form id="'+this.form_id+'"><div id="'+this.content_id+'" >...</div></form><div class="clear"></div></div><div class="clear"></div>';
			max_button = '<div class="ico_w_maximize dim right clickable_ico"  onclick="w_maximize(\''+this.window_id+'\');" ></div>';
			restore_button = '<div style="display:none;" class="ico_w_restore dim right clickable_ico"  onclick="w_restore(\''+this.window_id+'\',\''+ph_params.width +'\');"></div>';
			close_button = '<div class="ico_w_close dim right clickable_ico" onclick="Element.remove(\''+this.window_id+'\');o_exit_ids.unset(\''+this.window_id+'\');" ></div>';
			if(ph_params.b_close_button == false) close_button= '';
			window_head = '<div id="'+this.dragger_id+'" class="w_head">'+close_button+max_button+restore_button+'<div class="w_title left '+ph_params.title_class+'" id="'+this.title_id+'">'+ph_params.title+'</div><div style="clear:both"></div></div>';
			//window_tools
			win.update(window_head+window_contents); 
		/**
		* Make window Draggable & Center
		*/
			this.center();
			focus_form_default(this.form_id);//Focus first form element
			observe_enter_key(this.form_id);//Enter key to submit window
			if(ph_params.b_no_center_onload != false) center_onload(this.window_id); //Center window
			new Draggable(this.window_id,{handle:this.dragger_id,starteffect:'',endeffect:''});
			return win;
		}
		
		this.center 		 = function(){new Position.Center($(this.window_id));}
		this.set_title 	 = function(title) 	 {$(this.title_id).update(title);}
		this.set_content = function(content) {$(this.content_id).update(content);}
		
	}
	function w_maximize(p_window_id)
	{
		$(p_window_id).setStyle({position:'fixed',top:'10px',left:'10px',right:'10px',bottom:'10px',width:'auto'});
		var w_head_height=$(p_window_id).down('.w_head').getHeight() + 10;

		$(p_window_id).down('.w_body').setStyle({zIndex:'auto',position:'fixed',top:w_head_height+'px',left:'10px',right:'10px',bottom:'10px',width:'auto'});
		$(p_window_id).down('.ico_w_maximize').hide();
		$(p_window_id).down('.ico_w_restore').show();
		//alert($(p_window_id).down('.w_content').getStyle('height'));
	}
	
	function w_restore(p_window_id,p_width)
	{
		$(p_window_id).setStyle({width:p_width,position:'absolute',top:'',left:'',right:'',bottom:''});
		$(p_window_id).down('.w_body').setStyle({position:'',top:'',left:'',right:'',bottom:'',width:'auto'});
		$(p_window_id).down('.ico_w_maximize').show();
		$(p_window_id).down('.ico_w_restore').hide();
		new Position.Center($(p_window_id));
		
	}
	
	function display_loading(target_id)
		{
			new Element.update(target_id, '<div id="ajax_status_display" class="ajax_loading" style="">Loading...</div>');
		}
	
function w_open(window_id,parent_window_id,b_no_center)
	{		
		
		show(window_id);
		drag_id = window_id + "_dragger";
		if($(drag_id)) new Draggable(window_id,{handle:drag_id,starteffect:'',endeffect:''});
		Position.absolutize(window_id);
		
		if(b_no_center!==false)
		{
			var center_handler = 
			{
			onCreate: function(){
						Position.Center($(window_id),parent_window_id);
			},
			onComplete: function() {
					if(Ajax.activeRequestCount == 0){
						Position.Center($(window_id));
						Ajax.Responders.register(center_handler);
					}
				}
			};
			Ajax.Responders.register(center_handler);		
		}
	}
	
function create_ajax_window(ph_params)
	{
		// Create an AJAX capable window.
		
		window_id = ph_params.window_id;
		if($(window_id)) $(window_id).remove();
				
		var my_win = document.createElement('div');
		Element.extend(my_win);
		document.body.appendChild(my_win);
		
		my_win.addClassName('window');
		if(ph_params.width == undefined) ph_params.width = '500px';
		my_win.setStyle({width: ph_params.width,position: 'absolute'});
		my_win.id = window_id;

// insert it in the document
		my_win.update('<div class="wbody"><div id="'+window_id+'_dragger" class="whead"><div class="ico-cancel right"><input type="button" class="button"  onclick="Element.remove(\''+window_id+'\');" value="close" /></div><span id="'+window_id+'_title">'+ph_params.title+'</span></div><div id="'+window_id+'_content" class="wcontent">...</div></div><div class="clear"></div>');
		// Make window Draggable
		new Draggable(window_id,{handle:window_id+'_dragger',starteffect:'',endeffect:''});
		//Center window
		new Position.Center($(window_id));
		
		center_onload(window_id);
		return my_win;
}
function observe_enter_key(p_form_id)
{
		// If the enter key is pressed, submit the window
		var enter_key_handler = 
			{			
				onComplete: function() {
					if(Ajax.activeRequestCount == 0){
						Event.observe(p_form_id, 'keypress', 
													function(event){ 
																		var element = Event.element(event);
																		if (element.tagName != 'INPUT') return;
																		if(event.keyCode == Event.KEY_RETURN)
																			{
																				//Activate enter key press object
																				//alert();
																				
																				$(p_form_id).down('input.on_enter_keypress').click();
																				Event.stop(event);
																			}
																		});
						Ajax.Responders.unregister(enter_key_handler);
					}
				}
			};
			Ajax.Responders.register(enter_key_handler);		
}
	function form_observe_key(p_form_id)
	{
		Event.observe(p_form_id, 'keypress', 
										function(event){
												
															var element = Event.element(event);
															if (element.tagName != 'INPUT') return;
															if(event.keyCode == Event.KEY_RETURN)
																{
																	//Activate enter key press object
																	$(p_form_id).down('input.on_enter_keypress').click();
																	Event.stop(event);
																}
															});
	}
function focus_form_default(p_form_id)
{
		// Center an element after AJAX has completed...
		var focus_handler = 
			{			
				onComplete: function() {
					if(Ajax.activeRequestCount == 0){
						$(p_form_id).down('.focus').activate();
						Ajax.Responders.unregister(focus_handler);
					}
				}
			};
			Ajax.Responders.register(focus_handler);		
}


function center_onload(element_id,parent_window_id)
	{
		// Center an element after AJAX has completed...
		var center_handler = 
			{
			onCreate: function(){
						Position.Center($(element_id),parent_window_id);
			},				
				onComplete: function() {
					if(Ajax.activeRequestCount == 0){
						Position.Center($(element_id));
						Ajax.Responders.register(center_handler);
					}
				}
			};
			Ajax.Responders.register(center_handler);		
	}
	
function myAlert()
	{
		alert('test myAlert');
	}
			/*
			MAJAX
		m,mf,target_id,options
		
		*m:module name
		*mf:module function
		*id:id of html element,
		*options:GET style params
		*/
	function majax(m,mf,target_id,options)
	{	
		display_loading(target_id);
		//Module AJAX  Action
		if(majax_admin_url == null) alert('No target set for module.');
		var url = majax_admin_url + 'module.ajax.php';
		var url_public = majax_admin_url + 'module.ajax.public.php';
		var url_member = majax_admin_url + 'module.ajax.member.php';
		
		var pars = 'm='+escape(m);
		pars += '&mf='+escape(mf);
		if(options != undefined && options != "")
			{
				a_params = options.split('&');
				paramcount = a_params.length;
				r_params="";
				for(i=0;i<=paramcount-1;i++)
					{
						a_curr_param_set = a_params[i].split('=');
						r_params += "&" + escape(a_curr_param_set[0]) + "=" + escape(a_curr_param_set[1]);
					}
				pars += r_params;
			}
		//alert('target_id:'+target_id+'\nUrl'+url+'\nUrl'+pars);
		//Set focus to first input element in window
		if(mf.indexOf("public_") === 0) url = url_public;
		if(mf.indexOf("member_") === 0) url = url_member;
		
		var myAjax = new Ajax.Updater(target_id, url, {method: 'get', parameters: pars,evalScripts:true});
	}



function majax_update(p_majax,params)
	{
		
		if(majax_admin_url == null){ alert('No target set for module.'); return; }
		p_majax = $H(p_majax).toObject();
		params =  $H(params).toObject();

		if(p_majax.b_hide_loading!=true) display_loading(p_majax.target_id);
		//Module AJAX  Action
		var url = majax_admin_url + 'module.ajax.php';
		var url_public = majax_admin_url + 'module.ajax.public.php';
		var url_member = majax_admin_url + 'module.ajax.member.php';
		if(params.mf.indexOf("public_") === 0) url = url_public;
		if(params.mf.indexOf("member_") === 0) url = url_member;
		
		var myAjax = new Ajax.Updater(p_majax.target_id, url, {parameters:params,evalScripts:true
																														,onComplete:function(transport){if(p_majax.b_save_height || p_majax.b_clear_height){$(p_majax.target_id).setStyle({height: 'auto'})}}
																													}
																												);
		myAjax.b_clear_height = true;
	}


var myGlobalHandlers = 
	{
		onCreate: function(){
			Element.show('ajax_status_display');
		},
		onComplete: function(p_ajax) {
			if(Ajax.activeRequestCount == 0){
				Element.hide('ajax_status_display');
			}
		}
	};

	Ajax.Responders.register(myGlobalHandlers);		

Position.Center = function(element, parent) {
				//Don't recenter a fixed element because it's maximized or other
				if($(element).getStyle('position')=='fixed') return;
        var w, h, pw, ph;
        var d = Element.getDimensions(element);
        w = d.width;
        h = d.height;
        
        if (!parent) {
                var ws = Position.GetWindowSize();
                pw = ws[0];
                ph = ws[1];
        } else {
                pw = parent.offsetWidth;
                ph = parent.offsetHeight;
        }
				//Make sure element does not go off screen.
				offset = Element.cumulativeScrollOffset(element);//Position.realOffset(element);
				w_top = (ph/2) - (h/2) + offset[1];
				w_left = (pw/2) - (w/2) + offset[0];
				if(w_top 	<= 0 ) 	w_top = 10 + offset[1];
				if(w_left <= 0 ) 	w_left = 10 + offset[0];
        //element.style.top = w_top + "px";//Position.deltaY + "px";
        //element.style.left = w_left + "px";//Position.deltaX + "px";
				$(element).setStyle({top:w_top + "px"});
				$(element).setStyle({left:w_left + "px"});
}

Position.GetWindowSize = function(w) {
        w = w ? w : window;
        var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
        var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
        return [width, height];
}
