var Site = {

	Init: function() {
		Site.DisableRightClick(),
		Site.Reorder();
		Site.Editor();
		Site.FocusElements();		
		Site.SiteTips();
		Site.Ticker();
		Site.Flashing();
	},
	
	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 ($('frmCategory') && $('txtTitle')) $('txtTitle').focus();
		if ($('frmAdministrator') && $('txtName')) $('txtName').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 (textarea != '') var editor = new Editor($(textarea), { url: '/images/editor/', css: '/styles/editor-format.css' });	
	},
	
	CalculateWall: function() {
		var units = $$('input[name=rdoUnits]').filter( function(item, index) { return item.checked } )[0].get('value');

		var distance = parseFloat($('txtWallDistance').value);
		var wallHeight = parseFloat($('txtWallHeight').value);

		if (isNaN(units)) {
			window.alert("Please select a measurement unit");
			return;
		}

		if (isNaN(distance)) {
			window.alert("You must provide a numeric distance");
			return;
		} else if (distance < 1) {
			window.alert("Distance must be great than zero");
			return;
		}

		if (isNaN(wallHeight)) {
			window.alert("You must provide a numeric wall height");
			return;
		} else if (wallHeight < 1) {
			window.alert("Height must be great than zero");
			return;
		}

		var rolls = 1 + Math.floor((((units * distance) * (units * wallHeight)) / 0.52) / 10.05);
		$('txtWallRolls').value = rolls;
		
		if ($('btnPaperBuyNow')) {
			$('hdnQty').value = rolls;
			$('btnPaperBuyNow').removeEvents('click');
			$('btnPaperBuyNow').addEvent('click', function() { $('frmRollCalculator').submit(); });
		}
	},
	
	CalculateCeiling: function() {
		var units = $$('input[name=rdoUnits]').filter( function(item, index) { return item.checked } )[0].get('value');

		var ceilingLength = parseFloat($('txtCeilingLength').value);
		var ceilingWidth = parseFloat($('txtCeilingWidth').value);		
		
		if (isNaN(units)) {
			window.alert("Please select a measurement unit");
			return;
		}		
		
		if (isNaN(ceilingLength)) {
			window.alert("You must provide a numeric ceiling length");
			return;
		} else if (ceilingLength < 1) {
			window.alert("Ceiling Length must be great than zero");
			return;
		}

		if (isNaN(ceilingWidth)) {
			window.alert("You must provide a numeric ceiling width");
			return;
		} else if (ceilingWidth < 1) {
			window.alert("Ceiling width must be great than zero");
			return;
		}

		var rolls = 1 + Math.floor((((units * ceilingLength) * (units * ceilingWidth)) / 5.226));
		$('txtCeilingRolls').value = rolls;
		
		if ($('btnPaperBuyNow')) {
			$('hdnQty').value = rolls;
			$('btnPaperBuyNow').removeEvents('click');
			$('btnPaperBuyNow').addEvent('click', function() { $('frmRollCalculator').submit(); });
		}
	},
	
	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);
		} 
	}
		
};

window.addEvent("domready", Site.Init);