/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2008-02-08
	Status:		beta
	Version:	2.0.0
	Build:		20080609
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	Initialisation
	-----------------------------------------------------------------------	*/
	var x_debug = false;
	var x_page = new ClassPageX(x_debug);

/*	-----------------------------------------------------------------------	
	JQuery to be executed when the DOM document has finished loading.
	-----------------------------------------------------------------------	*/
	$(function() {
		try {
			// initialise page
			x_page.Initialise();
		} catch(e) {
			alert("loading error($): "+e.description);	
		}
	});

/*	-----------------------------------------------------------------------	
	ClassPageX:	Page javascript class
	-----------------------------------------------------------------------	*/
function ClassPageX(dbg) {
	/*	Public properties	*/
	this.parameter = "";

	/*	Private properties	*/
	var _self = this;	// self allows for a method/property to be called internally
	var _debug = (dbg)? dbg : false;
	var _id = "ClassPageX";
	var _doc = document;
	
	/*	Private methods	*/
	var _getEl = function(el) {return _doc.getElementById(el);};

	/*	Accessor methods	*/
	this.GetID = function() { return(_id); };
	
	/*	Public methods	*/
	this.Initialise = _Initialise;
	this.ToggleDisplay = _ToggleDisplay;
	this.DropShowHide = _DropShowHide;
	this.ToggleList = _ToggleList;
	this.Template = _Template;
	this.Test = _Test;
	this.calc = _calc
	this.slide_open = _slide_open;
	this.rotate = _rotate;
	this.change= _change;
	this.Ajax= _ajax;
	
	// Method:_Initialise
	function _Initialise(thisvar) {
		_Debug("_Initialise:", "thisvar", thisvar);
	}

	// Method: _ToggleList
	function _ToggleList(sel, arr) {
		
		for(var i=0;i<arr.length;i++) {
			var el = _getEl(arr[i]);
			if (el)
				el.style.display = (sel.value == arr[i])? "block" : "none";
		}
	}
	
	// Method: _ajax
	function _ajax(firstFieldName, secondFieldName, thirdFieldName){
		$(document).ready(function() {
			
			$('[name=' + secondFieldName +']').change(function(){
				if($('[name=' + firstFieldName +']').val()== "0"){
					alert("please select a region");
					$('[name=' + thirdFieldName +']').val("");
					return;
				}
				if($('[name=' + secondFieldName +']').val()== "0"){
					alert("please select an employee size category");
					$('[name=' + thirdFieldName +']').val("");
					return;
				}
				$("#loading").show();//animate({ opacity: 'show'}, 'slow');
				$("#loading").ajaxStop(function(){ $("#loading").hide();});
				$.get("/ajax.aspx?link=gentle_pricecalc&firstField=" + $('[name=' + firstFieldName +']').val() + "&secondField=" + $('[name=' + secondFieldName +']').val(),
				function(xml){
					var priceReturned = $("value",xml).text();
					$('[name=' + thirdFieldName +']').val(priceReturned);
				});
			});
		});
	}
	
	// Method: _rotate with hide
	function _rotate(first,total) {
		$(document).ready(function() {
			$(".rotate#"+first).animate({ height: 'show' }, 'slow');
				
				setTimeout( " x_page.change("+first+")",6000);
					
				if(first==total){
					first=1;
				}else{
					first++;
				}
				setTimeout( "x_page.rotate("+first+","+total+")",6500);
		});
	}
	function _change(id){
		$(".rotate#"+id).animate({ height: 'toggle' }, 'slow');
	}
	
	
	// Method: _slide_open
	function _slide_open(button, drawer) {
		$(document).ready(function() {
			
						$(button).click(
							function(){
								$(this).next(drawer).animate({ height: 'toggle', width: 'toggle'}, 'slow').siblings("div" + drawer + ":visible").animate({ height: 'toggle'}, 'slow');
							}
						);
		});
	}
	
	
	// Method: _calc 
	function _calc(firstFieldName, secondFieldName, thirdFieldName){
		$(document).ready(function() {
			var choices_A= new Array("SA1","A1","aus1","uk1","euro1","ME1","NZ1");
			var choices_B= new Array("SA2","A2","aus2","uk2","euro2","ME2","NZ2");
			var choices_C= new Array("SA3","A3","aus3","uk3","euro3","ME3","NZ3");
			var choices = new Array(choices_A, choices_B, choices_C);
			$('[name=' + secondFieldName +']').change(function(){
				var x = $('[name=' + secondFieldName +']').val();
				x--;
				
				var y = $('[name=' + firstFieldName +']').val();
				y--;
				
				
				$('[name=' + thirdFieldName +']').val(choices[x][y]);
			});
		});
	}	
	
	
	// Method: _ToggleDisplay
	function _ToggleDisplay(id){
		var el = _getEl(id);
		if (el){
			_Debug("_ToggleDisplay", id, el.style.display);
			if (el.style.display=='none' || !(el.style.display)) {
				el.style.display = 'block';
			} else {
				el.style.display = 'none';
			}
		}
		
	}

	// Method:_DropShowHide : responds to onmouseover event
	function _DropShowHide(el) {
		
		if (window.event.show == "hide")
			el.style.visibility  = "hidden";
		else
			el.style.visibility  = "visible";
	}
	
	// Method:_Template
	function _Template(thisvar) {
		_Debug("_Template:", "thisvar", thisvar);
	}

	// Method:_Test
	function _Test() {
		_Debug("_Test:");
	}
	
	// Method:_RaiseError
	function _RaiseError(thiserr) {
		_Debug("RaiseError:", "thiserr", thiserr);
		throw (thiserr);
	}
	// Method:_Debug - write debugging message. First param is the method label, followed by name, value pairs
	function _Debug(arg) {
		if (_debug) {
			var s = "<!-- ClassPageX::";
			s = s.concat(arguments[0], ":");
			var argn = arguments.length;
			for(var i=1; i<argn; i=i+2)
				s = s.concat(arguments[i], ":", arguments[i+1], " ");
			alert(s.concat("-->\n"));
		}
	}
	
}






