var status = 'idle';
var vid = null;
var vids = false;
var vidpt = null;
var videoinput = null;
var lastvidval = null;
var vidprev = null;
var imgcount = -1;
var totalimg = 5;
$(document).ready(function(){



	feedvid = $('p.feedvideo');
	if(feedvid.length > 0) {
		if (feedvid.find('a').html() != "") {
			feedvid.before('<br />'+getVideo(feedvid.find('a').html(),569));
		} 
		feedvid.remove();
		
	}
	

	
});


function getVideo(which,w) {
	var matches = new Array();
	if ((which.indexOf('http'))) {
		which = 'http://'+which;
	} //else {
		//var pattern = new RegExp("^www\.?([\.a-z0-9]+)\.(?:com|tv|net)");
	//}
	var pattern = new RegExp("^http://(?:www\.)?([\.a-z0-9]+)\.(?:com|tv|net)","i");
	matches = pattern.exec(which);
	//console.log(matches);
	if (matches != null) {
		// we have a match
		if (matches[1] == "youtube") {
			return getYouTube(which,w);
		} else if (matches[1] == "vimeo") {
			return getVimeo(which,w);
		} else {
			// error
			return '<p style="padding:7px;">URL incomplete or invalid<br /><img src="/layout/img/blank.gif" height="45" width="10" alt="" /></p>';
		}
	} else {
		// no match -> error
		return '<p style="padding:7px;">URL incomplete or invalid<br /><img src="/layout/img/blank.gif" height="45" width="10" alt="" /></p>';
	}

}

function getYouTube(which,w) {
	var matches = new Array();
	
	//which = 'http://www.youtube.com/watch?v=R7yfISlGLNU'; 
	var pattern = new RegExp("http://(?:www\.)?youtube\.com/watch[\?]v=([_\-a-zA-Z0-9]+)","i");
	//var pattern = new RegExp("^http://(?:www\.)?([\.a-z0-9]+)\.(?:com|tv|net)");
	//console.log(pattern.exec(which));
	matches = pattern.exec(which);
	//console.log(which.match(pattern));
	//console.log(matches);
	if (matches != null) {
		if(which.indexOf("&fmt=22") > 0 ) // Check for HD
		{
			var size = calcWH(w, .58875);
			//size[0] = 569;
			//size[1] = 335;
			// ratio = 0.6694
			return '<object width="' +size[0]+ '" height="' +size[1]+ '"><param value="http://www.youtube.com/v/' +matches[1]+ '&ap=%2526fmt%3D22" name="movie" /><param value="window" name="wmode" /><param value="true" name="allowFullScreen" /><embed width="' +size[0]+ '" height="' +size[1]+ '" wmode="window" allowfullscreen="true" type="application/x-shockwave-flash" src="http://www.youtube.com/v/' +matches[0]+ '&ap=%2526fmt%3D22"></embed></object>';
		}
		else
		{
			var size = calcWH(w, .809411);
			return '<object width="' +size[0]+ '" height="' +size[1]+ '"><param name="movie" value="http://www.youtube.com/v/' +matches[1]+ '&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' +matches[1]+ '&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' +size[0]+ '" height="' +size[1]+ '"></embed></object>';
		}
	} else {
		// no matches -> error
		return '<p style="padding:7px;">URL incomplete or invalid<br /><img src="/layout/img/blank.gif" height="45" width="10" alt="" /></p>';
	}
}

function getVimeo(which,w) {
	var matches = new Array();
	
	// example: http://vimeo.com/127768
	var pattern = new RegExp("http://(?:www\.)?vimeo\.com/([0-9]+)","i");
	matches = pattern.exec(which);
	if (matches != null) {
		var size = calcWH(w, .75);
		return '<object width="' +size[0]+ '" height="' +size[1]+ '"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' +matches[1]+ '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=' +matches[1]+ '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="' +size[0]+ '" height="' +size[1]+ '"></embed></object>';
	} else {
		return '<p style="padding:7px;">URL incomplete or invalid<br /><img src="/layout/img/blank.gif" height="45" width="10" alt="" /></p>';
	}
}


function calcWH(w,r) {
	h = w * r;
	return [w,Math.round(h)];
}


