﻿/*
* jQuery prettyPrint Plugin
* version: 0.0.2
* by Elmer Bulthuis
* (c) 2009 LuvDaSun
*/


(function($) {
    $.fn.prettyprint = function() {

        return this.each(function() {

            var formDecode = function(str, splitter1, splitter2) {
                var result;
                var keyValues;

                if (!splitter1) splitter1 = '&';
                if (!splitter2) splitter2 = '=';

                result = new Object();
                if (str) {
                    keyValues = str.split(splitter1);
                    for (var keyValueIndex = 0; keyValueIndex < keyValues.length; keyValueIndex++) {
                        var keyValue;
                        var key;
                        var value;

                        keyValue = keyValues[keyValueIndex];
                        key = keyValue.substring(0, keyValue.indexOf(splitter2));
                        value = keyValue.substring(key.length + splitter2.length);
                        result[unescape(key)] = unescape(value);
                    }
                }

                return result;
            };

            $(this).contents().each(function() {
                var match;
                var re;
                var lastIndex;
                var node;
                var query;

                if (this.nodeType == 3) {
                    re = /([a-z][a-z\d\+\-\.]*)(?:\:?\/\/([^\/\?\#\s\n\<]*))(?:\/([^\?\#\s\n\<]*))?(?:\?([^\#\s\n\<]*))?(?:\#([^\s\n\<]*))?/gi;
                    lastIndex = 0;
                    while (match = re.exec(this.nodeValue)) {
                        this.parentNode.insertBefore(document.createTextNode(this.nodeValue.substring(lastIndex, match.index)), this);

                        node = null;
                        query = formDecode(match[4]);

                        if (node == null && /(?:\.jpg)|(?:\.jpeg)|(?:\.gif)|(?:\.png)$/i.test(match[3])) {
                            with (node = document.createElement('img')) {
                                setAttribute('class', 'pretty-print');
                                setAttribute('src', match[0]);
                            }
                        }

                        if (node == null && /^www\.youtube\.com$/i.test(match[2]) && /^watch$/i.test(match[3])) {
                            with (node = document.createElement('embed')) {
                                setAttribute('class', 'pretty-print');
                                setAttribute('src', 'http://www.youtube.com/v/' + query.v);
                                setAttribute('type', 'application/x-shockwave-flash');
                            }
                        }

                        if (node == null && /^vids\.myspace\.com$/i.test(match[2])) {
                            with (node = document.createElement('embed')) {
                                setAttribute('class', 'pretty-print');
                                setAttribute('src', 'http://mediaservices.myspace.com/services/media/embed.aspx/m=' + query.videoid + ',t=1,mt=video');
                                setAttribute('type', 'application/x-shockwave-flash');
                            }
                        }

                        if (node == null && /^vimeo\.com$/i.test(match[2])) {
                            with (node = document.createElement('embed')) {
                                setAttribute('class', 'pretty-print');
                                setAttribute('src', 'http://vimeo.com/moogaloop.swf?clip_id=' + match[3]);
                                setAttribute('type', 'application/x-shockwave-flash');
                            }
                        }

                        if (node == null) {
                            with (node = document.createElement('a')) {
                                setAttribute('class', 'pretty-print');
                                setAttribute('href', match[0]);
                                setAttribute('onclick', 'window.open(this.href);return false;');
                                appendChild(document.createTextNode(match[2]));
                            }
                        }

                        //alert(match[0]);
                        this.parentNode.insertBefore(node, this);
                        lastIndex = re.lastIndex;
                    }

                    if (lastIndex) {
                        this.parentNode.insertBefore(document.createTextNode(this.nodeValue.substring(lastIndex)), this);
                        this.parentNode.removeChild(this);
                    }
                }

            });

        });

    };

})(jQuery);
