﻿/* ------------------------------------------------------------------------
Class: fixLinksForPrettyPhoto
Use: 
Author: Erika Pedersen (http://www.knowit.se)
Version: 1.0.0

This plugin is to be used with pretty photo lightbox to set the href parameters required for links to movies and webpages.
Must be called before the pretty photo is
------------------------------------------------------------------------- */

(function($) {
    $.fn.extend({
        fixLinksForPrettyPhoto: function(options) {
            var settings = {
                movie_width: 760,
                movie_height: 460
            };

            var options = $.extend(settings, options);

            this.each(function() {
                var href = $(this).attr("href");
                //alert(href);
                // set width and height for the swf, flv and mov links:
                if (href.match(/\b(swf|flv|mov)\b/gi)) {

                    if (!getParam('width', href)) {
                        href = addParam('width', settings.movie_width, href);
                    }
                    if (!getParam('height', href)) {
                        href = addParam('height', settings.movie_height, href);
                    }
                    
                    //change the href:
                    $(this).attr("href", href);

                } else if (!(href.match(/\b(swf|flv|mov|jpeg|jpg|png|gif)\b/gi)) && !(href.match(/youtube\.com\/watch/i)) && !(href.match(/vimeo\.com/i))) {
                    //change the href:
                    $(this).attr("href", addParam("iframe", "true", href));
                }

            });
        }
    }

   );


    function getParam(name, url) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(url);
        return (results == null) ? "" : results[1];
    };

    function addParam(name, value, url) {
        return url + (url.indexOf('?') != -1 ? "&" + name + "=" + value : "?" + name + "=" + value);

    };

})(jQuery);



