var AE = window.AE || {};

document.observe('dom:loaded',function(){
	//ready the login form...
	AE.initFancyLogin();

	AE.initMainColumns();
	
	AE.initTracking();
	
	AE.initBottomToolbar();
});

AE.trackEvent = function(eventName, targetUrl) {
	
	var these_events = ['video-start', 'video-stop', 'video-pause', 'video-resume', 'video-complete', 'video-dim-lights', 'video-raise-lights', 'video-time-code-scroll', 'video-session', 'video-editors-pick', 'flash-carousel', 'registration-complete', 'login-complete', 'search-bar-click'];
	
	eventUrl = window.location.href.replace(/[\?#].*$/,'');
	eventUrl = eventUrl.replace(/\/*$/, '');
	if (eventName) {
		if (targetUrl) {
			targetUrl = targetUrl.replace(/^(https?:\/\/[^\/]+)?\//i, '');
			eventName += '/'+targetUrl;
		}
		these_events.each(function(e)
		{
			if (eventName.match(e))
			{
				eventName = "event/" + eventName;
			}
		});
		
		if (eventName.match(/^search\/\?q=/))
		{
			eventUrl = eventName;
		}
		else
		{
			eventUrl = eventUrl+"/"+eventName;
			eventUrl = eventUrl.replace(/\/+/, '/');
		}

		try {
			pageTracker._trackPageview(eventUrl);
		}
		catch (err) {}
		// console.log('tracking: '+eventUrl);
	}
}

AE.initFancyLogin = function () {
    if ($('header-login-link')) {
        Event.observe('header-login-link', 'click', function (e) {
            Event.stop(e);
            elm = e.target;
            $('login-item').hide();
            $('create-account-item').hide();
            $('header-login-form').show();
            $('password-lookup').show();
        });
        Event.observe('header-login-cancel-link', 'click', function (e) {
            Event.stop(e);
            $('login-item').show();
            $('create-account-item').show();
            $('header-login-form').hide();
            $('password-lookup').hide();
        });
    }
}

AE.initMainColumns = function() {
	var els = $$('div.col-main ol li');
	if(els) {
		var maxHeight = 0;
		els.each(function(el) { if(el.getHeight()>maxHeight) maxHeight = el.getHeight(); });
		els.each(function(el) { el.setStyle({height: maxHeight+'px'}); });
	}
}

AE.initTracking = function() {
	var els = $$('div#editors-picks div.inner ol li');
	if (els) {
		els.each(function(el){
			var tmp = new Array();
			tmp.push(el.down('a.thumb'));
			tmp.push(el.down('h3 a'));
			tmp.each(function(elm){
				Event.observe(elm, 'click', function(e){
					Event.stop(e);
					AE.trackEvent('video-editors-pick', elm.href);
					window.location.href=elm.href;
				});
			});
		});
	}

	els = $$('div.results-nav div.tabs ul li a');
	if (els) {
		els.each(function(el){
			Event.observe(el, 'click', function(e){
				Event.stop(e);
				AE.trackEvent('menu-'+el.innerHTML.toLowerCase());
				window.location.href=el.href;
			});
		});
	}
}

AE.initBottomToolbar = function() {
	Event.observe('bottom-email-link','click',function(e) {
		Event.stop(e);
		elm = e.target;
		AE.hideBottomTools('bottom-email-form');
		new Effect.toggle('bottom-email-form','Appear',{queue:'end', duration:.1});
	});
	Event.observe('bottom-share-link','click',function(e) {
		Event.stop(e);
		elm = e.target;
		AE.hideBottomTools('bottom-share');
		new Effect.toggle('bottom-share','Appear',{queue:'end', duration:.1});
	});
	$$('img.close-bottom').each(function(elm) {
		Event.observe($(elm),'click',function(e) {
			Event.stop(e);
			AE.hideBottomTools();
		});
	});
	
}
AE.hideBottomTools = function(skipElm) {
	$$('div.bottom-form').each(function(elm) {
		if(elm.id != skipElm) {
			new Effect.Fade(elm, {queue:'front', duration:.1});
		}
	});
}

