var Site = {

	Init: function() {
		Site.ImageRotater();
		Site.Editor();
		Site.Reorder();
		Site.SetFocus();
		Site.SiteTips();
	},

	SetFocus: function() {
		if ($('txtUsername')) $('txtUsername').focus();
		if ($('txtCode')) $('txtCode').focus();
		if ($('txaData')) $('txaData').focus();
		if ($('txaQuestion')) $('txaQuestion').focus();
		if ($('txtHeadline')) $('txtHeadline').focus();
		if ($('txtTitle')) $('txtTitle').focus();
		if ($('txtFirstName')) $('txtFirstName').focus();
		
		if (($('frmProductCategory') || $('frmProduct')) && $('sltGoto')) {
			$('sltGoto').addEvent("change", function(e) {
				location.href = this[this.selectedIndex].value;
			});
		}
		
		if ($('frmTrainingServiceCategory') && $('sltParentID')) {
			if ($('sltParentID').getSelected()[0].value == 0) $('trainingServiceCategoryDescription').setStyle('display', 'none');
			
			$('sltParentID').addEvent("change", function(e) {
				$('trainingServiceCategoryDescription').setStyle('display', ((this[this.selectedIndex].value == 0) ? 'none' : ''));
			});
		}
				
	}, 

	ImageRotater: function() {
		if ($(document.body).getElement('div.imageRotater')) {
			var scroller = new ContentScroller({slideDuration: 4500, fadeDuration: 500});
			scroller.addSlides($$('div.imageRotater img').setStyle('z-index', 1));
			scroller.start();
		}
	},	

	ChangeImage: function (element, image, title) {
		if ($(element)) {
			$(element).src = image;
			$(element).title = title;		
		}
	},
	
	Reorder: function() {
		if ($('itemList')) {
			var sortableLists = new Sortables($('itemList'), {
				clone: true,
				handle: '.handle', 
				revert: {
					duration: 50
				},
				opacity: .1,
				onStart: function(el){
					el.highlight('#F3F865');	
				},
				onSort: function(el) {
				},
				onComplete: function(el) {
					$('hdnPostionIDs').value = this.serialize(0); 
				}
			}).attach();
		}
	},
	
	Editor: function() {
		var textarea = '';

		if ($('txaDescription')) textarea = 'txaDescription';
		if ($('txaContent')) textarea = 'txaContent';
		if ($('txaArticle')) textarea = 'txaArticle';
		
		if (textarea != '') var editor = new Editor($(textarea), { url: '/images/editor/' });	
		
		if ($('frmTrainingService')) {
			var edtAimedAt = new Editor($('txaAimedAt'), { url: '/images/editor/' });
			var edtDuration = new Editor($('txaDuration'), { url: '/images/editor/' });			
			var edtDuration = new Editor($('txaPrerequisites'), { url: '/images/editor/' });
			var edtDuration = new Editor($('txaStandard'), { url: '/images/editor/' });
			var edtDuration = new Editor($('txaTrainees'), { url: '/images/editor/' });
			var edtDuration = new Editor($('txaTrainers'), { url: '/images/editor/' });
			var edtDuration = new Editor($('txaRequirements'), { url: '/images/editor/' });
			var edtDuration = new Editor($('txaRefreshments'), { url: '/images/editor/' });
			var edtDuration = new Editor($('txaEquipment'), { url: '/images/editor/' });
			var edtDuration = new Editor($('txaNotes'), { url: '/images/editor/' });			
		}
	},
	
	FillingDeliveryAddress: function() {
		$('txtDeliveryFirstName').value = $('txtBillingFirstName').value;
		$('txtDeliveryLastName').value = $('txtBillingLastName').value;
		$('txtDeliveryAddress1').value = $('txtBillingAddress1').value;
		$('txtDeliveryAddress2').value = $('txtBillingAddress2').value;
		$('txtDeliveryTownCity').value = $('txtBillingTownCity').value;
		$('txtDeliveryCounty').value = $('txtBillingCounty').value;
		$('txtDeliveryPostcode').value = $('txtBillingPostcode').value;
		$('sltDeliveryCountryID').value = $('sltBillingCountryID').value;
	},
	
	SiteTips: function() {
		$$('img.tipz').each(function(element, index) { 
			var content = element.get('title').split('::'); 
			element.store('tip:title', content[0]); 
			element.store('tip:text', content[1]);
		});
		
		var tipz = new Tips('.tipz', { 
			className: 'tipz', 
			fixed: true, 
			hideDelay: 50, 
			showDelay: 50 
		}); 
	}	
		
};

window.addEvent('domready', Site.Init);