var Site = {

	Init: function() {
        Site.SetFocus();
        Site.HomePageImages();
		Site.ImageRotater();		
		Site.RolloverEffects();
        Site.MenuMatic();
        Site.SearchJump();
        Site.SearchAutocompleter();
        Site.FindAWishList();
        Site.Faqs();
        Site.FadeAddToBasketMessage();
	},

	SetFocus: function() {
		if ($('txtUsername') != null) $('txtUsername').focus();
		if ($('txtFirstName') != null) $('txtFirstName').focus();
	}, 

	HomePageImages: function() {
        if ($('homeImage') != null) {	       
    		if ($(document.body).getElement('div.homePageImageRotater')) {
    			var scroller = new ContentScroller({slideDuration: 6000, fadeDuration: 500});
    			scroller.addSlides($$('div.homePageImageRotater img').setStyle('z-index', 1));
    			scroller.start();
    		}
        }
	},	

	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();
		}
	},	
	
	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');
					}
				});
			}
		});
	},
    
    MenuMatic: function() {
        if ($('menu') != null) {
            var myMenu = new MenuMatic({ orientation: 'vertical', id: 'menu', subMenusContainerId: 'menuSub', effect: 'slide', opacity: 100, duration: 0, hideDelay: 1000, fixHasLayoutBug: true });	 
        }
        
        if ($('menuTopMore') != null) {
            var myMenuTop = new MenuMatic({ orientation: 'horizontal', id: 'menuTopMore', subMenusContainerId: 'menuTopMoreSub', effect: 'slide', opacity: 100, duration: 0, hideDelay: 1000, fixHasLayoutBug: true });
        } 
    },
    
    SearchJump: function() {
        if ($('resultBar') != null) {
            if ($('btnResultBarGo') != null) {
                $('btnResultBarGo').setStyle('display', 'none');
            }
            
            if ($('sltShow') != null) {
                $('sltShow').addEvent('change', function() {
                    $('resultBarSearch').submit();
                });
            }
                        
            if ($('sltViewBy') != null) {
                $('sltViewBy').addEvent('change', function() {
                    $('resultBarSearch').submit();
                });
            }
            
            if ($('sltOrderBy') != null) {
                $('sltOrderBy').addEvent('change', function() {
                    $('resultBarSearch').submit();
                });
            }
        }
    },
    
    SearchAutocompleter: function() {
        if ($('searchBox') != null) {            
            new Autocompleter.Request.JSON('txtSearch', 'ajax/search-results.php', {
                'postVar': 'txtSearch'
            });
        }        
    },
        
	ChangeImage: function (element, image, title) {
		if ($(element) != null) {
			$(element).src = image;
			$(element).title = title;
			
            if ($('mainLink') != null) {
                $('mainLink').href = image;
                $('mainLink').title = title;
            }		
		}
	},
	
	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;
	},
    
    FadeImages: function() {
        $$('img.fade').each(function(el) {
            el.addEvent('mouseover', function() {
                el.fade(0.5);  
            });
              
            el.addEvent('mouseout', function() {
                el.fade(1);
            });
        });    
    },
    
    FindAWishList: function() {
        if ($('findAWishList') != null) {
            $('txtFind').addEvent('click', function() {
                $('txtFind').selectRange(0, $('txtFind').value.length);
            });
        }
    },
    
    Faqs: function() {
        if ($('faqs') != null) {
            var accordion = new Accordion('strong.faqQuestion', 'p.faqAnswer', {
            	opacity: false,
            	onActive: function(toggler, element){
            		toggler.setStyle('color', '#ff3300');
            	},
             
            	onBackground: function(toggler, element){
            		toggler.setStyle('color', '#3b5997');
            	}
            }, $('faqs'));            
        }
    },
    
    FadeAddToBasketMessage: function() {
        if ($('addToBasketMessage') != null) {
            var div = $('addToBasketMessage').set('tween', {link: 'chain', duration: 2000});

            var startTimer = window.setTimeout(
            	function () {
            		div.tween('top', '0px')
                    window.clearTimeout(startTimer);        
            	},
            	1000
            );

            var endTimer = window.setTimeout(
            	function () {
            		div.tween('top', '-50px')
                    window.clearTimeout(endTimer);        
            	},
            	4000
            );
        }
    }
		
};

window.addEvent('domready', Site.Init);
