var $ = _$ = jQuery;

function apaslt09_home_news_load(pos) {
	var _$item = _$('#newsItems li').eq(pos);
	
	// Prepare Data
	var image = _$item.find('.image').html();
	var desc = _$item.find('.description .text').text();
	var moreLink = _$item.find('.description .readMore a').attr('href');
	
	// Don't wrap "SL-Tech" in title
	var title = _$item.find('.title').text().replace(/SL-Tech(\'s)?/, '<span class="nowrap">SL-Tech$1</span>');
	
	// Empty
	_$('#hero div.img, #hero div.text').empty();

	// Insert
	_$('#hero div.img').append(image);
	_$('#hero div.text')
		.append('<h3><a href="' + moreLink + '">' + title + '</a></h3>')
		.append('<p>' + desc + '</p>')
		.append('<p><a href="' + moreLink + '">Read more ...</a></p>');
	
	// Adjust buttons
	_$('#hero .buttons a.next').unbind('click').click(function() {
	    if(pos < (_$('#newsItems li').length-1)) {
	        apaslt09_home_news_load(pos+1);
	    }
	    else {
	        apaslt09_home_news_load(0);
	    }
	    return false;
	});
	_$('#hero .buttons a.previous').unbind('click').click(function() {
	    if(pos > 0) {
	        apaslt09_home_news_load(pos-1);
	    }
	    else {
	        apaslt09_home_news_load(_$('#newsItems li').length-1);
	    }
	    return false;
	});
	
	// Show
	_$('#hero').removeClass('loading');
}

function apaslt09_home_ready() {
	// Loading animation
    _$('#hero').addClass('loading').find('div').empty();
	
	// Sidebar form(s)
	apaslt09_inline_labels( _$('#sidebar div.form') ); 
	_$('#sidebar div.form').jNice({
		'enableTextFields': false
	});
	_$('input:text, input:password', '#sidebar div.form').addClass('jNiceInput').wrap('<div class="jNiceInputWrapper jNiceInputFixed"><div class="jNiceInputInner"><div></div></div></div>');
	_$('#supportLoginSubmit').click(function() {
		var formAction = 'http://support2.sl-tech.net/SupportCenter/logon.asp';
		document.forms[0].encoding = 'application/x-www-form-urlencoded';
		_$('body > form').submit();
		return false;
	});
	_$('fieldset.tech').append('<input type="hidden" name="return_page" value="http://www.sl-tech.net/" />');

		var formAction = 'http://support2.sl-tech.net/SupportCenter/logon.asp';
	if( (document.location.pathname.indexOf('login.aspx') == -1) && (document.location.pathname.indexOf('Home/') == -1) )
	{
		_$('#__VIEWSTATE, #__EVENTTARGET, #__EVENTARGUMENT, #ScrollTop, #__dnnVariable').remove();
	
	_$('body > form')
			.attr('action', formAction)
			.attr('method','POST')
			.attr('enctype', 'application/x-www-form-urlencoded')
			.removeAttr('id')
			.removeAttr('style')
			.removeAttr('autocomplete')
			.removeAttr('name');
	}
	if (document.location.pathname.indexOf('login.aspx') != -1)
	{
		
		_$('#dnn_ctr_Login_Login_DNN_txtPassword').click(function(){
			_$('body > form')
				.attr('action', document.location.pathname)
				.attr('method','POST')
				.attr('enctype', 'multipart/form-data')
				.attr('id', 'Form')
				.attr('name', 'Form')
				.attr('autocomplete','off')
				.attr('style','height: 100%;')
			_$('body').append('<input type="#_EVENTTARGET" value="dnn$ctr$Login$Login_DNN$cmdLogin">');
		});
		_$('.jNiceInputInner').click(function(){
			_$('body > form')
					.attr('action', 'http://support2.sl-tech.net/SupportCenter/logon.asp')
					.attr('method','POST')
					.attr('enctype', 'application/x-www-form-urlencoded')
					.removeAttr('id')
					.removeAttr('style')
					.removeAttr('autocomplete')
					.removeAttr('name');
			_$('#__EVENTTARGET').remove;
		});
	}
	_$('input', '#sidebar .form fieldset:first').keydown(function(event) {
		if (event.keyCode == 13) {
			_$('#supportLoginSubmit').trigger('click');
			return false;
		}
	});
	
	// Sidebar password field
	_$('input:password', '#sidebar div.form').each(function() {
		var _$password = _$('<input type="text" value="Password" class="pass_text" />').focus(function() {
			_$(this).hide().siblings('input[type=password]').show().trigger('focus');
		});
		_$(this).after(_$password);
		_$(this).blur(function() {
			_$(this).hide().siblings('input.pass_text').show();
		});
		_$(this).trigger('blur');
	});
	
	// Sidebar IE
	if( _$.browser.msie ) {
		_$('#sidebar fieldset ul li.clear').prepend('<br/>');
	}

    // Check for login form
    if (_$('#dnn_ctr_Login_pnlLogin').length) {
        _$('#hero, #columns').hide();
        _$('#notHome').css('padding', '20px');
    }
    else {
        // Load all news items
        _$('#newsItems').load('/Home/News/NewsFrontpage/tabid/98/Default.aspx #newsList table.ArticleList tr:not(:last-child) td', function() {
			// Transform to list
			_$('#newsItems td').each(function() {
				var tmpHtml = _$(this).html();
				_$(this).after('<li>' + tmpHtml + '</li>');
				_$(this).remove();
			});
			
			// Add buttons
			_$('#newsItems').before('<div class="buttons"><a href="#" class="previous"></a><a href="#" class="next"></a></div>');
		
            // Select random item
            var max = _$('#newsItems li').length;
            var num = Math.floor((Math.random() * max));
            apaslt09_home_news_load(num);
        });
    }
}

_$(document).ready(function() {
    apaslt09_home_ready();
});