var Site = {

	Init: function() {
		Site.DisableRightClick(),
		Site.Reorder();
		Site.Editor();
		Site.FocusElements();		
		Site.SiteTips();
		Site.Ticker();
		Site.Flashing();
		Site.RolloverEffects();
		Site.KeepAlive();
	},
	
	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).bind("contextmenu",function(e){
        	return false;
    	}); 		
	},
	
	FocusElements: function() {
		if ($('frmLogin').length && $('txtUsername').length) $('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();

			$('input[name=rdoImageType]').bind({
				'click': function() {
					if (this.value == 'Downloaded Image') {
						$('.imageFile').css('display', '');
						$('.imageUrl').css('display', 'none');
					} else if (this.value == 'Third Party Image') {
						$('.imageUrl').css('display', '');
						$('.imageFile').css('display', 'none');
					}
				}
			});
		}		
		if ($('frmProduct') && $('txtTitle')) $('txtTitle').focus();
		if ($('frmProduct') && $('sltGoto')) {
			$('sltGoto').bind("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').bind('click', function() { window.alert('Calculate Your Rolls First'); });
		}
		
		if ($('frmYourDetails') && $('txtBillingFirstName')) $('txtBillingFirstName').focus();
	},
	
	Reorder: function() {
		if ($('#itemList').length) {
			
			$("#itemList").sortable({ opacity: 0.6, cursor: 'move', update: function() {
				var value = $(this).sortable("serialize");
				$('#hdnPostionIDs').val(value); 
				}
																					 });
			
		}								  
		},
			
			
		/*
		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() {

		if ($('#txaDescription').length) $('#txaDescription').cleditor({
											width:        670, // width not including margins, borders or padding
          									height:       250 // height not including margins, borders or padding
																	   });
		if ($('#txaContent').length) $('#txaContent').cleditor({
											width:        670, // width not including margins, borders or padding
          									height:       250 // height not including margins, borders or padding
																	   });
		
	},
	
	CalculateWall: function() {
		var units = $$('input[name=rdoUnits]').filter( function(item, index) { return item.checked } )[0].get('value');

		var distance = parseFloat($('#txtWallDistance').val());
		var wallHeight = parseFloat($('#txtWallHeight').val());
		
		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').val(rolls);
		
		if ($('#btnPaperBuyNow')) {
			$('#hdnQty').val(rolls);
			$('#btnPaperBuyNow').unbind('click');
			$('#btnPaperBuyNow').bind('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').val());
		var ceilingWidth = parseFloat($('#txtCeilingWidth').val());		
		
		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').val(rolls);
		
		if ($('#btnPaperBuyNow')) {
			$('#hdnQty').value = rolls;
			$('#btnPaperBuyNow').unbind('click');
			$('#btnPaperBuyNow').bind('click', function() { $('#frmRollCalculator').submit(); });
		}
	},
	
	FillingDeliveryAddress: function() {
		$('#txtDeliveryFirstName').val($('#txtBillingFirstName').val());
		$('#txtDeliveryLastName').val($('#txtBillingLastName').val());
		$('#txtDeliveryAddress1').val($('#txtBillingAddress1').val());
		$('#txtDeliveryAddress2').val($('#txtBillingAddress2').val());
		$('#txtDeliveryTownCity').val($('#txtBillingTownCity').val());
		$('#txtDeliveryCounty').val($('#txtBillingCounty').val());
		$('#txtDeliveryPostcode').val($('#txtBillingPostcode').val());
		$('#sltDeliveryCountryID').val($('#sltBillingCountryID').val());
	},
	
	Ticker: function() {
		if($('#ticker').length){
		$('#ticker').marquee().mouseover(function () {
            $(this).trigger('stop');
        }).mouseout(function () {
            $(this).trigger('start');
        });		
		}
	},
	
	Flashing: function() {
		if ($('flashing').length) {
			$('flashing').flash('#000', '#f03', 6, 'color', 600);
		} 
	},
	
	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');
					}
				});
			}
		});
	}, 
	
	KeepAlive: function(){
		if($('isAdmin')){
			//alert('hey');
			Site.SendRequest.periodical(60000);
		}
	}, 
	
	SendRequest: function(){
		var request = new Request({
			url: '_ajax_keep_alive.php',
			method: 'get', 
			update: 'refresh-me',
			onComplete: function(response){
				
			} 
		})
		
		request.send();
	}
		
};
jQuery(document).ready( function() {
			Site.Init();
								 });
//window.addEvent("domready", Site.Init);
