
$(document).ready(function() {

    var filterCount = 0;
    $('#filter > li').each(function(index) {
        if ($(this).children().size() == 0) {
            $(this).css('display', 'none');
        } else {
            if ($(this).find("li a.disable").size() != $(this).find("li a").size()) {
                filterCount++;
            } else {
                $(this).css('display', 'none');
            }
        }
    });

    if (filterCount == 0) {
        $('#filter').css('display', 'none');
    }
    else {
        $('#filter').css('display', 'block');
    }

    $('#filter > li > ul > li > a').each(function(index) {
        $(this).append(" (<b>" + $(this).attr("rel") + "</b>)");
        if ($(this).attr("rel") == 0) {
            $(this).parent().css('display', 'none');
        }
    });

    $('.inner_list ul').each(function(index) {


        if (index == 0) {
            t = 12;
        } else {
            t = 0;
        }
  
        childrenCount = $(this).children().size();

        colums = Math.round(childrenCount / 12);

        if (childrenCount % 9 != 0) {
            colums = colums + 1;
        } 
        $(this).easyListSplitter({
            colNumber: colums
        });

        $('.listContainer' + (index + 1)).width(colums * 97 + (colums*t));
        if (index == 0) {
            $('.listContainer' + (index + 1) + ' li').width(97 + (t-2));
            $('.listContainer' + (index + 1) + ' ul').width(97 + (t));
            
        }
        $(this).parent().parent().parent().parent().width(colums * 97 + (colums * t)+t)
    });
    /*$('.listContainer' + 2).width(81); */

    var imageCount = 0;

    $('#brands img').load(function() {
        imageCount++;
    });

    if ($.browser.webkit) {
        checkLoad();
    } else {
        setwidth();
    }
    var check;
    check = 0;
    function checkLoad() {
        if (check == 4) {
            setwidth();
        } else {
            if (imageCount == $('#brands a').length) {
                setwidth();
            } else {
                setTimeout(function() {
                    checkLoad();
                }, 500);
            }
        }
        check++;
    }

    function setwidth() {
        $('#brands').fadeIn("slow");
        var brandsWidth = 0;
        $('#brands .content a').each(function() {
            brandsWidth += $(this).width();
        });

        $('#brands .content').width(brandsWidth);
        animateBrands();
    }

    function animateBrands() {
        $('#brands .content').css("right", "0px");
        $('#brands .content').animate({
            right: '+=' + ($('#brands .content').width()) / 2
        }, $('#brands a').length * 1666, 'linear', function() {
            animateBrands();
        });
    }
});

$(function () {
	
	$('#all_brands_bt').toggle(function() {
	$('#brands_dropdown').fadeIn(500);
		return false;
	}, function() {
	   $('#brands_dropdown').fadeOut(500);
		return false;
	});
	
	$('div.row').each(function(index) {
	var max=0;
		$(this).find('div.td').each(function() {
			if (max<$(this).height())
			{
			  max = $(this).height();
			}
		});
	$(this).find('div.td').css('height',max);
	});
	
	var count = 0;
	$(this).find('div.row').each(function() {
		if (count == 0) {
			count = 1;
			$(this).addClass("zebra");
		} else {
			count = 0;
		}
	});

	var count = 0;
	$(this).find('div.lines').each(function() {
	    if (count == 0) {
	        count = 1;
	        $(this).addClass("zebra");
	    } else {
	        count = 0;
	    }
	});
	
	
	$("#CopyAddress").click(function() {
		$("#dFirstName").val($("#bFirstName").val());
		$("#dLastName").val($("#bLastName").val());
		$("#dAddress1").val($("#bAddress1").val());
		$("#dAddress2").val($("#bAddress2").val());
		$("#dTown").val($("#bTown").val());
		$("#dCounty").val($("#bCounty").val());
		$("#dPostCode").val($("#bPostCode").val());
		$("#ctl00_center_TopMenu10_dTitle").val($("#ctl00_center_TopMenu10_bTitle").val());
		$("#ctl00_center_TopMenu10_DCountry").val($("#ctl00_center_TopMenu10_bCountry").val());
		$("#BEmail").val($("#BEmail").val());
		$("#ctl00_center_TopMenu10_ddlHowHeard").val($("#ctl00_center_TopMenu10_ddlHowHeard").val());
		$("#ctl00_center_TopMenu10_txtSpecInstr").val($("#ctl00_center_TopMenu10_txtSpecInstr").val());
		$('#aspnetForm').submit();
		return false;
	});
	
	$("#forgotPassword_btn").click(function() {
		$(".black_overlay").css('display','block');
		$("#forgotPassword").css('display','block');
	});
	
	$("#forgotPassword .close").click(function() {
		$(".black_overlay").css('display','none');
		$("#forgotPassword").css('display','none');
	});
	
	$("#recommend_btn").click(function() {
		$(".black_overlay").css('display','block');
		$("#recommend").css('display','block');
	});    
	
	$("#recommend .close").click(function() {
		$(".black_overlay").css('display','none');
		$("#recommend").css('display','none');
    });

    $("#review2").click(function () {
        $(".black_overlay").css('display', 'block');
        $("#prodReviews").css('display', 'block');
    });

    $("#prodReviews .close").click(function () {
        $(".black_overlay").css('display', 'none');
        $("#prodReviews").css('display', 'none');
    });
	
	$("#terms_conditions .close").click(function() {
		$(".black_overlay").css('display','none');
		$("#terms_conditions").css('display','none');
	});
	
	$("#terms_conditions_btn").click(function() {
		$(".black_overlay").css('display','block');
		$("#terms_conditions").css('display','block');
	});
	
	$("#registerBtn").click(function() {
		$("#delivery .content").slideDown();
		$("#loginPanelContent").slideUp();
		return false;
	});
	
	$(".registerHeder").click(function() {
		$("#delivery .content").slideDown();
		$("#loginPanelContent").slideUp();
		return false;
	});
	
	$(".signInHedear").click(function() {
		$("#delivery .content").slideUp();
		$("#loginPanelContent").slideDown();
		return false;
	});
	

	(function ($) {
		$.fn.vAlign = function() {
			return this.each(function(i){
			var ah = $(this).height();
			var ph = $(this).parent().height();
			var mh = (ph - ah) / 2;
			$(this).css('margin-top', mh);
			});
		};
		})(jQuery);
		$('span.vA').vAlign();
	

	$('.menu_button').mouseenter(function(){$(this).addClass("sub_menu_bt")});
	$('.menu_button').mouseleave(function(){$(this).removeClass("sub_menu_bt")});	
	$('.sub_menu_bottom').mouseenter(function(){$(this).parent().find('.menu_button').addClass("sub_menu_bt")});
	$('.sub_menu_bottom').mouseleave(function(){$(this).parent().find('.menu_button').removeClass("sub_menu_bt")});
	
	$('#top_menu').menuFix();
	
	$("#recommend .submit").click(function() {
		var error = 0;
		$("#recommend .error").css("display","block");
		$("#recommend input[type=text]").each(function(i){
			if ($(this).val() == "") {
				$(this).addClass("inputError");
				error = 1;
			} else {
				$(this).removeClass("inputError");
			}
		});
		var email = $("#recommend input#youremail").val();
			if(!isValidEmailAddress(email))
			{
				error = 1;
				$("#recommend input#youremail").addClass("inputError");
			}
		var email = $("#recommend input#friendemail").val();
			if(!isValidEmailAddress(email))
			{
				error = 1;
				$("#recommend input#friendemail").addClass("inputError");
			}
		
		if (error == 1)
		    return false;
		else
		{
			$("#recommend .error").css("display","none");
			$("#recommend .submit").css("display","none");
			$("#recommend #thankyou").css("display","block");
		}
	});
	
	function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(			/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
	}
	
});
