var Site = {

	Init: function() {
		Site.DisableRightClick(),
		Site.Reorder();
		Site.Editor();
		Site.FocusElements();		
		Site.SiteTips();
		Site.Ticker();
		Site.Flashing();
		Site.SetupModel();
		Site.LatestNews();
	},
	
	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 
		}); 
	},
	
	DisableRightClick: function() {
		document.body.addEvent('contextmenu', function(e) {         
			e.stop();     
		}); 		
	},
	
	FocusElements: function() {
		if ($('frmLogin') && $('txtUsername')) $('txtUsername').focus();
		if ($('frmCountry') && $('txtTitle')) $('txtTitle').focus();
		if ($('frmHearAbout') && $('txtTitle')) $('txtTitle').focus();
		if ($('frmMetaData') && $('txaData')) $('txaData').focus();
		if ($('frmWebPage') && $('txaContent')) $('txaContent').focus();
		if ($('frmWebPageImage') && $('txtTitle')) $('txtTitle').focus();   
		if ($('frmInformationPage') && $('txtTitle')) $('txtTitle').focus();
		if ($('frmInformationPageImage') && $('txtTitle')) $('txtTitle').focus();   
		if ($('frmCategory') && $('txtTitle')) $('txtTitle').focus();
		if ($('frmModel') && $('txtTitle')) $('txtTitle').focus();
		if ($('frmAdministrator') && $('txtName')) $('txtName').focus();
		if ($('frmNews') && $('txtHeadline')) $('txtHeadline').focus();
		if ($('frmGallery') && $('txtTitle')) $('txtTitle').focus();
		if ($('frmGalleryImage') && $('txtTitle')) $('txtTitle').focus();
		if ($('frmButton') && $('txtTitle')) { 
			$('txtTitle').focus();

			$(document.body).getElements('input[name=rdoImageType]').addEvents({
				'click': function() {
					if (this.value == 'Downloaded Image') {
						$(document.body).getElement('li.imageFile').setStyle('display', '');
						$(document.body).getElement('li.imageUrl').setStyle('display', 'none');
					} else if (this.value == 'Third Party Image') {
						$(document.body).getElement('li.imageUrl').setStyle('display', '');
						$(document.body).getElement('li.imageFile').setStyle('display', 'none');
					}
				}
			});
		}		
		if ($('frmProduct') && $('txtTitle')) $('txtTitle').focus();
		if ($('frmProduct') && $('sltGoto')) {
			$('sltGoto').addEvent("change", function(e) {
				location.href = this[this.selectedIndex].value;
			});
		}
		if ($('frmProductImage') && $('txtTitle')) $('txtTitle').focus();
		if ($('frmFeedbackForm') && $('txtName')) $('txtName').focus();
		
		if ($('frmRollCalculator') && $('txtWallDistance')) {
			$('txtWallDistance').focus();
			if ($('btnPaperBuyNow')) $('btnPaperBuyNow').addEvent('click', function() { window.alert('Calculate Your Rolls First'); });
		}
		
		if ($('frmYourDetails') && $('txtBillingFirstName')) $('txtBillingFirstName').focus();
	},
	
	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) {
					el.highlight('#7f7f7f');
				},
				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/', css: '/styles/editor-format.css' });	
	},
	
	
	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;
	},
	
	Ticker: function() {
		if ($('homeBestSellers') && $('ticker')) {
			var ticker = new Ticker('ticker', { rightToLeft: (Math.floor(10 * Math.random()) > 5), speed: 1.5 });
			ticker.start.delay(1000, ticker);					
		}
	},
	
	Flashing: function() {
		if ($('flashing')) {
			$('flashing').flash('#000', '#f03', 6, 'color', 600);
		} 
	},
	
	SetupModel: function() {	
		if ($('sltCategoryID') && $('sltModelID')) {
			$('sltCategoryID').addEvent('change', Site.UpdateModels);

			if (!$('sltModelID').hasChildNodes()) {
				Site.UpdateModels();
			}
		}
	},
	
	UpdateModels: function() {
		var request = new Request({
			method: 'get',
			onSuccess: Site.LoadModels,
			url: '/admin/ajax-get-model-list.php'
		}).send('id=' + $('sltCategoryID').value);
	},	
	
	LoadModels: function(response, xml) {
		while ($('sltModelID').hasChildNodes()) {
			$('sltModelID').removeChild($('sltModelID').firstChild);
		}

		var models = xml.getElementsByTagName('model');
		for (var i = 0; i < models.length; i++) {
			$('sltModelID').adopt(
				new Element('option', {
					'value': models[i].getAttribute('id')
				}).set('html', models[i].firstChild.nodeValue)
			);
		}
	},
	
	LatestNews: function() {
		if ($(document.body).getElement('div.newsArticle')) {
			$(document.body).getElement('div.newsArticle').setStyle('display', 'block');
			
			var scroller = new ContentScroller({slideDuration: 8500, fadeDuration: 500});
			scroller.addSlides($$('div.newsArticle div').setStyle('z-index', 3));
			scroller.start();
		}
	}
			
};

window.addEvent("domready", Site.Init);