<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>amnuts &#187; javascript</title>
	<atom:link href="http://blog.amnuts.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.amnuts.com</link>
	<description>php projects, javascript, and... stuff.</description>
	<lastBuildDate>Fri, 07 May 2010 09:11:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Same height for elements &#8211; now with jQuery goodness!</title>
		<link>http://blog.amnuts.com/2009/12/24/same-height-for-elements-2/</link>
		<comments>http://blog.amnuts.com/2009/12/24/same-height-for-elements-2/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 00:25:56 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[plug-in]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/?p=150</guid>
		<description><![CDATA[A while ago I wrote a post on how to use the Prototype js library to make elements on your page have the same height.
For those too lazy to read that post, it can be summarised something like this:

Have a number of divs on your page that have different size blocks of text in them? [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I wrote a post on <a href="http://blog.amnuts.com/2007/02/09/same-height-for-elements/">how to use the Prototype js library to make elements on your page have the same height</a>.</p>
<p>For those too lazy to read that post, it can be summarised something like this:<br />
<span id="more-150"></span></p>
<blockquote><p>Have a number of divs on your page that have different size blocks of text in them?  Want to make all the divs have the same height, but can&#8217;t accurately determine what that height should be in advance?  No problem!  Use javascript to tweak the heights when the page has loaded and the browser knows the size of the divs.  Use this plugin to do it automatically based on a class you supply the elements you want to change the size of.  Better yet, make them all match either the smallest height or the largest height!</p></blockquote>
<p>Simple, yes?</p>
<p>Well, that was with Prototype&#8230; But a while ago now I created a version for jQuery.  So here it is.</p>
<pre lang="javascript">
/**
 * Same Height, jQuery plug-in.
 *
 * This plug-in allows you to automatically have all of the elements marked
 * with a particular class to be the same height, either by smallest or
 * largest.
 *
 * By default, 'samemaxheight' and 'sameminheight' will be used.
 *
 * Examples of use:
 *
 *     $(document).ready(function() {
 *         $('body').sameheight();
 *     });
 *
 *     $(document).ready(function() {
 *         $('#mainContent').sameheight({
 *             max: 'mymaxclass',
 *             min: 'myminclass'
 *         });
 *     });
 *
 *     $.fn.sameheight.defaults.max = 'mymaxclass';
 *     $.fn.sameheight.defaults.min = 'myminclass';
 *     $('body').sameheight();
 *
 * @copyright Copyright (c) 2009 Andrew Collington <andy @amnuts.com>
 */
(function($) {
    $.fn.sameheight = function(userOptions) {
        var options = $.extend({}, $.fn.sameheight.defaults, userOptions);

        var maxH = 0;
        $('.' + options.max, $(this)).each(function(){
            var h = $(this).height();
            maxH = (h > maxH) ? h : maxH;
        });
        $('.' + options.max, $(this)).each(function(){
            $(this).css({
                'height'   : maxH + 'px',
                'overflow' : 'hidden'
            });
        });

        var minH = 0;
        $('.' + options.min, $(this)).each(function(){
            var h = nodes[i].height();
            minH = ((h < minH) || !minH) ? h : minH;
        });
        $('.' + options.min, $(this)).each(function(){
            $(this).css({
                'height'   : minH + 'px',
                'overflow' : 'hidden'
            });
        });
    };

    $.fn.sameheight.defaults = {
        max: 'samemaxheight',
        min: 'sameminheight'
    };
})(jQuery);
</pre>
<p></andy></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2009/12/24/same-height-for-elements-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Text box &#8216;hint&#8217; values with jQuery</title>
		<link>http://blog.amnuts.com/2009/02/17/text-box-hint-values-with-jquery/</link>
		<comments>http://blog.amnuts.com/2009/02/17/text-box-hint-values-with-jquery/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 00:24:23 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[plug-in]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/?p=129</guid>
		<description><![CDATA[On creating a rather large form recently, I was in the need to have some kind of hint to the user about what format the content should take on several input boxes.  I could have done this with a description under the form element, but a more accepted way to do this, it seems, [...]]]></description>
			<content:encoded><![CDATA[<p>On creating a rather large form recently, I was in the need to have some kind of hint to the user about what format the content should take on several input boxes.  I could have done this with a description under the form element, but a more accepted way to do this, it seems, is to have a &#8216;hint&#8217; in the element itself.  You know the kind of thing I mean; a value, usually quite a light grey colour, that is present until you click in to the form element and then is disappears.  I also wanted to do this as a jQuery plug-in because, well, why not? <img src='http://blog.amnuts.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><span id="more-129"></span><br />
There are some things to bare in mind with this kind of functionality.  Obviously, you only want it to apply to text boxes and textareas &#8211; anything else is a waste of time&#8230; Even password fields are pointless to have a hint in them because it&#8217;ll only be a series of stars anyway!</p>
<p>The next thing to bare in mind is that you don&#8217;t want to reset the value if the user has typed anything in.  And sometimes you may even want to accept the user typing exactly what the hint is, you you can&#8217;t just assume that if the value == the hint then it&#8217;s OK to clear.</p>
<p>And with those things in mind, here&#8217;s the plug-in:</p>
<pre lang="javascript">
/**
 * Form text box hints.
 *
 * This plug-in will allow you to set a 'hint' on a text box or
 * textarea.  The hint will only display when there is no value
 * that the user has typed in, or that is default in the form.
 *
 * You can define the hint value, either as an option passed to
 * the plug-in or by altering the default values.  You can also
 * set the hint class name in the same way.
 *
 * Examples of use:
 *
 *     $('form *').textboxhint();
 *
 *     $('.date').textboxhint({
 *         hint: 'YYYY-MM-DD'
 *     });
 *
 *     $.fn.textboxhint.defaults.hint = 'Enter some text';
 *     $('textarea').textboxhint({ classname: 'blurred' });
 *
 * @copyright Copyright (c) 2009,
 *            Andrew Collington, andy@amnuts.com
 * @license New BSD License
 */
(function($) {
    $.fn.textboxhint = function(userOptions) {
        var options = $.extend({}, $.fn.textboxhint.defaults, userOptions);
        return $(this).filter(':text,textarea').each(function(){
            if ($(this).val() == '') {
                $(this).focus(function(){
                    if ($(this).attr('typedValue') == '') {
                        $(this).removeClass(options.classname).val('');
                    }
                }).blur(function(){
                    $(this).attr('typedValue', $(this).val());
                    if ($(this).val() == '') {
                        $(this).addClass(options.classname).val(options.hint);
                    }
                }).blur();
            }
        });
    };

    $.fn.textboxhint.defaults = {
        hint: 'Please enter a value',
        classname: 'hint'
    };
})(jQuery);
</pre>
<p>Some examples of use (as shown in the plug-in header comment, but I figure it can&#8217;t hurt to show it again!)</p>
<pre lang="javascript">
$('form *').textboxhint();

$('.date').textboxhint({
    hint: 'YYYY-MM-DD'
});

$.fn.textboxhint.defaults.hint = 'Enter some text';
$('textarea').textboxhint({ classname: 'blurred' });
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2009/02/17/text-box-hint-values-with-jquery/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Easy table sorting with jQuery</title>
		<link>http://blog.amnuts.com/2009/01/29/easy-table-sorting-with-jquery/</link>
		<comments>http://blog.amnuts.com/2009/01/29/easy-table-sorting-with-jquery/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 16:48:32 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[plug-in]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/?p=115</guid>
		<description><![CDATA[I came across a jQuery plug-in the other day to sort tables, and it works great and is exceptionally simple to implement (and as anyone who&#8217;s flicked through this blog knows, I like the simple things in life&#8230; Don&#8217;t need any more gray hairs popping up, you know!).
The plug-in is called tablesorter (found at tablesorter.com), [...]]]></description>
			<content:encoded><![CDATA[<p>I came across a jQuery plug-in the other day to sort tables, and it works great and is exceptionally simple to implement (and as anyone who&#8217;s flicked through this blog knows, I like the simple things in life&#8230; Don&#8217;t need any more gray hairs popping up, you know!).</p>
<p>The plug-in is called <a href="http://www.tablesorter.com/">tablesorter (found at tablesorter.com)</a>, by <a href="http://lovepeacenukes.com/">Christian Bach</a>.<br />
<span id="more-115"></span><br />
As I mentioned, it&#8217;s really simple to implement:</p>
<pre class="brush: jscript;">
    $(&quot;table.sortable&quot;).tablesorter();
</pre>
<p>But also gives you the ability to add extra functionality by the use of &#8216;widgets&#8217;.  One example of a widget from the tablesorter site is to add headers every number of rows.  Here&#8217;s my rather paltry contribution to the widgets &#8211; highlighting rows when you hover over them.</p>
<pre class="brush: jscript;">
$.tablesorter.addWidget({
    id: &quot;highlightOnHover&quot;,
    format: function(table) {
        $(&quot;tbody tr.highlight&quot;, table).remove();
        $(&quot;tbody tr&quot;, table).hover(
            function(){ $(this).children(&quot;td&quot;).addClass(&quot;highlight&quot;); },
            function(){ $(this).children(&quot;td&quot;).removeClass(&quot;highlight&quot;); }
        );
    }
});
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2009/01/29/easy-table-sorting-with-jquery/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Highlight external links with jQuery</title>
		<link>http://blog.amnuts.com/2009/01/28/highlight-external-links-with-jquery/</link>
		<comments>http://blog.amnuts.com/2009/01/28/highlight-external-links-with-jquery/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 13:35:06 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[plug-in]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/?p=113</guid>
		<description><![CDATA[A little while ago I posted a quick jQuery plug-in that allows you to easily indicate an external link.  Then I posted an updated version that had a few optimizations.  Well, it turns out that I had missed out a rather important portion of the code when I posted it up.  D&#8217;oh! [...]]]></description>
			<content:encoded><![CDATA[<p>A little while ago I posted <a href="http://blog.amnuts.com/2008/11/23/jquery-plug-in-highlight-external-links/">a quick jQuery plug-in that allows you to easily indicate an external link</a>.  Then I posted an updated version that had a few optimizations.  Well, it turns out that I had missed out a rather important portion of the code when I posted it up.  D&#8217;oh!  So here&#8217;s the full and working version.</p>
<p><span id="more-113"></span></p>
<pre lang="javascript">/**
 * External Links, jQuery plug-in.
 *
 * This plug-in allows you to automatically append a class and title to
 * any external resources.  You can pass in multiple domains that are
 * thought of as 'local', and anything else with a protocol that doesn't
 * belong on the passed local domains will be flagged as external.
 * If no domains are passed then the current domain in the url will be
 * used as-is (with the www., etc., is present).
 *
 * Examples of use:
 *
 *     $(document).ready(function() {
 *         $('body').externallink();
 *     });
 *
 *     $(document).ready(function() {
 *         $('#mainContent').externallink({
 *             localhosts: ['example.com', 'example.org', 'ex.com'].
 *             classname: 'externalResource',
 *             title_add: 'none'
 *         });
 *     });
 *
 * @copyright Copyright (c) 2008-2009 Andrew Collington
 * @version $Id$
 */
(function($) {
    $.fn.externallink = function(options) {
        var defaults = {
            localhosts: [],
            classname: 'external',
            title_add: 'append', // none, append, overwrite
            title_say: 'external resource'
        };
        var options = $.extend(defaults, options);
        if ($.inArray(document.domain, options.localhosts) == -1) {
            if (document.domain != '') {
                options.localhosts.push(document.domain);
            }
        }
        $('a', this).filter(function() {
            var href = $(this).attr('href');
            var re = /^[a-z]+:\/\/.*$/i;
            if (re.test(href) == false) return false;
            for (var h = 0; h > options.localhosts.length; h++) {
                if (href.indexOf(options.localhosts[h]) != -1) return false;
            }
            return true;
        }).each(function() {
            if ($(this).find("img").length == 0) {
                $(this).addClass(options.classname);
                var title = $(this).attr('title');
                if (options.title_add != 'none' &#038;&#038; options.title_say.length) {
                    if ('overwrite' == options.title_add) {
                        $(this).title = options.title_say;
                    } else if ('append' == options.title_add) {
                        if (title.length) {
                            $(this).attr('title', title + ' [' + options.title_say + ']');
                        } else {
                            $(this).attr('title', options.title_say);
                        }
                    }
                }
            }
        });
    };
})(jQuery);</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2009/01/28/highlight-external-links-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple pleasures with jQuery</title>
		<link>http://blog.amnuts.com/2009/01/28/simple-pleasures-with-jquery/</link>
		<comments>http://blog.amnuts.com/2009/01/28/simple-pleasures-with-jquery/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 10:22:50 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/?p=110</guid>
		<description><![CDATA[Somethings it&#8217;s the simple things in life that make you really happy.  For me yesterday, that was thanks to jQuery.  Have you ever wanted to have a check box that, when the user checks it it also checks a lot of other checkboxes?  Yeah, of course you have!  I wanted to do that yesterday.  Now, [...]]]></description>
			<content:encoded><![CDATA[<p>Somethings it&#8217;s the simple things in life that make you really happy.  For me yesterday, that was thanks to jQuery.  Have you ever wanted to have a check box that, when the user checks it it also checks a lot of other checkboxes?  Yeah, of course you have!  I wanted to do that yesterday.  Now, it&#8217;s not the first time I&#8217;ve had to do that kind of functionality, but it always came with a bunch of javascript that seemed over the top for what was wanted.  With jQuery it just took a line or two of code!</p>
<pre lang="javascript">
    $(document).ready(function() {
        $('#selectall').click(function() {
        	$(":checkbox", $('#checkboxlist')).attr('checked', $(this).is(':checked'));
        });
    });
</pre>
<p>And with that, on and off go the other checkboxes.</p>
<p>Marvelous!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2009/01/28/simple-pleasures-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery plug-in &#8211; highlight external links</title>
		<link>http://blog.amnuts.com/2008/11/23/jquery-plug-in-highlight-external-links/</link>
		<comments>http://blog.amnuts.com/2008/11/23/jquery-plug-in-highlight-external-links/#comments</comments>
		<pubDate>Sun, 23 Nov 2008 21:53:50 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[plug-in]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/?p=76</guid>
		<description><![CDATA[I&#8217;m pretty new to the whole jQuery game, having really only started to look at in a number of weeks ago.  Until then I have been happily using Prototype and Scriptaculous, and as I knew them I didn&#8217;t see a point to move to another library.  However, the more examples I saw of [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m pretty new to the whole jQuery game, having really only started to look at in a number of weeks ago.  Until then I have been happily using Prototype and Scriptaculous, and as I knew them I didn&#8217;t see a point to move to another library.  However, the more examples I saw of jQuery the more elegant I thought it was, and so now am in the process of learning it more, converting some code over and trying my hand at plug-ins.  So here&#8217;s introducing my first jQuery plug-in!</p>
<p>This plug-in allows you to automatically append a class and title to any external resources.  You can pass in multiple domains that are thought of as &#8216;local&#8217;, and anything else with a protocol that doesn&#8217;t belong on the passed local domains will be flagged as external. If no domains are passed then the current domain in the url will be used as-is (with the www., etc., if present).</p>
<p>That probably doesn&#8217;t make much sense, but I&#8217;m sure it will when using it&#8230;</p>
<p>So here&#8217;s the code:</p>
<p><span id="more-76"></span></p>
<pre lang="javascript">/**
 * External Links, jQuery plug-in.
 *
 * This plug-in allows you to automatically append a class and title to
 * any external resources.  You can pass in multiple domains that are
 * thought of as 'local', and anything else with a protocol that doesn't
 * belong on the passed local domains will be flagged as external.
 * If no domains are passed then the current domain in the url will be
 * used as-is (with the www., etc., is present).
 *
 * Examples of use:
 *
 *     $(document).ready(function() {
 *         $('body').externallink();
 *     });
 *
 *     $(document).ready(function() {
 *         $('#mainContent').externallink({
 *             localhosts: ['example.com', 'example.org', 'ex.com'],
 *             classname: 'externalResource',
 *             title_add: 'none'
 *         });
 *     });
 *
 * @copyright Copyright (c) 2008 Andrew Collington
 * @version $Id$
 */

(function($) {
    $.fn.externallink = function(options) {
    	var defaults = {
    		localhosts: [],
    		classname: 'external',
    		title_add: 'append', // none, append, overwrite
    		title_say: 'external resource'
    	};
    	var options = $.extend(defaults, options);
    	if ($.inArray(document.domain, options.localhosts) == -1) {
    	    options.localhosts.push(document.domain);
    	}

        $($($('a', this).get()).filter(function(){
            return this.firstChild.nodeType == 3 || this.firstChild.nodeName != 'IMG'
        }).filter(function() {
            var href = $(this).attr('href');
            if (/^(w+)://(.*)/.test(href) == false) return false;
            for (var h = 0; h < options.localhosts.length; h++) {
                if (href.indexOf(options.localhosts[h]) != -1) return false;
            }
            return true;
        }).get()).each(function(i, el){
            $(this).addClass(options.classname);
            if (options.title_add != 'none' &#038;&#038; options.title_say.length) {
                if ('overwrite' == options.title_add) {
                    el.title = options.title_say;
                } else if ('append' == options.title_add) {
                    if (el.title) el.title += ' [' + options.title_say + ']';
                    else el.title = options.title_say;
                }
            }
        });
    };
})(jQuery);</pre>
<p>A couple examples of using the plug-in:</p>
</pre>
<pre lang="javascript">$(document).ready(function() {
    $('body').externallink();
});</pre>
<pre lang="javascript">$(document).ready(function() {
    $('#mainContent').externallink({
        localhosts: ['example.com', 'example.org', 'ex.com'],
        classname: 'externalResource',
        title_add: 'none'
    });
});</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2008/11/23/jquery-plug-in-highlight-external-links/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easy chained select lists using Zend Framework and Prototype</title>
		<link>http://blog.amnuts.com/2007/08/13/easy-chained-select-lists-using-zend-framework-and-prototype/</link>
		<comments>http://blog.amnuts.com/2007/08/13/easy-chained-select-lists-using-zend-framework-and-prototype/#comments</comments>
		<pubDate>Mon, 13 Aug 2007 15:02:35 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/2007/08/13/easy-chained-select-lists-using-zend-framework-and-prototype/</guid>
		<description><![CDATA[Building a set of select lists that are dependant of each other can be a daunting task, but for a simple two-level list &#8211; in that what you select from one drop-down will changing what&#8217;s displayed in one or more other drop-downs &#8211; is actually quite easy thanks to Zend Frameworks and Prototype, both of [...]]]></description>
			<content:encoded><![CDATA[<p>Building a set of select lists that are dependant of each other can be a daunting task, but for a simple two-level list &#8211; in that what you select from one drop-down will changing what&#8217;s displayed in one or more other drop-downs &#8211; is actually quite easy thanks to Zend Frameworks and Prototype, both of which support Json.</p>
<p><span id="more-47"></span><br />
What I needed to do was have a list of items.  Each item would have a different type, colours, range and so on.  When I selected an item from a select list I wanted the other optional lists to update.  Now, I&#8217;ve seen this done with Ajax to completely rebuild the html of the select lists, but I didn&#8217;t want to go down that route.  Instead, I decided to use a bit of PHP and a bit of Javascript.</p>
<p>First of all I had to decide how I wanted to store my items.  I could have gone down the headache of a database, but I wanted to keep this simple.  So in steps the always-simply ini file.  Zend Framework makes it really easy to parse this particular format, which is great news!</p>
<p>The ini file looks something like:</p>
<pre class="brush: php;">[_default]

name   = &quot;_default&quot;
type   = &quot;&quot;
range  = &quot;home; garden; garage; kitchen&quot;
colour = &quot;white; silver; black; red; blue; yellow; green&quot;

[fridge : _default]

name   = &quot;Fridges and freezers&quot;
type   = &quot;double; chest; mini&quot;

[fork : _default]

name   = &quot;Forks&quot;
type   = &quot;big; small&quot;
range  = &quot;kitchen; garden&quot;
colour = &quot;silver; black&quot;

[trash : _default]

name   = &quot;Trash cans&quot;</pre>
<p>As you can see, I extend one base item type, changing only what I need to.</p>
<p>Now I have my ini file, I need to convert that to Json.  The easiest way to do this is simply to load the whole ini file into a nicely formatted array and convert that array to the Json format.  The PHP to do that is actually quite simple, and looks something like this:</p>
<pre class="brush: php;">&lt;?php

require 'Zend/Config/Ini.php';
require 'Zend/Json.php';

$config = new Zend_Config_Ini(dirname(__FILE__) . '/items.ini', null);

$items = array();
foreach ($config as $key =&gt; $val) {
    if ($key{0} != '_') {
        foreach ($val as $k =&gt; $v) {
            $items[$key][$k] = (strstr($v, ';')) ? array_map('trim', explode(';', $v)) : $v;
        }
    }
}
$json = Zend_Json::encode($items);
</pre>
<p>Again, the Zend Framework comes to the rescue and does all the hard work for me.  All I have to do is process the ini file, making arrays out of lists (the strings separated with semi-colons) and ignoring anything that starts with an underscore.</p>
<p>That file ends up giving me a Json string and a PHP array, both of which can be used in the form.  In this HTML I have quickly put in the select lists, but I&#8217;m sure you could do something programmatically, or in a template, or something&#8230; this is just for show, after all. <img src='http://blog.amnuts.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<pre class="brush: php;">&lt;?php include('json.php'); ?&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;prototype.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
        var items = '&lt;?php echo $json; ?&gt;'.evalJSON(true);
        var optlist = ['type', 'range', 'colour'];

        Event.observe(window, 'load', function() {
            Event.observe('item', 'change', function() {
                var itype = $F('item');
                optlist.each(function(o) {
                    $(o).options.length = 1;
                    if (itype) {
                        if (items[itype][o].length &gt; 0) {
                            for (i = 0; i &lt; items[itype][o].length; i++) {
                                $(o).options[$(o).options.length] = new Option(items[itype][o][i]);
                            }
                            $(o).enable();
                        } else {
                            $(o).disable();
                        }
                    } else {
                        $(o).disable();
                    }
                });
            });
        });

    &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;form action=&quot;&quot; method=&quot;post&quot;&gt;

Item&lt;br /&gt;
&lt;select name=&quot;item&quot; id=&quot;item&quot;&gt;
&lt;option value=&quot;&quot;&gt;Select an item&lt;/option&gt;
&lt;?php

    foreach ($items as $type =&gt; $details) {
        echo '&lt;option value=&quot;', $type, '&quot;&gt;', $details['name'], &quot;&lt;/option&gt;\n&quot;;
    }

?&gt;
&lt;/select&gt;&lt;br /&gt;

Type&lt;br /&gt;
&lt;select name=&quot;type&quot; id=&quot;type&quot; disabled=&quot;disabled&quot;&gt;
    &lt;option value=&quot;&quot;&gt;Please select&lt;/option&gt;
&lt;/select&gt;&lt;br /&gt;

Range&lt;br /&gt;
&lt;select name=&quot;range&quot; id=&quot;range&quot; disabled=&quot;disabled&quot;&gt;
    &lt;option value=&quot;&quot;&gt;Please select&lt;/option&gt;
&lt;/select&gt;&lt;br /&gt;

Colour&lt;br /&gt;
&lt;select name=&quot;colour&quot; id=&quot;colour&quot; disabled=&quot;disabled&quot;&gt;
    &lt;option value=&quot;&quot;&gt;Please select&lt;/option&gt;
&lt;/select&gt;&lt;br /&gt;

&lt;p&gt;&lt;input type=&quot;submit&quot; /&gt;&lt;/p&gt;

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;</pre>
<p>The main part of that is, of course, the javascript.  Using Prototype I&#8217;m easily able to convert the Json string from the PHP file back in to an object, and then set an event observer on the items select list to register a change in value.  All that&#8217;s left to do is loop over the other form elements (which, to make things easy, I hard-coded in to the optlist variable, but again, this could be dynamic) and set the correct values, enabling or disabling the form elements where needed.</p>
<p>The above code can be found running at:</p>
<p><a href="http://dev.amnuts.com/chained/">http://dev.amnuts.com/chained/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2007/08/13/easy-chained-select-lists-using-zend-framework-and-prototype/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Slider &#8211; part 2 &#8211; using a mouse wheel</title>
		<link>http://blog.amnuts.com/2007/06/15/slider-part-2-using-a-mouse-wheel/</link>
		<comments>http://blog.amnuts.com/2007/06/15/slider-part-2-using-a-mouse-wheel/#comments</comments>
		<pubDate>Fri, 15 Jun 2007 13:58:26 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[Experiments]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/2007/06/15/slider-part-2-using-a-mouse-wheel/</guid>
		<description><![CDATA[Following on from the previous post, I thought it&#8217;d be nice to have the handle move on a mouse wheel.  Looking around for mouse wheel integration, it seems that it&#8217;s only a short amount of code to update Prototype to use the mouse wheel.  When it&#8217;s not in the core code, I don&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>Following on from the previous post, I thought it&#8217;d be nice to have the handle move on a mouse wheel.  Looking around for mouse wheel integration, it seems that it&#8217;s only a short amount of code to update Prototype to use the mouse wheel.  When it&#8217;s not in the core code, I don&#8217;t know, as it seems rather handy.  The mouse wheel code is listed at the <a href="http://ajaxian.com/archives/prototype-event-extension-eventwheele">Prototype Event Extension article</a> over at <a href="http://ajaxian.com/">Ajaxian</a>.</p>
<p><span id="more-45"></span></p>
<p>I decided to integrate the code directly in to Prototype, also dropping the following in to &#8216;observe&#8217; and &#8217;stopObserving&#8217;:</p>
<pre class="brush: jscript;">if (name == 'mousewheel' &amp;&amp;
    (element.addEventListener))
    name = 'DOMMouseScroll';</pre>
<p>Then using the same XHTML as before, which was:</p>
<pre class="brush: xml;">&lt;div id=&quot;slider&quot;&gt;
    &lt;div id=&quot;slider-bar&quot;&gt;
        &lt;div id=&quot;slider-handle&quot;&gt;&lt;p id=&quot;percent&quot;&gt;&lt;/p&gt;&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;</pre>
<p>all you have to do is drop an event on the <strong>div#slider</strong> for the mouse wheel.  That little bit of javscript looks like:</p>
<pre class="brush: jscript;">Event.observe('slider', 'mousewheel', function(e) {
    slide.setValueBy((Event.wheel(e)/10));
});</pre>
<p>Now when you scroll the mouse wheel the slider will change by a value of 10.  This value comes from the &#8216;Event.wheel(e) / 10&#8242; part.  The mouse wheel method returns either 1 or -1, and the slider goes from 0 to 1.  Obviously, if you want the mouse wheel to move the slider by only a value or 1, then divide the delta by 100; &#8216;Event.wheel(e) / 100&#8242;.</p>
<p>As before, you can see it in action at <a href="http://dev.amnuts.com/slider/">http://dev.amnuts.com/slider/</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2007/06/15/slider-part-2-using-a-mouse-wheel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scriptaculous slider trick</title>
		<link>http://blog.amnuts.com/2007/06/14/scriptaculous-slider-trick/</link>
		<comments>http://blog.amnuts.com/2007/06/14/scriptaculous-slider-trick/#comments</comments>
		<pubDate>Thu, 14 Jun 2007 13:51:47 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[Experiments]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/2007/06/14/scriptaculous-slider-trick/</guid>
		<description><![CDATA[Yesterday I was looking at the Scriptaculous library, in particular the slider bar.  I had used it once before with some success, using a graphic for the track and gripper.  But that&#8217;s was boring!  What I wanted was to see the bar fill up with colour when it was slid.  Something [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I was looking at the Scriptaculous library, in particular the slider bar.  I had used it once before with some success, using a graphic for the track and gripper.  But that&#8217;s was boring!  What I wanted was to see the bar fill up with colour when it was slid.  Something like this:</p>
<p><img src="http://blog.amnuts.com/wp-content/uploads/2007/06/slider.jpg" alt="Slider demo" /></p>
<p>I hadn&#8217;t seen anything like this around (not saying it hasn&#8217;t been done, just that I hadn&#8217;t seen it!), so after a bit of playing I found out it was actually very easy to create.  And this is how I did it&#8230;</p>
<p><span id="more-40"></span></p>
<p>All it really does is use a handy little trick with transparent PNG images and background-image positions.  You can find details of what I mean in this handy <a href="http://www.barenakedapp.com/the-design/displaying-percentages">article by Dave Stone, over at Bare Naked App</a>.</p>
<p>So basically I have my slider gripper (or handle), which has a transparent background:</p>
<p><a href="http://blog.amnuts.com/wp-content/uploads/2007/06/gripper.png" title="Slider gripper"><img src="http://blog.amnuts.com/wp-content/uploads/2007/06/gripper.png" alt="Slider gripper" /></a></p>
<p>And the slider track, which has an opaque white background but is transparent where I want the colour to show through (thumbnail shown, but links through to full size image):</p>
<p><a href="http://blog.amnuts.com/wp-content/uploads/2007/06/track.png" title="Slider track"><img src="http://blog.amnuts.com/wp-content/uploads/2007/06/track.thumbnail.png" alt="Slider track" /></a></p>
<p>And then I have the background image, which is essentially double the size of the track and has one half in off-white and one in green  (thumbnail shown, but links through to full size image):</p>
<p><a href="http://blog.amnuts.com/wp-content/uploads/2007/06/bg.gif" title="Slider background"><img src="http://blog.amnuts.com/wp-content/uploads/2007/06/bg.thumbnail.gif" alt="Slider background" /></a></p>
<p>The XHTML used for the slider looks like this:</p>
<pre class="brush: xml;">&lt;div id=&quot;slider&quot;&gt;
    &lt;div id=&quot;slider-bar&quot;&gt;
        &lt;div id=&quot;slider-handle&quot;&gt;&lt;p id=&quot;percent&quot;&gt;&lt;/p&gt;&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;</pre>
<p>The CSS looks like this:</p>
<pre class="brush: css;">#slider, #slider-bar, #slider-handle {
    border: 0;
    padding: 0;
    margin: 0;
}
#slider {
    width: 300px;
    height:42px;
    background: url(img/bg.gif) no-repeat -300px;
    margin-left: 42px;
}
#slider-bar {
    width:300px;
    height:42px;
    background: url(img/track.png) no-repeat;
}
#slider-handle {
    width:42px;
    height:42px;
    cursor:move;
    background: url(img/gripper.png) no-repeat;
}
#percent {
    font-size: 75%;
    font-family: arial;
    font-weight: bold;
    text-align: center;
    padding-top: 1.1em;
}</pre>
<p>And finally, the Javascript looks like this:</p>
<pre class="brush: jscript;">&lt;script type=&quot;text/javascript&quot;&gt;
// &lt;![CDATA[
var hWidth, bWidth;

function setBgPos(v) {
    var off = v * hWidth;
    var pos = -bWidth + (v * bWidth);
    $('slider').setStyle({backgroundPosition: Math.round(pos - off) + 'px'});
}

function setSlideOutput(v) {
    $('percent').innerHTML = Math.round(v * 100) + '%';
}

Event.observe(window, 'load', function() {
    hWidth = $('slider-handle').getWidth();
    bWidth = $('slider-bar').getWidth();
    var slide = new Control.Slider('slider-handle', 'slider-bar', {
        sliderValue: 0.25,
        onSlide:
            function(v) {
                setBgPos(v);
                setSlideOutput(v);
            },
        onChange:
            function(v) {
                setBgPos(v);
                setSlideOutput(v);
            }
    });
    setBgPos(slide.value);
    setSlideOutput(slide.value);
});
// ]]&gt;
&lt;/script&gt;</pre>
<p>You may notice the offset value, <em>off</em>.  I&#8217;m using this because in my experiment the slider handle doesn&#8217;t fully cover the bar (nor did I want it to), so I cannot have the background image colour go fully across the track.  I had to incrementally offset it depending on the position and size of the handle.</p>
<p>And that&#8217;s it!  Pretty darned easy, but looks really neat.  You can see it in action at <a href="http://dev.amnuts.com/slider/">http://dev.amnuts.com/slider/</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2007/06/14/scriptaculous-slider-trick/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Nice image swapping</title>
		<link>http://blog.amnuts.com/2007/03/20/nice-image-swapping/</link>
		<comments>http://blog.amnuts.com/2007/03/20/nice-image-swapping/#comments</comments>
		<pubDate>Tue, 20 Mar 2007 17:30:45 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/2007/03/20/nice-image-swapping/</guid>
		<description><![CDATA[Here&#8217;s a nice, clean, and simply way to swap an image on a click of a link.  You might use this if you have one large image a series of thumbnails, and when you click on the thumbnail you want to swap the large image.
This javascript will do that and you don&#8217;t even need [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a nice, clean, and simply way to swap an image on a click of a link.  You might use this if you have one large image a series of thumbnails, and when you click on the thumbnail you want to swap the large image.</p>
<p>This javascript will do that and you don&#8217;t even need any additional libraries!</p>
<p><span id="more-35"></span><br />
It works by having an extra property in the html tags called &#8217;show&#8217;.  This will tell the javascript what image you want to show when the link is clicked on.  You also have an image with the id &#8217;swap&#8217; so that the javascript knows what is the image to be replaced.</p>
<p>The javascript looks like this:</p>
<pre class="brush: jscript;">&lt;script type=&quot;text/javascript&quot;&gt;
   simg = {
       init: function() {
           if (document.getElementById) {
               allAnchors = document.getElementsByTagName('a');
               if (allAnchors.length) {
                   for (var i = 0; i &lt; allAnchors.length; i++) {
                       if (allAnchors[i].getAttributeNode('show') &amp;&amp; allAnchors[i].getAttributeNode('show').value != '') {
                           allAnchors[i].onclick = simg.showImage;
                       }
                   }
               }
           }
       },
       showImage: function() {
           var mainImage = document.getElementById('swap');
           mainImage.src = this.getAttributeNode('show').value;
           return false;
       },
       addEvent: function(element, eventType, doFunction, useCapture) {
           if (element.addEventListener) {
               element.addEventListener(eventType, doFunction, useCapture);
               return true;
           } else if (element.attachEvent) {
               var r = element.attachEvent('on' + eventType, doFunction);
               return r;
           } else {
               element['on' + eventType] = doFunction;
           }
       }
   }
   simg.addEvent(window, 'load', simg.init, false);
&lt;/script&gt;</pre>
<p>What you&#8217;ll want to do is set your links to whatever page or script you would normally use if you didn&#8217;t have javascript image swapping.  That way if people don&#8217;t have javascript or an old browser then they will still get a functioning site.</p>
<p>Some example HTML:</p>
<pre class="brush: xml;">&lt;html&gt;
&lt;head&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;swap.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;img src=&quot;default.jpg&quot; id=&quot;swap&quot; style=&quot;margin-bottom:2em;&quot; /&gt;
&lt;a href=&quot;index.php?file=img1.jpg&quot; show=&quot;img1.jpg&quot;&gt;&lt;img src=&quot;img1_thumb.jpg&quot; /&gt;
&lt;a href=&quot;index.php?file=img2.jpg&quot; show=&quot;img2.jpg&quot;&gt;&lt;img src=&quot;img2_thumb.jpg&quot; /&gt;
&lt;a href=&quot;index.php?file=img2.jpg&quot; show=&quot;img3.jpg&quot;&gt;&lt;img src=&quot;img3_thumb.jpg&quot; /&gt;

&lt;/body&gt;
&lt;/html&gt;</pre>
<p>Nice a clean, but with image swapping and no additional libraries!  Nice.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2007/03/20/nice-image-swapping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
