

(function($) {
	var name = navigator.appName
    $.fn.initScript = function(options) {
		
// variable font size
		var f = readCookie('font_size')
		$('body').css('font-size', f);
				
		$('.nyromodal').nyroModal();
		
		$('.nyromodal').click(function (){
			var docheight = $(document).height();
			$('#nyroModalBg').css({height: docheight});
			}
			
		);
			
			
		$('a.fontControl').click(function() {
			// find the last part of the href and use this to set the font
			var f = this.href.substring(this.href.lastIndexOf('/')+1);
			set_font_size(f);
			return false; // cancel the click through
		});
		
		function set_font_size(f) {
			$('body').css('font-size', f);
			createCookie('font_size', f, 365);
		}
		
		$('input.page')
			.focus(function () {
			$(this).val("");
		});
		
// accordion menu

		$('.leftmenu').accordion({ event: 'mouseover', active:'h3.menu-open' });
		
		//hover states on the static widgets
		$('span.folder').hover(
			function() { $(this).addClass('ui-state-hover'); }, 
			function() { $(this).removeClass('ui-state-hover'); }
		);
				
		$('.copy .header .middle') 
		.after('<div id="nav">click here to change the image&#160;&#160;</div>') 
		.cycle({ 
			fx:     'fade', 
			speed:  3000, 
			timeout: 0, 
			pager:  '#nav' 
		});
		
		
		$(".show").click(function () {
			$(this).next("div").toggle();
		});
		
		$('.initialfocus').focus();
		
		$('.Post').hide();
		
		
		$(".Explore, .collapse_control").click(function () {
			$(this).next("div").toggle();
		});
		
		$(".Explore").click(function () {
			$('.Explore a').toggleClass("open");
			$('.Explore a').toggleClass("closed");
		});
		
		$(".collapse_control").click(function () {
			$('.collapse_control a.open').toggle();
			$('.collapse_control a.closed').toggle();
		});
		
		
		$('#Email').click(function () {
			$('.Post').hide('slow');
			$('.Email').show('slow');
		});
		
		$('#Post').click(function () {
			$('.Email').hide('slow');
			$('.Post').show('slow');
		});

// utilises cycle.js
        $('.copy .header .middle')
					.cycle({
					    fx: 'fade',
					    speed: 3000,
					    timeout: 8000
					});

        $(".Tooltip").hover(
		  function() { $('.HelpText, .HelpText_Review').show();  return false; },
		  function() { $('.HelpText, .HelpText_Review').hide(); return false; }
		);


        $(".close a").click(function() {
            $(this).parent("div").parent("div").parent("div").hide();
            return false;
        });

        $(".show, .show_1").click(function() {
            var me = $(this);
            var grandparent = me.parent("div").parent("div");
            var details = me.parent("div").next("div");
            var customformname = grandparent.siblings(".customformname").val();
            $.get("/products/getcustomform", { formname: customformname }, function(html) {
                if (html != "") {
                    grandparent.find(".notes_form").html(html);
                }
                details.toggle();
            });

            return false;
        });

        $(".MakeanEnquiry a").click(function() {
            var me = $(this);
            var grandparent = me.parent("div").parent("div");
            var details = me.parent("div").next("div");
            var customformname = grandparent.siblings(".customformname").val();
            $.get("/products/getcustomform", { formname: customformname }, function(html) {
                if (html != "") {
                    grandparent.find(".notes_form").html(html);
                }
				
                details.toggle();
            });

            return false;
        });

        $(".showDetails, .close a").click(function() {

            $(".close a").click(function() {
                $(this).parent("div").parent("div").parent("div").hide();
                return false
            }
						)
            $('.openline').toggle();
            return false
        });

// utilises jquery-ui-1.6.custom.min.js
		//hover states on the static widgets
		
		$('.leftmenu').hover(
			function() { $(this).addClass('ui-state-hover'); }, 
			function() { $(this).removeClass('ui-state-hover'); }
		);

        $('.initialfocus').focus();
		
        $('.linenotesBasket .title, .checkoutHead').click(function() {
            $(this).next("div").toggle();
        });
		
		$(".email input").keyup(function(){
            var emailInput = this;
			var firstEmailInput = document.getElementsByName("EmailAddress");
			if (emailInput.name == 'ConfirmEmail')
			{
				if( isValidEmailAddress(emailInput.value) && emailInput.value == firstEmailInput[0].value)
				{
					emailInput.style.backgroundColor="lightgreen";
				}
				else
				{
					emailInput.style.backgroundColor="pink";
				}
			}
			else
			{
				if( isValidEmailAddress(emailInput.value))
				{
					emailInput.style.backgroundColor="lightgreen";
				}
				else
				{
					emailInput.style.backgroundColor="pink";
				}
			}
		});
		

        // Something dodgey about this code so run it last.
        var addToBasketForm;
        try {
            addToBasketForm = $("form[action$=/basket]");
        }
        catch (ex) { }

        if (addToBasketForm) {
            addToBasketForm.submit(function() {
                var request_form = $(this);
                var notes = request_form.find(":input[name=Notes]:hidden");
                if (notes.length > 0) {
                    var notes_value = "";
                    var form_values = request_form.find(":input[type!=hidden]").serializeArray();
                    $.each(form_values, function() {
                        if (this.name != "Notes") {
                            notes_value += this.name + ": " + this.value + "\n\r";
                        }
                    });

                    notes.val(notes_value);
                }

                // We want the form to be submitted.
                return true;
            });
        }
    }


})(jQuery);
       
    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);
	}
					
					
					
					
						
	
	