$( function () {
	var GTIMER, GSTOPPER;

	$('#topMainImages li').each( function (i) {
		$(this).attr( 'id', 'topMainImageItem' + i );
		$('#topMainBtnUnit').append( '<li class="topimgOff" id="topMainBtnItem' + i + '"><a href="#"></a></li>' );
	} );

	$('#topMainBtnUnit a').click( function () {
		try { this.blur(); } catch(de) {}

		if (GSTOPPER) {
			return false;
		}
		var e = $(this).parent();
		if (e.hasClass('topimgOn')) {
			return false;
		}
		if (GTIMER) {
			clearTimeout(GTIMER);
			GTIMER = null;
		}

		$('#topMainBtnUnit .topimgOn').removeClass('topimgOn').addClass('topimgOff');
		var tbx = e.removeClass('topimgOff').addClass('topimgOn').attr('id').replace('topMainBtnItem','topMainImageItem');

		GSTOPPER = true;
		$( '#' + tbx ).hide().css( { zIndex: 2, top: 0, left: 0 } ).prependTo('#topMainImages');
		$('#topMainImages li').slice(2).hide();
		$('#topMainImages li').first().next().css( { zIndex: 1, top: 0, left: 0 } );
		$('#topMainImages li').first().fadeIn( 'slow', function () {
			GSTOPPER = null;
			GTIMER = setTimeout( function () {
				var e = $('#topMainBtnUnit .topimgOn').next();
				if (!e.length) {
					e = $('#topMainBtnUnit li').first();
				}
				e.find('a').trigger('click');
			}, 10000 );
		} );

		return false;
	} );

	$('#topMainArrowL').click( function () {
		try { this.blur(); } catch(de) {}

		var e = $('#topMainBtnUnit .topimgOn').prev();
		if (!e.length) {
			e = $('#topMainBtnUnit li').last();
		}
		e.find('a').trigger('click');

		return false;
	} ).parent().css('visibility','visible');

	$('#topMainArrowR').click( function () {
		try { this.blur(); } catch(de) {}

		var e = $('#topMainBtnUnit .topimgOn').next();
		if (!e.length) {
			e = $('#topMainBtnUnit li').first();
		}
		e.find('a').trigger('click');

		return false;
	} ).parent().css('visibility','visible');

	$('#topMainBtnUnit li').first().find('a').trigger('click');
} );

