/*jslint browser: true, plusplus: true, sloppy: true, vars: true, white: true */
(function() {
    if (window.WeiboVideoFetcher) {
        return window.WeiboVideoFetcher.fetch();
    }

    function fetch() {
        var host = window.location.host;
        var aTags, tag;
        if (host.indexOf('56.com') >= 0) {
            aTags = document.querySelectorAll("[href*='vxml.56.com/html5']");
            if (aTags.length) {
                tag = aTags[aTags.length - 1];
                return tag.href;
            }
        } else if (host.indexOf('yinyuetai.com') >= 0) {
            aTags = document.querySelectorAll("[href*='yinyuetai.com/uploads/videos']");
            if (aTags.length) {
                tag = aTags[0];
                return tag.href;
            }
        } else {
        	function eventFire(el, etype){
			  if (el.fireEvent) {
			    el.fireEvent('on' + etype);
			  } else {
			    var evObj = document.createEvent('Events');
			    evObj.initEvent(etype, true, false);
			    el.dispatchEvent(evObj);
			  }
			}
        	if (host.indexOf('miaopai.com') >= 0) {
        		var vid = document.querySelector('.vid_img');
        		if (vid) {
        			eventFire(vid, 'click');
        		}
        	}
            var videoTags = document.getElementsByTagName('video');
            var videoTagsCount = videoTags.length;
            if (videoTagsCount) {
                var i, videoTag, srcTags, srcTagsCount, srcTag, j;
                for (i = 0; i < videoTagsCount; i++) {
                    videoTag = videoTags[i];
                    if (videoTag.src.length) {
                        return videoTag.src;
                    }
                    srcTags = videoTag.getElementsByTagName('source');
                    srcTagsCount = srcTags.length;
                    if (srcTagsCount) {
                        for (j = 0; j < srcTagsCount; j++) {
                            srcTag = srcTags[i];
                            if (srcTag.src.length) {
                                return srcTag.src;
                            }
                        }
                    }
                }
            }
        }
        return '';
    }

    var fetcher = {
        fetch: fetch
    };

    window.WeiboVideoFetcher = fetcher;

    return fetch();
}());