var Site = {

	Init: function() {
		Site.ImageRotater();
		Site.Editor();
		Site.Reorder();
		Site.SetFocus();
		Site.SiteTips();
		Site.NurseryRhymes();
		Site.RolloverEffects();
	},

	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 ($('frmProductCategory') && $('sltParentID')) {
			if ($('sltParentID').getSelected()[0].value == 0) $('productCategoryDescription').setStyle('display', 'none');
			
			$('sltParentID').addEvent("change", function(e) {
				$('productCategoryDescription').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;
			
			$('mainLink').href = image;
			$('mainLink').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/' });	
	},
	
	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 
		}); 
	},
	
	NurseryRhymes: function() {
		if ($('nurseryRhymes')) {
			fnTickerStart();
		} //end if
	},
	
	RolloverEffects: function() {
		$$('.rollover').each(function(el) {
			if (el.src.test(/\.(gif|jpg|jpeg|png)$/i)) {
				var preload = new Image();
				preload.src = el.src.replace(/\.(gif|jpg|jpeg|png)/, '-over.$1');
				
				el.addEvents({
					mouseover: function() {
						this.src = this.src.replace(/\.(gif|jpg|jpeg|png)/, '-over.$1');
					},
					
					mouseout: function() {
						this.src = this.src.replace(/-over\.(gif|jpg|jpeg|png)/, '.$1');
					}
				});
			}
		});
	}	
		
};

window.addEvent('domready', Site.Init);