$(document).addEvent('click', function(event) {
	event=new Event(event);
	var el=$(event.target);

	if ('button'==event.target.getTag()) {
		if (event.target.hasClass('add')) {
			buttonAdd(event);
		} else if (event.target.parentNode
			&& $(event.target.parentNode).hasClass('controls')
		) {
			if (event.target.hasClass('up')) {
				buttonUp(event);
			} else if (event.target.hasClass('down')) {
				buttonDown(event);
			} else if (event.target.hasClass('del')) {
				buttonDel(event);
			}
		}
	}
});


var tbApiCookie=1;
var gAtManage=0;

// \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ //

window.addEvent('domready', function() {
	try {
		var manageLink=$E('.secondaryNav #nav_manage');
		if (!sitepass.passed) manageLink.style.display='none';
	} catch (e) { }
});

// \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ //

// Track clicks on add/remove buttons for SiteCatalyst.
function clickTrack(element, opts) {
	// checking if opts is valid
	if (
		'undefined'==typeof(opts.prop) ||
		'undefined'==typeof(opts.eVar) ||
		'undefined'==typeof(opts.event)
	) {
		// problem w/ opts.
		return false;
	}
	
	var account='mivaalotcom';
	if (typeof(env)!='undefined' && 'home'==env) {
		account='mivahomealotcom';
	}
	
	// Check, because it's never defined in dev, when sitecat is excluded.
	if ('undefined'!=typeof s_gi) {
		// Cache a few generated values used below.
		var prop='prop'+opts.prop;
		var evar='eVar'+opts.eVar;
		var event='event'+opts.event;

		// Use those values to do SiteCatalyst tracking of the event.
		var s=s_gi(account);
		s.linkTrackVars=prop+','+evar+',events'; 
		s.linkTrackEvents=s.events=event; 
		s[prop]=s[evar]=element.getAttribute('name');
		s.tl(element, 'o', s[prop]);
	}

	return true;
}

// \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ //

// Use setTimeout to run after gAtManage might be set.
setTimeout(function() {
	if ('ie'==sitepass.platform) {
		SHOT$$.OnDownloadComplete=catchButtonEvent('add');
		SHOT$$.OnMove=catchButtonEvent('move');
		SHOT$$.OnRemove=catchButtonEvent('remove');
	} else if ('ff'==sitepass.platform) {
		// moo methods do not work here!
		window.addEventListener('AlotButtonAdd', catchButtonEvent('add'), false);
		window.addEventListener('AlotButtonMove', catchButtonEvent('move'), false);
		window.addEventListener('AlotButtonRemove', catchButtonEvent('remove'), false);
	}
}, 0);

function catchButtonEvent(event) {
	if (gAtManage) {
		return showButtons;
	} else {
		return function(){};
	}
}

function checkToolbar() {
	if (location.pathname.match(/^\/error/)) return;

	if (!sitepass.passed) location.replace('/error?notb');
	if ( ( 'ie'==sitepass.platform && 2.2>parseFloat(sitepass.version, 10) ) ||
		 ( 'ff'==sitepass.platform && 2.0>parseFloat(sitepass.version, 10) )
	) {
		location.replace('/error?tbver');
	}
}

// \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ //

function addDelLog(addDel, buttonId) {
	new Json.Remote('/buttons/addDelLog').send({
		'addDel':addDel,
		'button':buttonId,
		'client':sitepass.client_id,
		'source':sitepass.src_id
	});
}

// \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ //

function buttonAdd(event) {
	checkToolbar();

	var el=$(event.target);

	var m=el.id.match(/button_(\d+)_(.+)/);
	var id=m[1];
	var version=m[2];
	var urlBase='update.alot.com/1/update/button_configs/button_config_';

	try {
		if ('ie'==sitepass.platform) {
			SHOT$$.add(
				id, version, 'http://'+urlBase+id+'_1.xml',
				1, tbApiCookie++
			);
		} else if ('ff'==sitepass.platform) {
			sitepass.api.Custom.add(
				id, version, 'https://'+urlBase+id+'_1_plaintext.xml',
				1, tbApiCookie++
			);
		}

		addDelLog(1, id);
	} catch (e) {
	}
	
	
	// --- siteCatalyst ---
	
	// if #featured exists and 'this' is a child
	if ($('featured') && $('featured').hasChild(el)) { 
		var opts={
			'prop':'23',
			'eVar':'22',
			'event':'16'
		}
	} else { // regular 'add me' button
		var opts={
			'prop':'10',
			'eVar':'10',
			'event':'10'
		}
	}
	clickTrack(el, opts);
}

// \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ // // \\ //


window.addEvent('domready', function () {
	var searchEl=$E('form input[name=q]');
	if (!searchEl) return;

	var hintText=$defined(window.env) && 'home'==window.env ?
		"(ex: News and Headlines)" :
		"Search for ALOT Buttons here";

	function addHint() {
		// Only put the hint in an empty box.
		if (''!=searchEl.getValue()) return;

		searchEl.setProperty('value', hintText);
	}

	function removeHint() {
		// Only remove the value if it is the hint.
		if (hintText!=searchEl.getValue()) return;

		searchEl.setProperty('value', '');
	}

	// Add the hint, now.
	addHint();
	// Remove it whenever the element gets focus, or the form is submitted.
	searchEl.addEvent('focus', removeHint);
	$(searchEl.form).addEvent('submit', removeHint);
	// Put it back when the element loses focus.
	searchEl.addEvent('blur', addHint);
});


