jQuery.noConflict();
jQuery(function($){
	
    var frontend =
    {
        init:function()
        {
            this.resultsPerPage();
            this.formValidation();
            this.clearSearch();
            this.eyeCatcher();
            this.productGallery();
            this.fancyBox();
        },
		
        resultsPerPage:function()
        {
            if($('#resultsPerPage').length)
            {
                var $container = $('#resultsPerPage');
                var $ul = $('#resultsPerPage ul');
                var $links = $('#resultsPerPage a');
				
				
                /* default option or take the one from the request
				 * ----------------------------------------------------- */
                var currentOption = 20;
                var q = window.location.search;
				
                if (q)
                {
                    var posLimit = q.indexOf("limit");
                    if (posLimit != -1)
                    {
                        posLimit += 6
                        var posAmp = q.indexOf('&', posLimit);
                        if (posAmp != -1)
                        {
                            currentOption = q.substring(posLimit, posAmp);
                        }
                        else
                        {
                            currentOption = q.substring(posLimit);
                        }
                    }
                }
				
                /* remove current option from list links
				 * prepare ul and push current as a first item
				 * ----------------------------------------------------- */
				
                $links.each(function(){
                    if($(this).text() == currentOption)
                    {
                        $(this).parent().remove();
                    }
                });
				
                $ul.addClass('js')
                .prepend('<li><a class="current">' + currentOption + '</a></li>')
                .hover(function()
                {
                    $ul.css('overflow', 'visible')
                }, function(){
                    $ul.css('overflow', 'hidden')
                }
                );
            }
        },
		
        formValidation: function()
        {
            $.extend($.validator.messages, {
                required: "Required."
            });
            var formMessages =
            {
                Email: {
                    email: "Must be valid."
                },
                SpamAnswer:""
            }
            var basketFormMessages = {
                Email: {
                    email: "Must be valid."
                }
            }
            if($('#Form_EnquiryForm').length) $('#Form_EnquiryForm').validate({
               
                messages: formMessages
            });
            if($('#ProductForm_EnquiryForm').length)$('#ProductForm_EnquiryForm').validate({
                
                messages: formMessages
            });
            if($('#ProductForm_AddToBasketForm').length)$('#ProductForm_AddToBasketForm').validate();
            
            if($('#BasketForm_BasketForm').length)$('#BasketForm_BasketForm').validate({
                messages: basketFormMessages
            });
        },
		
        clearSearch:function()
        {
            if($('#Form_SearchForm_Search').length)
            {
                $('#Form_SearchForm_Search').focus(function(){
                    $(this).attr('value', '');
                });
            }
        },
		
        eyeCatcher:function()
        {
            if($('#eyeCatchers').length)
            {
                $('#eyeCatchers img').each(function(index){
                    if(index != 0) $(this).hide();
                })
                $('#eyeCatchers p a').eq(0).addClass('hover notop');
                $('#eyeCatchers p a').hover(function(){
                    $('#eyeCatchers img').hide();
                    $('#eyeCatchers p a').removeClass('hover');
                    $(this).parent().next().show();
                    $(this).addClass('hover');
                },
                function(){
					
                    });
                $('#eyeCatchers p a').eq(0).hover(function(){
                    $(this).addClass('notop');
                });
                $('#eyeCatchers p a').eq(4).hover(function(){
                    $(this).addClass('nobottom');
                });
            }
        },
		
        productGallery:function()
        {
            if($('#thumbs').length)
            {
                $('#thumbs a').click(function(){
					
                    var targ = this.href;
                    $('#bigs img').css('zIndex', 99);
                    $('#bigs img[src*='+targ+']').css('zIndex', 1000);
                    return false;
                });
            }
        },
		
        fancyBox:function()
        {
            if($('body.NewsPage').length)
            {
                if($('a.newsImage').length)
                {
                    $('a.newsImage').fancybox();
                }
            }
        }
    }
	
    frontend.init();
	
});

Cufon.replace('h1:not(#header h1)');
Cufon.replace('h2:not(#eyeCatchers h2)', {
    hover:true
});
Cufon.replace('h3');
Cufon.replace('p.productTitle');
