/**
 * JS file for Acadam Buttons
 *
 * @author Gabriel Lemonde-Labrecque
 * @copyright 2010, SchoolSuccess inc.
 */

if (typeof TC.html == 'undefined') {
	TC.html = {};
}
 
if (typeof TC.html.buttons == 'undefined') {
	TC.html.buttons = {
		options: {
			hyperlinkClass : 'button',
			
			activeButtonClass : 'button_active',
			
			states : {
				outer : {
					active : {
						backgroundPosition : 'bottom left'
					}
					,inactive : {
						backgroundPosition : 'top left'
					}
				}
				,inner : {
					active : {
						backgroundPosition : 'bottom right'
					}
					,inactive : {
						backgroundPosition : 'top right'
					}
				}
			},
			
			iphone: {
				replaceButtons : true
			}
		},
		
		buttons: [],
		
		iphone: false,
		
		init: function(options) {
			for (var property in options) {
				this.options[property] = options[property];
			}
			
			this.iphone = (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i));
			
			this.process(document.getElementsByTagName('button'), false);
			this.process(document.getElementsByTagName('a'), true);
			
			if (this.iphone && this.options.iphone.replaceButtons) {
				this.remove();
			}
		},
		
		process: function(elements, links) {
			var linkTest = new RegExp('\\b' + this.options.hyperlinkClass + '\\b');
			
			for (var a = 0; a < elements.length; a++) {
				if ((links && linkTest.test(elements[a].className)) || !links) {
					if (this.iphone && !links) {
						this.mobile(elements[a]);
					}
					else {
						this.disable(elements[a]);
						this.setup(elements[a]);
					}
					
					if (!links) {
						this.buttons.push(elements[a]);
					}
				}
			}
		},
		
		mobile: function(element) {
			var input = document.createElement('input');
			input.setAttribute('type', element.getAttribute('type') == 'submit' ? 'submit' : 'button');
			
			var attributes = new Array('id', 'name', 'value', 'class', 'onclick', 'onmouseover', 'onmouseout', 'onpress', 'onfocus', 'onblur', 'onmouseup', 'onmousedown');
			
			for (var a = 0; a < attributes.length; a++) {
				if (element.getAttribute(attributes[a])) {
					input.setAttribute(attributes[a], element.getAttribute(attributes[a])); 			
				}
			}
			
			input.style.marginLeft = element.style.marginLeft;
			input.style.marginRight = element.style.marginRight;
			
			element.parentNode.insertBefore(input, element);
		},
		
		remove: function() {
			for (var a = 0; a < this.buttons.length; a++) {
				this.buttons[a].parentNode.removeChild(this.buttons[a]);
			}
		},
		
		disable: function(element) {
			element.onselectstart = function() { return false; };
			element.style.MozUserSelect = 'none';
			element.style.KhtmlUserSelect = 'none';
			element.style.UserSelect = 'none';
			element.style.cursor = 'default';
		},
		
		setup: function(element) {
			if (document.all) {
				if (element.tagName == 'BUTTON') {
				  element.attachEvent('onfocus', this.bind(this.toggle, this, element));
				}
				else {
				  element.attachEvent('onmousedown', this.bind(this.toggle, this, element));
				}
				element.attachEvent('onmouseup', this.bind(this.toggle, this, element));
			}
			else {
				element.onfocus = function() { this.blur(); };
			}
		},
		
		toggle: function(o, element) {
			if (element.tagName != 'BUTTON' && element.tagName != 'A') {
				while (element.tagName != 'A')
				{
				  element = element.parentNode;
				}
			}
			if (event.type == 'focus' || event.type == 'mousedown') {
				element.className += ' ' + o.options.activeButtonClass;
				o.style(element.childNodes[0], o.options.states.inner.active);
				o.style(element.childNodes[0].childNodes[0], o.options.states.outer.active);
				element.blur();
			} 
			else {
				element.className = element.className.replace(o.options.activeButtonClass, '');
				o.style(element.childNodes[0], o.options.states.inner.inactive);
				o.style(element.childNodes[0].childNodes[0], o.options.states.outer.inactive);
			}
		},
		
		style: function(element, styles) {
			for (var property in styles) {
				element.style[property] = styles[property];
			}
		},
		
		bind : function(func) {
			var args = [];
			for (var a = 1; a < arguments.length; a++) {
				args.push(arguments[a]);
			}
			return function() { return func.apply(this, args); };
		}
	};
}
 
$(document).ready(function() {
	var buttons = $('a.grey');
	buttons.wrapInner('<span><span></span></span>');
	
	buttons.each(function() {
		if ($(this).hasClass('download'))
			$(this).find('span span').prepend('<img style="border:none;" src="/images/icons/download.png" align="absmiddle" width="16" height="16" alt="download" />&nbsp;');
		if ($(this).hasClass('cancel'))
			$(this).find('span span').prepend('<img style="border:none;" src="/images/icons/cancel.png" align="absmiddle" width="11" height="11" alt="cancel" />&nbsp;');
		if ($(this).hasClass('ok'))
			$(this).find('span span').prepend('<img style="border:none;" src="/images/icons/accept.png" align="absmiddle" width="11" height="11" alt="ok" />&nbsp;');
		if ($(this).hasClass('add'))
			$(this).find('span span').prepend('<img style="border:none;" src="/images/icons/add.png" align="absmiddle" width="11" height="11" alt="add" />&nbsp;');
		if ($(this).hasClass('find'))
			$(this).find('span span').append('&nbsp;<img style="border:none;" src="/images/icons/magnifying.png" align="absmiddle" width="11" height="11" alt="find" />');
	});
});


