/*
embedSwf v0.4

swfobject needed!!!

broadcast sites:
- youtube			ok
- google video 		ok
- metacafe			ok
- dailymotion		ok
- vidoem			ok
*/
(function ($) {
    $.embedSwf = { version: 0.6 };

    $.embedSwf = function () {


       
        var flashId = 0;
        $('.embedswf').each(function () {
            
            var url = '';
            var width = 640;
            var height = 385;
            var allowfullscreen = true;
            var wmode = 'transparent';
            var quality = 'hight';
            var menu = false;

            var element = this;
            var id = $(element).attr('id');

            if (id.length == 0) {
                id = 'flashcontainer_' + flashId;
                $(element).attr('id', id)
            }
            var attributes = $(element).children('.data').html().split(';');
            for (var i = 0; i < attributes.length; i++) {
                var index = attributes[i].indexOf(':');
                var name = attributes[i].substr(0, index);
                var value = attributes[i].substr(index + 1);

                switch (name) {
                    case 'url':
                        url = clearUrl(value);
                        break;
                    case 'width':
                        width = value;
                        break;
                    case 'height':
                        height = value;
                        break;
                    case 'allowfullscreen':
                        allowfullscreen = value;
                        break;
                    case 'wmode':
                        wmode = value;
                        break;
                    case 'quality':
                        quality = value;
                        break;
                    case 'menu':
                        menu = value;
                        break;
                }
            }



            var flashvars = {};
            flashvars.siteUrl = siteUrl;
            flashvars.photoUrl = siteUrl + "transitions_header_photos.xml";
            flashvars.configUrl = siteUrl + "transitions_header_config.xml"
            var params = {};
            params.wmode = wmode;
            params.quality = quality;
            var attributes = {};
            attributes.id = id + "_flash";
           
            swfobject.embedSWF(url, id, width, height, "9.0.0", false, flashvars, params, attributes);
            flashId += 1;
        })
        function clearUrl(url) {
            //Sites
            var youtube = url.indexOf('youtube');
            var googlevideo = url.indexOf('video.google');
            var metacafe = url.indexOf('metacafe');
            var dailymotion = url.indexOf('dailymotion');
            var vimeo = url.indexOf('vimeo');



            var site = false;

            var starttag = '';
            var endtag = '';
            var urlprefix = '';
            var urlsuffix = '';

            if (youtube != -1) { starttag = 'v='; endtag = '&'; urlprefix = 'http://www.youtube.com/v/'; site = true }
            else if (googlevideo != -1) { starttag = 'docid='; endtag = '&'; urlprefix = 'http://video.google.com/googleplayer.swf?docid='; site = true }
            else if (metacafe != -1) { starttag = 'watch/'; endtag = '/'; urlprefix = 'http://www.metacafe.com/fplayer/'; urlsuffix = '/rodney_mullen.swf'; site = true }
            else if (dailymotion != -1) { starttag = 'video/'; endtag = '_'; urlprefix = 'http://www.dailymotion.com/swf/video/'; site = true }
            else if (vimeo != -1) { starttag = 'http://vimeo.com/'; urlprefix = 'http://vimeo.com/moogaloop.swf?clip_id='; site = true }





            if (site) {
                var startpos = url.indexOf(starttag) + starttag.length;
                if (startpos != -1) {
                    var endpos = url.indexOf(endtag, startpos)
                    if (endtag.length == 0 || endpos == -1) {
                        endpos = url.length;
                    }
                    var id = url.substr(startpos, endpos - startpos)
                    url = urlprefix + id + urlsuffix;
                }
            }



            return url;
        }
    };
})(jQuery);


