/**
*@author Paul Visco
*@version 1.45
*Copyright (c) 2004-2006
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

var sc = {};
sc.debug =0;

if(typeof scBase == 'undefined'){
	sc.base = '../surebertCom';
} else {
	sc.base = scBase;
}

if(typeof scNoFlash == 'undefined'){
	scNoFlash = 0;
}

if(typeof scDebug == 'undefined'){
	scDebug = 0;
}

sc.locations = {
	spacerGif : sc.base+'/spacer.gif',
	swf : sc.base+'/surebertCom.swf'
};

sc.consol = {
	num : 1,
	open : function(){
		this.box = sc.browser.win('', 600, 400, 'no');
		this.box.document.write('<html><body style="background-color:orange;"><div id="controls" style="background-color:white;border:1px solid gray;width:100%;height:20px;width:100%;"><a href="#" onclick="document.body.lastChild.innerHTML=\'\';return false;">(clear)</a></div><div id="pad" style="border:1px dotted green;background-color:lime;width:100%;height:95%;overflow:auto;"></div><body></html>');	
		this.box.onunload = function(){
				delete sc.consol.box;
		};
	},
	
	write : function(str, color, bgColor, debug){
	
		var note,preWrap;
		if(sc.debug ==1 || debug !== undefined){
			color = color || 'green';
			bgColor = bgColor || 'black';
			
			this.file(str);
			
			if(typeof this.onlog == "function"){
				this.onlog(str);
			} else {
				
				if(typeof this.box == 'undefined' || typeof this.box.document =='unknown'){
					this.open();
					this.box.focus();
				}
				
				str = sc.strings.escapeHTML(String(str));
				str = sc.strings.linkify(str);
				str = str.replace(/url\((.*?)\)/, "<a href=\"$1\" target=\"_blank\">$1</a>");
				
				if(sc.browser.agent =='ff') {
			 		preWrap = 'white-space: -moz-pre-wrap;';
			 	} else if(sc.browser.agent =='ie'){
			 		preWrap =  'word-wrap: break-word;';
			 	} else if(sc.browser.agent =='op'){
			 		preWrap =  'white-space: pre-wrap;';
			 	} else if(sc.browser.agent =='sf'){
			 		preWrap = 'white-space: pre-wrap;';
			 	}
			
			 	this.box.document.getElementById('pad').innerHTML='<pre style="'+preWrap+';padding:4px;background-color:'+bgColor+';color:'+color+'">'+this.num+'. '+new Date()+"\n"+str+'</pre>'+this.box.document.body.lastChild.innerHTML;
		 		
				note = null;
				str =null;
				this.num++;
			}
		}
		
	},
	
	error : function(str, override){
		this.write(str, 'white', 'red', override);
	},
	
	warning : function(str, override){
		this.write(str, 'black', 'yellow', override);
	},

	log : function(str, override){
		this.write(str, 'yellow', 'green', override);
	},
	
	debug : function(str){
		this.write(str, 'gray', 'pink', 1);
	},
	
	dump : function(str){
		var obj;
		
		if(typeof str =='string'){
			obj = eval(str);
		} else {
			obj = str;
		}
		
		this.debug('OBJECT: '+str+"\n"+sc.objects.dump(obj), 'white', 'red', 1);
	},
	
	file : function(str){
		
		if(typeof scLog !== 'undefined'){
	
			var xfer = new sc.ajax({debug:0,method:'post',data:'log='+escape(str)});
			xfer.fetch(scLog);
		}
	}
};

sc.dom = {
	
	
	$ : function(it, tag){
			
		if(it===null){return null;}
		if(typeof it=='object'){return it;}
		
		var x,els,obj;
		
		if(sc.isArray(it)){
			
			obj = sc.arrays.map(it, function(v){return sc.$(v);});
			
		} else if(typeof it =="string"){
	
			if(sc.arrays.inArray(sc.dom.tags, it)){
				
				obj = sc.dom.nodesToArray(document.getElementsByTagName(it), it);
				
			} else if (document.getElementById(it)){
				
				obj = document.getElementById(it);
				
			} 
		} else if(typeof it == "object" || typeof it == "function") {
			
			obj = it;
		} 
		
		if(typeof tag == 'string' && sc.arrays.inArray(sc.dom.tags, tag)){
					
			obj = sc.dom.nodesToArray(obj.getElementsByTagName(tag), tag);
			
		}
		
		if(obj !== null){
			return obj;
		} else {
			return null;
		}
	},
	
	s$ : function(obj){
		obj = sc.dom.$(obj);
		sc.objects.addProps(obj, {
			a: obj.appendChild,
			r: obj.removeChild,
			i: obj.insertBefore,
			
			css : function(prop, val){
				return sc.css(this, prop, val);
			},
			
			opacity : function(o){
				this.css('opacity', o);
				this.wh(this.offsetWidth, this.offsetHeight);
			},
			
			styles : function(params){
				try{
					sc.objects.addProps(this.style, params);
				} catch(e){}
			},
			
			wh : function(w,h){
				this.css('width', w);
				this.css('height', h);
			},
			
			mv : function(x,y,z,pos){
				pos = pos ||'absolute';
				if(this.style.position===''){this.css('position', pos);}
				this.css('left', x);
				this.css('top', y);
				this.css('zIndex', z);
				this.getBounds();
			},
			
			getBounds : function(params){
				params = params || {};
				params.addProps = 1;
				return sc.dom.getBounds(this, params);
			},
			
			dump : function(){
				this.innerHTML = 'top: '+this.top+'<br />bottom: '+this.bottom+'<br />left: '+this.left+'<br />right: '+this.right;	
			},
			
			sync :function(file, data, interval){return sc.dom.sync(this, arguments[0], arguments[1], arguments[2]);}
		
		});
		obj.getBounds();
		return obj;
	},
	
	singleTags : ['body', 'base', 'head', 'title'],
	
	tags : ['*', 'a','b','base','body','br','button','div','em','embed','form','frame','head','hr','i','iframe','img','input','label','li','link','object','ol','optgroup','option','p','pre','script','select','span','strong','table','td','textarea','tbody','th','thead','title', 'tr','u','ul'],
	
	nodesToArray : function(obj, tag){
		var x,nodes=[];
		if(sc.isEnumerable(obj)){
			obj = sc.arrays.map(obj, function(v){return v;});
			for(x=0;x<obj.length;x++){
				nodes.push(obj[x]);
			}
			
			obj=(nodes.length ==1 && sc.arrays.inArray(sc.dom.singleTags, tag))? nodes[0] : nodes;
			
			nodes = null;
		}
		return obj;
	},
	
	ce : function(typ, stylify){
		var el = document.createElement(typ);
		
		if (stylify !=='undefined'){
			sc.stylify(el);
		}
		
		return el;
	},

	txt : function(str){
		return document.createTextNode(str);	
	},

	remove : function (obj){
		obj = sc.$(obj);
	
		if(typeof(obj)=='object'){
			obj.parentNode.removeChild(obj);
		}
	},

	replace : function (obj, newObj){
		obj = sc.$(obj);
		newObj = sc.$(newObj);
		
		if(typeof(obj)=='object'){
			obj.parentNode.replaceChild(newObj, obj);
		}
	},

	x : function(obj){
		var pos = sc.dom.getBounds(obj);
		return pos.left;
	},
	
	
	y : function(obj){
		var pos = sc.dom.getBounds(obj);
		return pos.top;
	},

	getBounds : function(obj, params){
		//try{
		obj=sc.$(obj);
		params=params||{};
		var el=obj,pos={top:0,left:0};
	
		do{
			pos.top += el.offsetTop;
			pos.left += el.offsetLeft;
			if(params.pos =='rel'){
				el = false;
			} else{
				try{el = el.offsetParent;}catch(e){el = false;}
				
			}
		} while(el);

		if(params.minusScroll ===1){
			pos.top -=sc.browser.scrollY;
			pos.left -=sc.browser.scrollX;
		}
		
		pos.bottom = pos.top+obj.offsetHeight;
		pos.right = pos.left+obj.offsetWidth;
		pos.h = obj.offsetHeight;
		pos.w = obj.offsetWidth;
		
		if(params.addProps === 1){
			sc.objects.addProps(obj, pos);
		}
		
		return pos;	
		//}catch(e){}
	},
		
	create : function(typ, str, id, className){
		id = id || '';
		className = className ||'';
		var el = sc.dom.ce(typ);
		el.id = id;
		el.className = className;
		el.innerHTML = str;
		return el;
	},
	
	importNode : function(node, deep){
		var nodeHTML = node.xml||node.outerHTML;
		if(!nodeHTML) {return false;}
		if(typeof deep == "undefined"){return false;}
		var tmpNode = document.createElement("div");
		tmpNode.innerHTML = nodeHTML;
		return tmpNode.firstChild.cloneNode(deep);
	},
	
	insertBefore : function(node, before){
		node = sc.$(node);
		before = sc.$(before);
		before.parentNode.insertBefore(node, before);
		node =null;
		before = null;
	},
	
	insertAfter : function(node, after){
		node = sc.$(node);
		after = sc.$(after);
		after.parentNode.insertBefore(node, after.nextSibling);
		node=null; after=null;
	},
	
	image : function(src, alt, cl, id){
		var i = sc.dom.ce('img');
		i.src=src||'';
		i.alt=alt||'';
		i.className=cl||'';
		i.id=id||'';
		return i;
	},
	
	sync : function(node, file, data, interval){
		data = data ||'x';
		var obj =sc.$(node);
		var d = new sc.ajax({
			url: file,
			data: data,
			method: 'post',
			handler: function(dat){
				
				if(sc.isArray(obj)){
					obj.forEach(function(v,k,a){v.innerHTML=dat;v.value=dat;});
				} else {
					obj.innerHTML=obj.value=dat;
				}
				
			}
		});
		
		if(typeof interval !='undefined') {
			var intTimer = new sc.timer(interval, function(){d.data=data;d.fetch(file);});
			intTimer.begin();
			return intTimer;
		} else {
			d.fetch(file);
		}
	},
		
	isChildOf : function (child, of) {

		child = sc.$(child);
		while(child = child.parentNode) {
			if(child.id == of) {return true;}
		}
		return false;
	},
		
	toggle : function(obj){
		obj = sc.$(obj);
		if(obj.style){
			if(obj.style.display ==='' || obj.style.display=='block'){
				obj.style.display ='none';
			} else {
				obj.style.display ='block';
			}
		}
	},
	
	setTitle : function(title){
		document.title = title;
	}

};

sc.$=sc.dom.$;

sc.isEnumerable = function(it){
		
	if((typeof it == 'object' || typeof it == 'function') && it.length !== 'undefined') {
			return true;
	}
	
	return false;
};

sc.objects = {
	isProp : function(obj, prop){
		for(var p in obj){
			if(p===prop){ return true;}
		}
		return false;
	},
	
	serialize : function(obj, esc){
		var prop,val, a=[];
		for(prop in obj){
			val = obj[prop];
			if(esc == 1){
				val = escape(val);
			}
			a.push(prop+'='+val);
		}
		return a.join("&");
	},
	
	addProtos : function(obj, props) {
		props = props || {};
		for(var prop in props) {
			obj.prototype[prop] = props[prop];
		}
		return obj;
	},
	
	addProps : function(obj, props) {
		props = props || {};
		for(var prop in props) {
			obj[prop] = props[prop];
		}
		return obj;
	},
	
	dump : function(obj, ret){
		ret = ret || 0;
		var prop,dat ='';
		for(prop in obj){
			dat+="\n"+prop+' = '+obj[prop];
		}
		
		if(ret !=1 ){ return dat;} else { return '<pre style="margin:5px;border:1px;padding:5px;">'+dat+'</pre>';}
	},
	
	alertProps : function(obj){
		alert(sc.objects.dump(obj));
	}
	
};

sc.isArray = function(it){
		
	if((typeof it == 'object' || typeof it == 'function') && it.constructor) {
	
		if(it.constructor.toString().match(/array/i)){
			
			return true;
		}
	}
	
	return false;
};

sc.arrays = {
	
	inArray : function(arr, val){
		return sc.arrays.some(arr, function(v){return v===val;});
	},
	
	remove : function(arr, values){
		return sc.arrays.filter(arr, function(v){return !sc.arrays.inArray(values, v);});
	},
	
	random : function(arr){
 		return arr[sc.math.rand(0,arr.length)];
	},
	
	unique : function(arr){
		var n=[];
		sc.arrays.forEach(arr, function(v){if(!sc.arrays.inArray(n, v)){n.push(v);}});
		return n;
	},
	
	combine : function(){
		var n=[];
		sc.arrays.forEach(arguments, function(a){
				sc.arrays.forEach(a, function(val){n.push(val);});
		});
		return n;
	},
	
	inject : function(arr, index, val){
		if(index <0){return arr;}
		var a = arr.slice(0, index), b = arr.splice(index, arr.length-index);
		a[index] = val;
		return sc.arrays.combine(a,b);
	},
	
	empty : function(arr){
		arr.length=0;
		return arr;
	},
	
	min : function(arr){
		 var min=arr[0];
		 sc.arrays.forEach(arr, function(v){min=(v<min)?v:min;});
		 return min;
	},
	
	max : function(arr){
		 var max=arr[0];
		 sc.arrays.forEach(arr, function(v){max=(v>max)?v:max;});
		 return max;
	},
	
	copy : function(arr){
		return sc.arrays.filter(arr, function(){return true;});
	},

	forEach : function(arr, func){
		if(typeof func == 'function'){
			for(var k=0;k<arr.length;k++){
				func(arr[k], k, arr);
			}
		}
	},
	
	map : function(arr, func){
		var n=[];
		if(typeof func == 'function'){
			sc.arrays.forEach(arr, function(v,k,a){n.push(func(v,k,a));});
		}
		return n;
	},
	
	filter : function(a, func){
		var n=[];
		if(typeof func == 'function'){
			sc.arrays.forEach(a, function(v,k,arr){
				if(func(arr[k], k, arr) === true){
					n.push(v);
				}
			});
		}
		return n;
	},
	
	some : function(arr, func){
		if(typeof func == 'function'){
			for(var k=0;k<arr.length;k++){
				if(func(arr[k], k, arr) === true){
					return true;
				}
			}
			return false;
		}
	},
	
	every : function(arr, func){
		if(typeof func == 'function'){
			for(var k=0;k<arr.length;k++){
				if(func(arr[k], k, arr) === false){
					return false;
				}
			}
			return true;
		}
	},
	
	indexOf : function(arr, val){
		for(var k=0;k<arr.length;k++){
			if(arr[k] == val){
				return k;
			}
		}
		return -1;
	},
	
	lastIndexOf : function(arr, val){
		var p=-1,k;
		for(k=0;k<arr.length;k++){
			if(arr[k] == val){
				p=k;
			}
		}
		return p;
	},
	
	regex: function(arr, expression) {
		
		var a = arr.filter(function(v, k, a) {
		 	if(v.v.toString().match(expression)){return true;}
		});
		
		return a;
	}
};

sc.css = function(){
	var obj,prop,val=null,values,x;
	//arguments obj, prop, val
	if(typeof this.obj != 'undefined'){

		obj = this.obj;
		prop = arguments[0];
		val = (arguments[1] !== undefined) ? arguments[1]  :null;
		
	} else {
	
		obj = sc.$(arguments[0]);
		prop = arguments[1];
		val = (arguments[2] !== undefined) ? arguments[2] :null;
		
	}
	
	if(sc.isArray(obj)){
		
		values = [];
		sc.arrays.forEach(obj, function(v){
			if(val===null){
				values.push(sc.styles.read(v, prop));
			} else {
				sc.styles.write(v, prop, val);
			}
		});
		return values;
	
	}

	if(val === null){
		return sc.styles.read(obj, prop);

	} else {
		sc.styles.write(obj, prop, val);
	}
};

sc.styles = {
	
	newSheets : [],
	
	pxProps : ['fontSize', 'width', 'height', 'padding', 'border', 'margin', 'left', 'top'],
	
	writeRule : function(domEl, rule){
		
		if(typeof this.sheet ==='undefined'){
			if(document.createStyleSheet) {
				this.sheet = document.createStyleSheet('');
			} else {
			
				var sheet = document.createElement('style');
				sheet.type="text/css";
				sheet.appendChild(document.createTextNode(domEl+'{'+rule+'}'));
				this.sheet = sc.$('head').appendChild(sheet);
				sheet=null;
			}
		}
		
		if(this.sheet.insertRule){
			this.sheet.insertRule(domEl+'{'+rule+'}', this.numRules);
		} else if(this.sheet.addRule){
			this.sheet.addRule(domEl, rule);
		} else if (document.styleSheets.length >0){
			document.styleSheets[document.styleSheets.length-1].insertRule(domEl+'{'+rule+'}', this.numRules);
		}
		
		this.numRules++;
	},
	
	read : function(obj, prop){
		obj = sc.$(obj);
		var val;
		if(prop.match(/^border$/)){
			prop = 'border-left-width';				
		}
				
		try{
			if (obj.style[prop]) {
				val = obj.style[prop];
				return val.replace(/px/, '');
			} else if (obj.currentStyle) {
				
				prop = sc.strings.toCamel(prop);
				val = obj.currentStyle[prop];
				return val.replace(/px/, '');
			} else if (document.defaultView && document.defaultView.getComputedStyle) {
					
				prop = prop.replace(/([A-Z])/g, "-$1");
				prop = prop.toLowerCase();
				
				val = document.defaultView.getComputedStyle(obj,"").getPropertyValue(prop);
				return val.replace(/px/, '');
				
			} else {
				
				return null;
			}
			
		} catch(e){}
		
	}, 

	write : function(obj,prop,val){
		obj = sc.$(obj);
		val = String(val);
		if(sc.styles.pxProps.inArray(prop) && val !=='' && !val.match(/em|cm|pt/)){
		
			if(!val.match("px")){val +='px';}
		}
		
		prop = sc.strings.toCamel(prop);
		
		if(prop == 'opacity'){
			if(val <= 0){ val =0;}
			if(val >= 1){ val =1;}
			obj.style.opacity = val;
			
			if(typeof obj.style.filter == 'string'){
				obj.style.filter = "alpha(opacity:"+val*100+")";
			}
			
		} else {
			obj.style[prop] = val;
		}
		
		return val;
	},

	load : function(href){

		var extSheet = document.createElement('link');
		extSheet.rel = 'stylesheet';
		extSheet.href = href;
		extSheet.type = 'text/css';
		this.newSheets.push(extSheet);
		return sc.$('head').appendChild(extSheet);
		
	},
	
	disable : function(sheet){
		if(typeof sheet !='undefined'){sheet.disabled=1;}
		
	},
	
	disableLoaded : function(){
		for(var x=0;x<this.newSheets;x++){
			this.disable(this.newSheets[x]);
		}
	},
	
	wrapPre : function(){
	 	if(sc.browser.agent =='ff') {
	 		this.writeRule('pre', 'white-space: -moz-pre-wrap;');
	 	} else if(sc.browser.agent =='ie'){
	 		this.writeRule('pre', 'word-wrap: break-word;');
	 	} else if(sc.browser.agent =='op'){
	 		this.writeRule('pre', 'white-space: -o-pre-wrap;');
	 	} else if(sc.browser.agent =='sf'){
	 		this.writeRule('pre', 'white-space: pre-wrap;');
	 	}
	},
	
	copy : function(from, to){
		from = sc.$(from);
		to = sc.$(to);
		
		for(var prop in from.style){
			try{to.style[prop] = from.style[prop];}catch(e){}
		}
	}
	
};

sc.styles.numRules =1;

sc.stylify = function(obj){
	return sc.dom.s$(obj);
};

sc.keepTrying = function(){
	for(var x=0;x<arguments.length;x++){
		try{return arguments[x]();} catch(e){sc.consol.error(e);}
	}
};

sc.javascript = {
	load : function(src){
		var extScript = document.createElement('script');
		extScript.src = src;
		extScript.type = 'text/javascript';
		return sc.$('body').appendChild(extScript);	
	},
	
	list : function(){
		var x, scripts= sc.$('script');
		this.scripts = [];
		for(x=0;x<scripts.length;x++){
			if(scripts[x].src.match(/js/)){
				this.scripts.push(scripts[x].src);
			}
		}
		return this.scripts;
	},
	
	check : function(src){
		this.list();
		var x,found =0;
		for(x=0;x<this.scripts.length;x++){
			if(sc.strings.basename(this.scripts[x]) == sc.strings.basename(src)){
				return 1;
			}
		}
		return found;
	}
};

sc.forms = {
	getInputsByName : function(name, form){
		var inputs=[], forms;
		if(form ===undefined){
			forms = sc.$('form');
			forms.forEach(function(v){inputs = sc.arrays.combine(inputs, sc.$(v, 'input'));});
			forms =null;
		} else {
			inputs = sc.$(form, 'input');
		}
		
		inputs = inputs.filter(function(v){return v.name==name;});
		
		return inputs;
	},
	
	input : function(typ, val, nam){
		var inp = sc.dom.ce('input');
		inp.type=typ;
		inp.value=val;
		inp.name=nam;
		inp.id=nam;
		return inp;
	},

	toggleChecked : function(name, status){
	
		sc.forms.getInputsByName(name).forEach(function(v){
			if(status !== undefined){v.checked=status;} else if(v.checked==1){v.checked=0;} else {v.checked=1;}
		});
	},
	
	serialize : function(form) { 
		var dat=[],s,e=sc.dom.nodesToArray(sc.$(form).elements);
		e.forEach(function(v){
			var n=v.name,t=v.type;
			if(n && v.value){
				if(t=='select-one'){
					dat.push(n + "=" + escape(v.options[v.selectedIndex].value));
				} else if(t =="select-multiple"){
					for(s=0;s<v.options.length;s++){
						if(v.options[s].selected===true){
							dat.push(n + "=" + escape(v.options[s].value));	
						}
					}
				} else if(t == "checkbox" || t=="radio"){
					if(v.checked==1){dat.push(n + "=" + escape(v.value));}
				} else {
					dat.push(n + "=" + escape(v.value));
				}
			}
		});
		
		return dat.join('&');
	},
	
	send : function(form, url, interval){
		if(sc.$(form)){
			var sendVal = new sc.ajax({url:url,format:'send',method:'post',data:sc.forms.serialize(form)});
			if(interval !=='undefined'){
				var intTimer = new sc.timer(interval, function(){sendVal.fetch();});
				intTimer.begin();
				return intTimer;
			} else {
				sendVal.fetch();
			}
		}
	},
	
	getSelection : function(field){
		field = sc.$(field);
		var range,sel={},selectionEnd,selectionStart,stored_range;
		field.focus(); 
		
		if (document.selection) {

			//if(document.selection.type =="None"){return;}

			range = document.selection.createRange();
			stored_range = range.duplicate();
			stored_range.moveToElementText( field );
			stored_range.setEndPoint( 'EndToEnd', range );
			selectionStart = stored_range.text.length - range.text.length;
			
			selectionEnd = selectionStart + range.text.length;
			sel.begin = selectionStart;
			sel.end = selectionEnd;
	
		} else if(typeof field.selectionStart !='undefined'){
	
			sel.begin = field.selectionStart;
			sel.end = field.selectionEnd;
		} 
	
		sel.caret = sel.begin;
		sel.before = field.value.substr(0, sel.begin);
		sel.selected = field.value.substr(sel.begin, (sel.end - sel.begin));
		sel.after = field.value.substr(sel.end, (field.value.length - sel.end));
		return sel;
	},
	
	addTags : function(field, beginTag, endTag){
		field = sc.$(field);
		var sel = sc.forms.getSelection(field); 
		var tagLength = beginTag.length +endTag.length;
		field.value = sel.before + beginTag + sel.selected + endTag + sel.after;
		sc.timer.wait(0, function(){
			field.focus();sc.forms.movecaret(field, sel.caret);
		});
	},
	
	setSelection : function(field, start, end) {
		field = sc.$(field);
		if (field.setSelectionRange) {
			field.setSelectionRange(start, end);
		} else {
			var r = field.createTextRange();
			r.collapse(true);
			r.moveStart("character", start);
			r.moveEnd("character", end - start);
			r.select();
		}
	},
	
	replaceSelection : function(field, txt){
		field = sc.$(field);
		var sel = sc.forms.getSelection(field);
		
		field.value = sel.before + txt + sel.after;	
		sc.timer.wait(0, function(){
			try{sc.forms.moveCaret(field, sel.caret+1);field.focus();}catch(e){}
		});
	},
	
	insertAtCaret : function(field, txt){
		field = sc.$(field);
		sc.forms.replaceSelection(field, txt);
		sc.timer.wait(0, function(){
			field.focus();
		});
					
	},
	
	moveCaret : function(field, pos){
		field = sc.$(field);
		if (field.setSelectionRange) {
			field.setSelectionRange(pos, pos);
		} else {
			var r = field.createTextRange();
			r.collapse(true);
			r.moveStart("character", pos);
			r.moveEnd("character", pos - pos);
			r.select();
		}
		field.focus();
	},
	
	allowTabs : function(textarea){
		textarea = sc.$(textarea);
		sc.events.add(textarea, 'keydown', function(e){
			var key = sc.keys(e), tab=String.fromCharCode(9);
			
			if(key.tab==1){
				try{
					sc.forms.insertAtCaret(textarea,tab);
				}catch(e){}
				return false;
			}
		});
	}	
};

sc.ajax = function(params){ 
	var t=this;
	
	/* instantiate xmlhttp object */
	try{t.o=new XMLHttpRequest();}catch(e){}
	try{t.o=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){}
	try{t.o=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}
	if(!t.o){return null;}
	t.debug=0;t.data='';t.local=0;t.method=sc.ajax.defaultMethod;t.format = sc.ajax.defaultFormat;
	sc.objects.addProps(this, params);
	
};
sc.ajax.debug =0;

sc.ajax.defaultMethod = 'get';
sc.ajax.defaultMethod = 'text';

sc.ajax.prototype = {

	response :0,
	onlog :'',
	log : [],

	addToLog : function(log){
	
		var t=this;
		
		if(typeof(log)=='number'){
			log = {'data':sc.messages[log],'code':log,'type':'error'};
		} else if(typeof(log)=='object'){
			
		} else {
			log = {'data':log,'code':5,'type':'log'};
		}
	
		if(typeof(t.onlog)=='function'){
			t.onlog(log);
		} else {
			try{
				if(t.debug == 1 || (sc.ajax.debug == 1 && t.debug !==0)){
					
					if(log.type =='error'){
						sc.consol.error(log.data, 1);
					} else {
						sc.consol.log(log.data, 1);
					}
				}
			}catch(e){}
		}
	},
	
	onreadystatechange : function() {
		var t=this;
		if (t.o.readyState != 4) {return; }
			if(t.method =='get'){
				
				t.addToLog('URL:url('+t.url+'?'+t.data+")\nDATA: "+t.data+'\nMETHOD:'+t.method+'\n');
			} else {
				
				t.addToLog('URL:url('+t.url+")\nDATA: "+t.data+'\nMETHOD:'+t.method+'\n');
			}
			
		
			
		try{
			if (t.o.status != 200 && t.local !==1){
				
				t.addToLog({'data':sc.messages[2]+"\nURL:url("+t.url+")\nDATA:("+t.data+")\nSTATUS: "+t.o.status+"\nSTATUS TEXT: "+t.o.statusText,'code':2,'type':'error'});
				return;
			}
		} catch(e){return;}
		
		if(t.format !='send'){
			if(t.o.responseText === ''){
				t.addToLog(3);
			} else{
				t.addToLog("HEADER: "+"\n"+t.o.getAllResponseHeaders()+"\n\nRECEIVED: \n"+t.o.responseText);
			}
		}
		
		if(t.format == "head" || typeof t.header !=='undefined'){
			if(typeof t.header ==='undefined'){
				t.response = t.o.getAllResponseHeaders();
			} else {
				t.response = t.o.getResponseHeader(t.header);
			}
		} else if(t.format == "xml"){ 
			if(t.o.responseXML !== null){ 
				t.response = t.o.responseXML.documentElement;
			} else { 
				t.addToLog(4);
			}
			
		} else if (t.format == "js") {
			t.response = t.o.responseText;
			eval(t.response); 
		} else if (t.format == "send"){
			t.addToLog(13);
		} else {
			t.response = t.o.responseText;
		}
		
		if(typeof(t.handler) =='function'){t.handler(t.response);}
		t.o.abort();
		return; 
	},
	
	fetch : function(url) {
		
		var t=this;
		url = url || t.url;
		t.url =url;
		
		if(!t.o){t.addToLog(1);return;}
		t.o.onreadystatechange = function(){t.onreadystatechange();};
	
		if(t.method=='get' && t.data !== undefined){
			url = url+'?'+t.data;
		}
		
		t.o.open(t.method, url, true);
	
		if(t.method=='post'){
			t.o.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
		
		try{t.o.send(t.data); }catch(e){}
		
		//clear data if save not requested
		//if(t.saveData !==1){t.data='';}
	},
	
	eatForm : function(form) { 
		var t=this;
		if(t.data !==''){t.data +="&";}
		t.data += sc.forms.serialize(form);
	},
	
	set : function(params){
		sc.objects.addProps(this, params);
	}
};

sc.fileExists =function(file, handler){
	var sb_check = new sc.ajax();
	sb_check.onlog = function(e){
		if(typeof handler == 'function'){
			if(e.code ==2){handler();}
		}
	};
	sb_check.fetch(file);
};

sc.cookies={

	days : 7,
	path : '/',
	domain : '',
	onlog : '',
	
	recall : function(name){
		var i,n, parts = document.cookie.split(';');
		
		for(i=0;i<parts.length;i++){
			n = parts[i].split('=');
			n[0] = n[0].replace(/ /, "");
			if(name==n[0]){
				return unescape(n[1]);
			} 
		}
		return false;
	},
	
	listAll : function(){
		var i, c, list=[], parts = document.cookie.split(';');
		
		for(i=0;i<parts.length;i++){
			c = parts[i].split('=');
			list.push(c[0].replace(/ /, ""));
		}
		return list;
	},
	
	remember : function(name, value, days){

		days = days || this.days;
	
	    var ck, date = new Date();
	    date.setTime(date.getTime()+(days*24*60*60*1000));
	
		ck=name+'='+escape(value)+'; expires='+date.toGMTString()+'; path='+this.path+';'+' host='+this.domain;
		document.cookie =ck;
	},
	
	forget : function(name){
		
		this.remember(name, "", -1);
	},
	
	forgetAll : function(name){
	
		var n,i,deleted =[], parts = document.cookie.split(';');
		for(i=0;i<parts.length;i++){
			n = parts[i].split('=');
			
			if(n[0] !== undefined){
				deleted.push(n[0]);
				this.remember(n[0], "", -1);
			}
		}
	}
};

sc.strings = {
	stripHTML : function(str){
		var re = new RegExp("(<([^>]+)>)", "ig");
		str = str.replace(re, "");
		var amps = ["&nbsp;", "&amp;", "&quot;"];
		var replaceAmps =[" ", "&", '"'];
		for(var x=0;x<amps.length;x++){
			str = str.replace(amps[x], replaceAmps[x]);
		}
		
		re = new RegExp("(&(.*?);)", "ig");
		str = str.replace(re, "");
		
		return str;
	},
	
	escapeHTML : function(str){
		str = str.replace(/</g, '&lt;');
		return str.replace(/>/g, '&gt;');
	},

	nl2br : function(str){
		var re = new RegExp("\n", "ig");
		return str.replace(re, "<br />");
	},
	
	br2nl : function(str){
		var re = new RegExp("<[br /|br]>", "ig");
		return str.replace(re, "\n");
	},
	
	trim : function(str, side) {
		side = side || 'lr';
	
		switch(side){
			case 'lr':
				return  str.replace(new RegExp("^\\s*|\\s*$", "g"),"");
			case 'l':
				return  str.replace(new RegExp("^\\s*(.*?)$", ""), "$1");
			case 'r':
				return  str.replace(new RegExp("^(.*?)\\s*$", ""), "$1");
		}
	},
	
	ltrim : function(str){
		sc.strings.trim(str, 'l');
	},
	
	rtrim : function(str){
		sc.strings.trim(str, 'r');
	},
	
	stripWhitespace : function(str){
		var spaces = "\\s";
		return str.replace(new RegExp("\\s", "g"), "");
	},
	
	numpad : function(num){
		return (num<=9) ? '0'+num : num;
	},
	
	isNumeric : function(str){
		if(typeof str == 'number'){
			return 1;
		}
		var dat,valid = "0123456789.",x;
	
		for (x=0;x<str.length;x++){ 
			dat = str.charAt(x); 
			if (valid.indexOf(dat) == -1){
				 return false;
			}
		}
		return true;
	},
	
	basename : function(str){
		var re = new RegExp("/\\/", "g");
		str = str.replace(re, "/");
		var filename=str.split("/");
		return filename[(filename.length - 1)];
	},
	
	substrCount : function(str, needle, caseInsensitive){
		var matches, ig = (caseInsensitive === undefined) ? 'g' : 'ig';
			
		var re = new RegExp(needle, ig);
		matches = str.match(re);
		if(matches !==null){
			return matches.length;
		} else {
		 	return false;
		}
	},

	strstr: function(str, needle, caseInsensitive){
		var ig = (caseInsensitive === undefined) ? 'g' : 'ig';
		var re = new RegExp(needle, ig);
		var matches = str.match(re);
		if(matches !==null){
			return true;
		} else {
		 	return false;
		}
	},
	
	stristr : function(str, needle){
		return sc.strings.strstr(str, needle, 1);
	},
	
	px : function(str){
		if(!String(str).match(/px/)){
			str +='px';
			return str;
		} else {
			return str.replace('px', ''); 
		}
	},
	
	hilite : function(str, needle, className){
		className = className || 'hilite';
		//match all the instances of a text str within an HTML objects innerHTML
		var matches = new RegExp( "("+needle+")", "ig");
		//make it yellow and underlined with a class of markMe so further css can be applied
		return str.replace(matches, '<u class="'+className+'">$1</u>');
	},
	
	linkify : function(str, target){
		target = target || '_blank';
		var match_url = new RegExp("(\s|\n|)([a-z]+?):\/\/([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)", "i");
		return str.replace(match_url, "<a href=\"$2://$3\" title=\"$2://$3\" target=\""+target+"\">::link::</a>");
		
	},
	
	toCamel : function(str){
		var x, dashes = str.match(/(.)-(.)/g);
		if(dashes !==null){
			for(x=0;x<dashes.length;x++){
				str=str.replace(dashes[x], dashes[x].charAt(0)+dashes[x].charAt(2).toUpperCase());
			}
		}
		return str;
	}

};

sc.date = function(str, timestamp){

	this.timestamp = timestamp || undefined;
	this.process(str, timestamp);
};

sc.date.prototype= {
	process : function(str, timestamp){
		var prop,d = new Date();
		if(this.timestamp !== undefined){
			d.setTime(timestamp*1000);
		}
		
		d.m = sc.strings.numpad(d.getMonth()+1);
		d.F = sc.dates.months[d.getMonth()];
		d.M = d.F.substr(0,3);
		d.d = sc.strings.numpad(d.getDate());

		d.l = String(sc.dates.days[d.getDay()]);
		d.D = d.l.substr(0,3);
		//d.w = sc.dates.days.indexOf(d.l);
		d.Y = String(d.getFullYear());
		d.y = d.Y.substr(2,4); 
		d.H = sc.strings.numpad(d.getHours());
		d.h = (d.H >12) ?  sc.strings.numpad(d.H-12) : d.H;
		d.i = d.getMinutes();
		d.s = sc.strings.numpad(d.getSeconds());
		d.U = Date.parse(d)/1000;
		d.n = d.getMilliseconds();
		d.date = d.m+'/'+d.d+'/'+d.y;
		d.time = d.h+':'+d.i;
		d.time24 = d.H+':'+d.i;
	
		for(prop in d){
			this[prop] = d[prop];
		}
		d=null;
		
		if(str !==''){this.toString = function(){return this.format(str);};}
		return;
	},
	
	format : function(str){
		var x,f='';
		if(str !== undefined){
			
			for(x=0;x<str.length;x++){
				switch(str.charAt(x)){
					case 'm':
						f += str.charAt(x).replace(/m/, this.m);
					break;
					
					case 'M':
						f += str.charAt(x).replace(/M/, this.M);
					break;
					
					case 'F':
						f += str.charAt(x).replace(/F/, this.F);
					break;
					
					case 'd':
						f += str.charAt(x).replace(/d/, this.d);
					break;
					
					case 'D':
						f += str.charAt(x).replace(/D/, this.D);
					break;
					
					case 'l':
						f += str.charAt(x).replace(/l/, this.l);
					break;
					
					case 'w':
						f += str.charAt(x).replace(/w/, this.w);
					break;
					
					case 'Y':
						f += str.charAt(x).replace(/Y/, this.Y);
					break;
					
					case 'y':
						f += str.charAt(x).replace(/y/, this.y);
					break;
					
					case 'H':
						f += str.charAt(x).replace(/H/, this.H);
					break;
					
					case 'h':
						f += str.charAt(x).replace(/h/, this.h);
					break;
					
					case 'i':
						f += str.charAt(x).replace(/i/, this.i);
					break;
					
					case 's':
						f += str.charAt(x).replace(/s/, this.s);
					break;
					
					case 'U':
						f += str.charAt(x).replace(/U/, this.U);
					break;
					
					case 'n':
						f += str.charAt(x).replace(/n/, this.n);
					break;
					
					case 'w':
						f += str.charAt(x).replace(/w/, this.w);
					break;
					
					default:
						f+= str.charAt(x);
				}
			
			}
		
			this.formatted =f;
			f=null;
			return this.formatted;
		}
	}
};

sc.dates = {
	months : ["January","Febuary","March","April","May","June","July","August","September","October","November","December"],

	days : ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
	
};

/* based on script from quirksmode.org event binding contest*/
sc.events = {
	log : [],
	add: function(obj, event, handler){
		obj = sc.$(obj);
		if(obj.forEach){
			obj.forEach(function(v,k,a){sc.events.add(v, event, handler);});
		}
		
		if (obj.addEventListener){
			obj.addEventListener( event, handler, false );
		} else if (obj.attachEvent) {
			obj["e"+event+handler] = handler;
			obj[event+handler] = function() { obj["e"+event+handler]( window.event ); };
			obj.attachEvent( "on"+event, obj[event+handler] );
		}
		
		this.record(obj, event, handler);
		return this.log.length-1;
	},
	
	remove: function(event){
		try{
			this.removeEvent(this.log[event][0], this.log[event][1], this.log[event][2]);
		} catch(e){}
	},
	
	removeAll: function(){
		var e;
		for(e in this.log){
			this.remove(e);
		}
		this.log =[];
	},

	removeEvent : function(obj, event, handler){
		if (obj.removeEventListener){
			obj.removeEventListener( event, handler, false );
		} else if (obj.detachEvent){
			obj.detachEvent( "on"+event, obj[event+handler] );
			obj[event+handler] = null;
			obj["e"+event+handler] = null;
		}
	},
	
	record: function(obj, event, handler){
	
		this.log.push([obj, event, handler]);
	},
	
	preventDefault : function(e){
		
		if(window.event){
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		} else {
			e.stopPropagation();
			e.preventDefault();
		}
	}
	
};

$_GET = [];
sc.browser ={
	init : function(){
		if(window.location.search !==''){this.populateGET();}
		
		this.getAgent();
		this.measure();
	},
	
	populateGET : function (){
		var i,val,key;
		var q = window.location.search.substring(1);
		var v = q.split("&");
	
		for (i=0;i<v.length;i++) {
			var s = v[i].split("=");
			
			key = unescape(s[0]);
		
			val = unescape(s[1]);
			$_GET[key] = val.replace("+", " ");
		 }
	},
	
	measure : function(){
	
		if( typeof(window.innerWidth) == 'number' ) {
		    //Non-IE
		    sc.browser.w = window.innerWidth;
		    sc.browser.h = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {

		    sc.browser.w = document.documentElement.clientWidth;
		    sc.browser.h = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {

		    sc.browser.w = document.body.clientWidth;
		    sc.browser.h = document.body.clientHeight;
		}
		
		return [sc.browser.w, sc.browser.h];
	},
	
	win : function(url, w, h, sb){
		if(sb =='y' || sb === 1){sb ='yes';}
		if(sb =='n' || sb === 0){sb ='no';}
		if(sb != 'yes' && sb !='no'){sb ='yes';}
	
		var newWin = window.open(url,'name','height='+h+',width='+w+',menubar=no,resizable=yes,toolbar=no,scrollbars='+sb);
		sc.browser.newWindows.push(newWin);
		newWin.focus();
		return newWin;
	},
		
	closePops : function(){
		for(var x=0;x<sc.browser.newWindows.length;x++){
			sc.browser.newWindows[x].close();
		}
	},
	
	scrollPos : function(){
		var x,y;
		if(window.pageYOffset){
			y = window.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			y= document.documentElement.scrollTop;
		} else if (document.body && document.body.scrollTop){
			y= document.body.scrollTop;
		} else if (document.documentElement && !document.documentElement.scrollTop){
			y = 0;
		}
		sc.browser.scrollY = y;
	
		if(window.pageXSOffset){
			x = window.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollLeft){
			x = document.documentElement.scrollLeft;
		} else if (document.body && document.body.scrollLeft){
			x = document.body.scrollLeft;
		} else if (document.documentElement && !document.documentElement.scrollLeft){
			x = 0;
		}
		
		sc.browser.scrollX = x;
		return [sc.browser.scrollX, sc.browser.scrollY];
	},

	getAgent : function(){
		var opera = new RegExp("Opera/(\\d{1}\.\\d{1})", "i");
		var safari = new RegExp("safari/(\\d{3})", "i");
		var firefox = new RegExp("firefox/(\\d{1}\.\\d{1})", "i");
		var os,agent = navigator.userAgent;
		var str;
		
		if(agent.match(/Mac_PowerPC/ig)){
			this.agent = 'iemac';
		} else if(window.opera && !window.print){
			this.agent = 'op';
			this.version =5;
		} else if(window.opera && window.print && !document.childNodes){
			this.agent = 'op';
			this.version =6;
		} else if(window.opera && document.childNodes) {
			this.agent = 'op';
			this.version =7;
			str = navigator.userAgent.match(new RegExp("Opera/(\\d{1}\.\\d{1})", "i"));
			this.version = str[1];
			
		} else if(document.all && !document.getElementById){
			this.agent = 'ie';
			this.version = 4;
		} if(document.all && !document.getElementById && !document.compatMode){
			this.agent = 'ie';
			this.version = 5;
		} else if (document.all && !window.XMLHttpRequest && document.compatMode){
			this.agent = 'ie';
			this.version = 6;
		} else if (document.all && window.XMLHttpRequest && document.compatMode){
			this.agent = 'ie';
			this.version = 7;
	
		} else if(agent.match(safari)){
			str = agent.match(safari);
			this.agent = 'sf';
			if(str[1] <= 100){
				this.version = 1;
			} else if(str[1] <= 200){
				this.version =1.2;
			} else if(str[1] < 400){
				this.version =1.3;
			} else if(str[1] > 400){
				this.version =2;
			}
		
		} else if(agent.match(firefox)){
			this.agent = 'ff';
			str = agent.match(firefox);
			this.version = str[1];
	
		} else {
			this.agent='other';
		}
	
		if(agent.match(/mac/i)){
			this.os = 'mac';
		} else if (agent.match(/window/i)){
			this.os = 'win';
		} else if (agent.match(/linux/i)){
			this.os = 'lin';
		}
		return this.agent;
	},

	pngFix : function(obj){
		var im,images,src,st;
		if(this.agent == 'ie' && this.version <7){
			obj = sc.$(obj);
			if(typeof obj.src !='undefined'){
				images = [obj];
			} else {
				images = obj.getElementsByTagName('img');
			}
			
			for(var i=0;i<images.length;i++)
			{	
				im = images[i];
				src = im.src;
				st = im.style;
				if( src.substr((src.length -3),3)== "png"){
					st.width = im.width + 'px';
					st.height = im.height + 'px';
					im.src = sc.locations.spacerGif;
					st.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='image')";
		
				}
			}
			
		}
	},
	
	removeSelection : function(){
		try{
			if(window.getSelection){
				window.getSelection().removeAllRanges();
			} else if(document.selection){
				document.selection.empty();
			}
		}catch(e){}
	}
};

sc.browser.newWindows = [];

sc.browser.init();

sc.math = {

	rand : function(min,max){
		min = min || 0;
		max = max || 100;
		return Math.floor(Math.random()*max+min);
	},
	
	hex2dec : function(str){
		var val = parseInt(str,16);
		val = (typeof(val) =='number') ? val : 0;
		return val;
	},
	dec2hex: function(num){
		var hex = "0123456789ABCDEF",mask = 0xf,val='';
	
		while(num !== 0)
		{
			val = hex.charAt(num&mask) + val;
			num>>>=4;
		}
		
		return sc.strings.numpad(val);
	},
	round: function(val, precision){
		var x,dec = '1';
		for(x=1;x<=precision;x++){
			dec +='0';
		}
		dec = parseInt(dec, 10);
		return Math.round(val*dec)/dec;
	}
	
};

sc.colors = {

	rgb2hex : function(rgb, asArray){
		if(!rgb.match(/^rgb/i)){return false;}
		
		var re = new RegExp('rgb\\((\\d{1,}),(\\d{1,}),(\\d{1,})\\)', "ig");
		var colors = re.exec(sc.strings.stripWhitespace(rgb));
		var r= sc.math.dec2hex(colors[1]);
		var g= sc.math.dec2hex(colors[2]);
		var b= sc.math.dec2hex(colors[3]);
		if(asArray){
			return [r,g,b];
		} else {
			return '#'+r+''+g+''+b;
		}
	},
	
	hex2rgb : function(hex, asArray){
		
		hex = sc.strings.stripWhitespace(hex).replace("#", "");
		var r= sc.math.hex2dec(hex.substr(0,2));
		var g = sc.math.hex2dec(hex.substr(2,2));
		var b = sc.math.hex2dec(hex.substr(4,2));
		if(asArray){
			return [r,g,b];
		} else {
			return 'rgb('+r+','+g+','+b+')';
		}
	},
	
	color2array : function(color){
		if(color.match(/\#/)){
			color = sc.colors.hex2rgb(color);
		}
		
		var re = new RegExp('rgb\\((\\d{1,}),(\\d{1,}),(\\d{1,})\\)', "ig");
		var colors = re.exec(sc.strings.stripWhitespace(color));
		
		return [colors[1], colors[2], colors[3]];
		
	},
	
	rand : function(grey){
		var rand = sc.math.rand;
		if(grey == 1){
			grey = rand(0,255);
			return "rgb("+grey+","+grey+","+grey+")";	
		} else {
			return "rgb("+rand(0,255)+","+rand(0,255)+","+rand(0,255)+")";	
		}
	}
	
};

sc.keys = function(e){

	var k, key, pressed;
	e = e || window.event;
	key = e.keyCode;
	
	k = {};
	k.esc = (key == 27) ? 1 :0;
	k.ret = (key == 13) ? 1 :0;
	k.tab = (key ==9) ? 1 : 0;
	k.shift = (e.shiftKey) ? 1 :0;
	k.ctrl = (e.ctrlKey) ? 1 :0;
	k.alt = (e.altKey) ? 1 :0;
	k.home = (e.keyCode == 36) ? 1 : 0;
	k.up = (e.keyCode == 38) ? 1 : 0;
	k.down = (e.keyCode == 40) ? 1 : 0;
	k.left = (e.keyCode == 37) ? 1 : 0;
	k.right = (e.keyCode == 39) ? 1 : 0;
	k.letter = String.fromCharCode(key).toLowerCase();
	if(!k.letter.match(/\w/)){
		k.letter ='';
	}

	if(sc.debug ===1){
		sc.consol.log(sc.objects.dump(k));
	}
	
	return k;
};


sc.timer = function(sec, handler){
	var t =this;
	t.interval = (sec !== undefined) ? sec*1000 : 1000;
	if(handler !== undefined){t.handler=handler; }
	t.count=1;
};

sc.timer.wait = function(sec, func, beginHandler){
	if(typeof(beginHandler) == 'function'){beginHandler();}
	return window.setTimeout(func, sec*1000);
};

sc.timer.cancel = function(evt){
	window.clearTimeout(evt);
};
	
sc.timer.prototype = {

	begin : function() {
	
		var t = this;
		this.end();
		this.repeater = window.setInterval(function () {
			if (typeof t.handler  == "function"){
				
				t.handler();
				t.count++;
			}
		}, this.interval);
		
	},
	
	end : function (resetCount) {

		if (this.repeater !== null){window.clearInterval(this.repeater);}
		
		if(arguments.length !==0){this.count=0;}
		
		if (typeof(this.endHandler) == "function"){
			this.endHandler();
		}
	},
	
	reset : function(){
		this.end(1);
		this.begin();
	},
	
	changeInterval : function(interval){
	
		this.interval = interval;
		this.end();
		this.begin();
	}
};


sc.swf = function(src, width, height, bgColor, version, alt){
	if(typeof arguments[0] == 'object'){
		sc.objects.addProps(this, arguments[0]);
	} else {
		this.src = src;
		this.width = width || '150px';
		this.height = height || '105px';
		this.bgColor = bgColor || '#FFFFFF';
		this.id = 'sc_swf_'+sc.swf.instanceId;
		this.version = version || 5;
		this.alt = alt || '';
	}
	sc.swf.instanceId++;
};

sc.swf.swfs=[];
sc.swf.instanceId=0;

sc.swf.prototype = {
	
	embed : function(obj){
		var dat;
		
		//embed flash move in all non IE browsers
		if(sc.swf.format=='embed'){
			
			dat = '<embed type="application/x-shockwave-flash" src="'+this.src+'"  id="'+this.id+'"   bgcolor="'+this.bgColor+'" width="'+this.width+'" height="'+this.height+'"  />';
		
		} else if(sc.swf.format=='object'){
				dat = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+this.width+'" height="'+this.height+'" id="'+this.id+'"><param name="movie" value="'+this.src+'" /><param name="bgcolor" value="'+this.bgColor+'" /></object>';
		}
		
		if(this.version > sc.swf.version){
			dat =this.alt;
		}
		
		if(sc.strings.isNumeric(obj)){
			var swf = {};
			swf.dat = dat;
			swf.id = this.id;	
			return swf;
			
		} else if(sc.$(obj)){
			sc.$(obj).innerHTML =dat;
		} else {
			window.document.body.innerHTML +=dat;
		}
	
		return sc.$(this.id);
	}
};

sc.swf.version =4;

sc.swf.check= function(){
	try{
	var versionStr = new RegExp("\\d{1}\.\\d{0,5}", "i");
	var str = navigator.plugins["Shockwave Flash"].description;
	if(str.match(versionStr)){
		sc.swf.version = str.match(versionStr);
	}
	} catch(e){sc.swf.version=0;}
	return sc.swf.version;
};

sc.swf.testIe = function(){
	try{
		var x = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + sc.swf.version);
		if(x){
			return false;
		}
	} catch(e){return true;}
		
};

sc.swf.ieCheck= function(){	
	try{
		//THERE MUST BE A BETTER SOLUTION
		while(!sc.swf.testIe()){
			sc.swf.version++;
		}
		sc.swf.version--;
		return sc.swf.version;
	} catch(e){
		return true;
		
	}
	
};

sc.swf.cleanup = function() {
	sc.$('object').forEach(function(obj){
		obj.style.display='none';
		for(var prop in obj){
			if(typeof obj == 'function'){obj[prop] = function(){};}
		}
	});
};

sc.swf.unload = function() {
	__flash_unloadHandler = function(){};
	__flash_savedUnloadHandler = function(){};
	window.attachEvent( "onunload", sc.swf.cleanup );
	
};
	
sc.swf.detect = function(){
	if (navigator.plugins && navigator.plugins.length){
		sc.swf.format = 'embed';
		return sc.swf.check();
	} else {
		sc.swf.format = 'object';
		return sc.swf.ieCheck();
	}
};

sc.swf.detect();

sc.sharedObject = {
	remember :function(key, v){
	
		try{
			sc.sharedObject.box.remember(key,escape(v));
		} catch(e){
			window.setTimeout(function(){sc.sharedObject.remember(key,v);}, 1000);
		}
	},
	
	recall : function(key){
		try{
			var val = unescape(sc.sharedObject.box.recall(key));
			if(val == 'null'){return false;} else {return val;}
		} catch(e){return false;}
	},
	
	forget : function(key){
		try{
			sc.sharedObject.box.forget(key);
		} catch(e){return false;}
		return true;
	}
};

sc.persistentData = {
	remember : function(name, value){
		sc.cookies.remember(name, value);
		if(sc.cookies.recall(name) != value){
			
			sc.sharedObject.remember(name, value);
		}
	},

	recall : function(name){
		
		if(sc.cookies.recall(name)){
			return sc.cookies.recall(name);
		} else {
			return sc.sharedObject.recall(name);
		}
	},

	forget : function(name){
		if(sc.cookies.recall(name)){
			sc.cookies.forget(name);
		}
		
		if(sc.sharedObject.recall(name)){
			sc.sharedObject.forget(name);
		}
	}
};


sc.sound = function(url){
	this.url = url || '';
	this.trackId = sc.sound.trackId;
	this.percent=0;
	this.playing=0;
	sc.sound.log(this);
	sc.sound.trackId++;
};

sc.sound.errors =0;

sc.sound.prototype = {
	title : '',
	artist : '',
	length : '',
	cookieName : 'demo',
	vol : sc.sound.globalVolume,
	pos: '',
	sounds : sc.sound.sounds,
	
	play : function(vol, pos){
		var t=this;
		this.playing=0;
		if(this.fademe !==undefined){this.fademe.end();}
		if(sc.sound.mute !=1){
			vol = vol || sc.sound.globalVolume;
			try{
				sc.sound.player.playSound(this.trackId, this.url, vol, pos);
			}catch(e){
				if(sc.sound.errors <5){
					window.setTimeout(function(){t.play(vol, pos);}, 100);
					sc.sound.errors++;
					
				} else {
					sc.consol.error(this.url+': '+sc.messages[12]);
				}
				return false;
			}
			
			if(typeof this.onplay =='function'){this.onplay();}
			this.playing=1;
			
			this.vol = vol;
		
		}
	},
	
	stop : function(){
		try{
			sc.sound.player.stopSound(this.trackId);
			this.playing=0;
		
		}catch(e){}
	},
	
	volume : function(vol){
		try{
			if(arguments.length ===0){
				
				return sc.sound.player.changeVolume(this.trackId);
			} else {
			
				sc.sound.player.changeVolume(this.trackId, vol);
			}
		}catch(e){}
	},
	
	pan : function(pan){
		try{
			if(arguments.length ===0){
				
				return sc.sound.player.changePan(this.trackId);
			} else {
				pan = (pan > -100) ? pan : -100;
				pan = (pan < 100) ? pan : 100;

				sc.sound.player.changePan(this.trackId, pan);
			}
		}catch(e){}
	},
	
	position : function(percent){
		var t=this;
		try{
			
			if(arguments.length ===0){
				percent = sc.sound.player.position(this.trackId);
				return sc.math.round(percent, 2);
			} else {
				percent = (percent >= 0) ? percent : 0;
				percent = (percent <= 100) ? percent : 100;
				this.percent = percent;
				
				sc.sound.player.position(this.trackId, percent);
				
			}
		}catch(e){
			
			return false;
		}
		
		return true;
	},
	
	setInfo : function(info){
		for(var prop in info){
			this[prop] = info[prop];
		}
		if(typeof this.onload == 'function'){
			this.onload();
		}
		if(typeof this.displayID3 == 'function'){
			this.displayID3(info);
		}
	},
	
	fadeout : function(interval){
		var t=this;
		interval = interval || 20;
		this.fademe = new sc.timer(function(){
			var newVol;
			if(typeof t.volume =='function'){
				if(t.volume() >= 0){
					newVol = t.volume()-t.volume()/20;
					t.volume(newVol);
					if(t.volumeSlider !== undefined){
						t.volumeSlider.valueToPos(newVol, 1);
					}
						
				} else {
					t.stop();
					this.end();
				}
			}
		}, interval);
		this.fademe.begin();
	},
	
	fadein : function(interval){
		var t=this;
		interval = interval || 20;
		this.fademe = new sc.timer(function(){
			if(typeof t.volume =='function'){
				var newVol;
				if(t.volume() <= this.vol){
					newVol = t.volume()+(this.vol/20);
					t.volume(newVol);
					if(t.volumeSlider !== undefined){
						t.volumeSlider.valueToPos(newVol, 1);
					}
				} else {
					this.end();
				}
			}
		}, interval);
		this.fademe.begin();
	},
	
	attach : function(obj, evt){
		var t=this;
		return sc.events.add(sc.$(obj), evt, function(e){sc.events.preventDefault(e);t.play();});
	},
	
	detach :function(evt){
		sc.events.remove(evt);
	}
};

sc.sound.mute =0;

sc.sound.stopAll = function(){

		for(var sound in this.sounds){
			if(this.sounds[sound].stop){
				this.sounds[sound].stop();
			}
		}
};

sc.sound.setInfo = function(info){
		
	if(typeof info.x !==undefined){
	
		this.sounds[info.x].setInfo(info);
	}
	
};

sc.sound.trackId=0;
sc.sound.sounds = [];
sc.sound.globalVolume = 20;
sc.sound.log = function(trackId){
	if(!sc.arrays.inArray(this.sounds, trackId)){
		this.sounds.push(trackId);		
	}
};

sc.sound.trackComplete = function(x){
	if(typeof this.sounds[x].oncomplete == 'function'){
		this.sounds[x].oncomplete();
	}
};
sc.sound.setDuration = function(x, duration){

		this.sounds[x].milliseconds = duration;
		this.sounds[x].seconds = duration/1000;
		this.sounds[x].minutes = sc.math.round(duration/60000, 2);
		this.sounds[x].length = this.sounds[x].minutes;
};

sc.ln = function(i, o){
	
	if(!window[i] && o!==null){
		window[i] = o;
	} else {
		sc.consol.error('Cannot assign window.'+i+': '+o+' does not exist');
	}
};

sc.debugMe = function(){
	sc.debug =1;
	//check to see if spacer.gif
	sc.spacer = sc.ce('img');
	sc.spacer.src =sc.locations.spacerGif;
	sc.spacer.onerror=function(){
		
		sc.consol.error(sc.messages[6]);
	};
	
	//check to see if sc.swf file exists
	sc.fileExists(sc.locations.swf, function(){
		
			sc.consol.error(sc.messages[7]);
	});	
	
	//list all cookies that are stored
	sc.consol.log(sc.messages[8]+sc.cookies.listAll());
	sc.consol.log('window.width='+sc.browser.w+"\n"+'window.height='+sc.browser.h);
	sc.consol.log('agent='+sc.browser.agent+"\n"+'version='+sc.browser.version);
	if(scNoFlash ===1){
		sc.consol.warning(sc.messages[11]);
	} else {
		if(sc.swf.version===0){
			sc.consol.error(sc.messages[10]);
		} else {
			sc.consol.log('flashPlayer='+sc.swf.version);
		}
	}
		
};

sc.initialized =0;
sc.init = function(){
	if(sc.initialized ===0){
		
		sc.initialized=1;
		
		if(scDebug ===1){sc.debug =1;}
		
		if(scNoFlash!==1){
			//instantiate flash communication object
			var mySwfBox = sc.dom.ce('span');
			
			mySwfBox.id = 'sc_logo';
			window.document.body.appendChild(mySwfBox);
			
			sc.swfBox = new sc.swf(sc.locations.swf, 57, 14, '#FFAC00');
			sc.swfBox.id = 'sc_sticker';
			sc.swfBox.lnk = sc.swfBox.embed('sc_logo');
			sc.sharedObject.box = sc.swfBox.lnk;
			sc.sound.player = sc.swfBox.lnk;
		
			mySwfBox = null;
		}
		
		if(typeof scInitHandler != 'undefined'){
			scInitHandler();
			
		}
		if(sc.debug ===1){
			sc.debugMe();
		}
	}
};

sc.messages = {
	1:'This browser does not support surebert, please visit again with firefox, ie 5.5-7 for win, safari, netscape or opera.',
	2:'Page not found. status codes explained at http://surebert.com/errorCodes.html ',
	3:'page found but blank',
	4:'invalid xml',
	5:'log sent',
	6:'spacerGif missing - make sure it is in the sc baseFolder and that the sc basefolder location has been specified if it is other than ../sc',
	7:'sc.swf missing - - make sure it is in the sc baseFolder and that the sc basefolder location has been specified if it is other than ../sc.',
	8:'cookies found: ',
	9:'Object is not a DOM object or not yet inserted into the DOM.  Thus it has no offsetParent, x, y, left, top, bottom ,or right position, etc!',
	10 :"FlashPlayer not installed or not detected.  You will need at least Flash play 8 to use sc's flash functionality\n GET FLASH PLAYER: http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash",
	11 :"You have disabled flash functionality. i.e. sound and flash shared object storage space access by setting the scNoFlash=1 before calling surebert.",
	12 :"Surebert has tried to play this track 5 times with no sucess, perhaps you have flash functionality disabled or the sound does not exist.",
	13 : "Data has arrived at the destination url"
};

sc.attachPrototype = function(obj, prop, scMod){
	obj.prototype[prop] = function() {
		var a = [this];
		sc.arrays.forEach(arguments, function(v){a.push(v);});
		return scMod[prop].apply(this, a);
	};
};

sc.attachProp = function(obj, prop, scMod){
	obj[prop] = function() {
		return scMod[prop](this, arguments[0], arguments[1]);
	};
};

sc.setGlobals = function(){
	var prop;
	for(prop in sc.strings){
		sc.attachPrototype(String, prop, sc.strings);	
	}
	
	for(prop in sc.arrays){
		sc.attachPrototype(Array, prop, sc.arrays);	
	}
	
	//add global links to internal sc functions
	sc.globals = {
		$ : sc.dom.$,
		s$ : sc.dom.s$,
		toggle : sc.dom.toggle,
		setTitle : sc.dom.setTitle,
		browser : sc.browser,
		txt : sc.dom.txt,
		create : sc.dom.create,
		ce : sc.dom.ce,
		getBounds : sc.dom.getBounds,
		remove: sc.dom.remove,
		replace: sc.dom.replace,
		insertBefore : sc.dom.insertBefore,
		insertAfter : sc.dom.insertAfter,
		importNode : sc.dom.importNode,
		image : sc.dom.image,
		input : sc.forms.input,
		remember: sc.persistentData.remember,
		recall: sc.persistentData.recall,
		forget: sc.persistentData.forget,
		timer: sc.timer,
		wait: sc.timer.wait,
		cancel: sc.timer.cancel,
		px: sc.strings.px,
		datestamp: sc.dates.date,
		swf: sc.swf,
		removeSelection: sc.browser.removeSelection,
		win: sc.browser.win,
		ajax: sc.ajax,
		surebert: sc.ajax,
		cookies: sc.cookies,
		events: sc.events,
		stylify: sc.stylify,
		consol: sc.consol,
		objDump: sc.objects.dump,
		sound : sc.sound,
		arrays : sc.arrays,
		strings : sc.strings,
		isArray : sc.isArray
	};
	
	for(var g in sc.globals){
		sc.ln(g, sc.globals[g]);
	}
	
	surebertCom = sc;
};

if(typeof scNoGlobals === 'undefined'){
	sc.setGlobals();
}
if(!document.importNode){
	document.importNode = sc.dom.importNode;
}

if(sc.browser.agent =='ie' && sc.swf.version >=9){
	//cleanup flash players for IE
	window.attachEvent( "onbeforeunload", sc.swf.unload);
}

/* for Mozilla */
if (document.addEventListener) {
   document.addEventListener("DOMContentLoaded", sc.init, false);
}

// for Internet Explorer (using conditional comments)
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
    if (this.readyState == "complete") {
        sc.init(); // call the onload handler
    }
};
/*@end @*/
/*
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var sc_timer = setInterval(function() {
    	if(document.readyState.match(/loaded|complete/)){
           clearInterval(sc_timer);
           sc.init(); // call the onload handler
        }
    }, 100);
}
*/

/* for other browsers */
sc.events.add(window, 'load', function(){if(sc.initialized===0){sc.init();}});
sc.events.add(window, 'resize', sc.browser.measure);
sc.events.add(window, 'scroll', sc.browser.scrollPos);
sc.events.add(window, 'unload', function(){
	sc.events.removeAll();
});
