var Site = {

	Init: function() {
		//Site.SetupMenuSecond();
        Site.HomeImageRotater();
		Site.ContentImageRotater();
		Site.Editor();
		Site.Reorder();
		Site.SetFocus();
		Site.SiteTips();
		Site.Design();
        Site.ColourAutocomplete();
        Site.MenuSwitch();
	},

	SetupMenuSecond: function() {
		$$('a.subLink').each(function(element, index) {
			var id = element.get('id');
			
			$(id).addEvents({
		    	'mouseenter': function() {
        			$(id + 'Options').setStyle('display', 'block');
    			},
    			'mouseleave': function() {
        			$(id + 'Options').setStyle('display', 'none');
    			}
			}); 			
		});
	},

	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 ($('frmColour') && ($('txtDesign'))) $('txtDesign').focus(); 
	}, 

	HomeImageRotater: function() {
        if ($('homePageImages')) {
    		if ($(document.body).getElement('div.imageRotater')) {
    			var scroller = new ContentScroller({slideDuration: 3750, fadeDuration: 1000});
    			scroller.addSlides($$('div.imageRotater img').setStyle('z-index', 1));
    			scroller.start();
    		}
        }
	},	

	ContentImageRotater: function() {
        if (!$('homePageImages')) {
    		if ($(document.body).getElement('div.imageRotater')) {
    			var scrollerContent = new ContentScroller({slideDuration: 4500, fadeDuration: 500});
    			scrollerContent.addSlides($$('div.imageRotater img').setStyle('z-index', 1));
    			scrollerContent.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/' });	
	},
	
	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 
		}); 
	},
	
	Design: function() {
		if ($('rolloverMessage')) { //fullWidth
			$('rolloverMessage').addEvents( { //image
		    	'click': function() { //mouseenter
       				$('fullWidth').setStyle('display', 'block');
    			}
			}); 
			
			$('fullWidth').addEvents({
    			'mouseleave': function() {
       				$('fullWidth').setStyle('display', 'none');
    			}
			}); 			
		}
	},
    
    ColourAutocomplete: function() {
        if ($('txtDesign')) {
        	var ac = new Autocompleter.Request.JSON('txtDesign', '../ajax/get-designs.php', {
        		'postVar': 'txtDesign',
        		minLength: 3,
        		maxChoices: 10,
        		autoSubmit: false,
        		cache: true,
        		delay: 300,
        		onRequest: function() {
        			$('txtDesign').setStyles({
        				'background-image': 'url(../images/please-wait.gif)',
        				'background-position': '745px 0',
        				'background-repeat': 'no-repeat'
        			});
        		},
        		onComplete: function() {
        			$('txtDesign').setStyle('background-image', 'url(../images/blank.gif)');
        		}
        	});
        } 
    },
    
    MenuSwitch: function() {
		$$('#mainmenu li').each(
			function(el){
				el.getElement('a').addEvent('mouseover', 
					function(subLinkId){
						var layer = subLinkId+"_menu";
						// Display current submenu
						$('childlinks').getElements('ul').setStyle('display', 'none');
						
						var arVersion = navigator.appVersion.split("MSIE")
						var version = parseFloat(arVersion[1])
						if ((version >= 5.5) && (document.body.filters)) {
							$(layer).setStyle('display', 'inline');
						}
						else {
							$(layer).setStyle('display', 'block');
						}
						
					}.pass( el.id)
				);
			}
		);
	}
    
    
		
};

window.addEvent('domready', Site.Init);