/**
 * @author james.mcconnell
 */

var vlinks = new Array();
vlinks [1] = "crissangel_pullapart.flv";
vlinks [2] = "teabagged.flv";
vlinks [3] = "metalstorm.flv";
vlinks [4] = "mjmoonwalk.flv";
vlinks [5] = "newbike.flv";
vlinks [6] = "painfulcomp.flv";
vlinks [7] = "fattycarwash.flv";
vlinks [8] = "koreanrobinhood.flv";
vlinks [9] = "shrimptreadmill.flv";
vlinks [10] = "asianrobotgirl.flv";
vlinks [11] = "hotweathergirl.flv";
vlinks [12] = "ruffending.flv";
vlinks [13] = "babypanda.flv";
vlinks [14] = "bonfiremishap.flv";
vlinks [15] = "bonzaikitty.flv";
vlinks [16] = "bunnyopener.flv";
vlinks [17] = "carfalls.flv";
vlinks [18] = "drugbust.flv";
vlinks [19] = "funnyfaces.flv";
vlinks [20] = "grannyskydive.flv";
vlinks [21] = "killbillbird.flv";
vlinks [22] = "needyourrun.flv";
vlinks [23] = "pipedream.flv";
vlinks [24] = "sledge_wd40.flv";
vlinks [25] = "whatsahoe.flv";
vlinks [26] = "wiiboxing.flv";

var v_fileheader = "http://www.profile-pimp.net/media/video/";
var subtitle_text = '0:0:1>Write your subtitle #1 here;0:0:10>Write your subtitle #2 here;0:0:20>Write your subtitle #3 here';

var num_videos = 1;
var total_videos = 0;

function getValue(e)
{
	return document.getElementById(e).value;
}

function setValue(e, val)
{
	if (val == 'empty')
	{
		document.getElementById(e).value = '';
	}
	else
	{
		document.getElementById(e).value = val;
	}
}

function hideElement(e)
{
	document.getElementById(e).style.display = 'none';
}

function showElement(e)
{
	document.getElementById(e).style.display = '';
}

function getPlayerValue(player)
{
	var inputs = document.getElementsByTagName('input');
	if (inputs)
	{
		for (var i = 0; i < inputs.length; ++i)
		{
			if (inputs[i].type == "radio" && inputs[i].name == player && inputs[i].checked)
			{
				return inputs[i].value;
			}
		}
	}
}

function add_subtitle(vid)
{
	// Show subtitle div
	showElement("dsub" + vid);
	
	// Hide add subtitle element
	hideElement("add_sub" + vid);
}

function remove_subtitle(vid)
{
	// Hide subtitle div
	hideElement("dsub" + vid);
	
	// Reset subtitle text
	setValue("subtitle" + vid, subtitle_text);
	
	// Show add subtitle button
	showElement("add_sub" + vid);
}

function add_video()
{
	num_videos++;
	
	if (num_videos <= 10)
	{
		showElement("dvideo" + num_videos);
	}
}

function clear_videos()
{
	// Loop through files, clearing videos
	for (var i = 1; i <= 10; i++)
	{
		clear_vid(i);
	}
	
	num_videos = 1;
}

function clear_vid(num)
{
	// Clear filename
	setValue("video" + num, 'http://');
	
	// Clear subtitles
	setValue("subtitle" + num, subtitle_text);
	
	// Hide subtitle div
	hideElement("dsub" + num);

	// Show the add sub button
	showElement("add_sub" + num);
	
	if (num != 1)
	{
		hideElement("dvideo" + num);
	}
}

function generate_code()
{
	var error = false;
	var autostart = '';
	var file_string = '';
	var xml_list = "playlistSource=VARS&";
	
	// Count of videos
	var video_count = 0;
	
	var video_string = '<div><embed src="http://myspace.freeze.com/mediaplayers/mlmp' + getPlayerValue('skins') + '.swf?mediaType=FLV&';
	
	// Now loop through our user files
	for (var i = 1; i <= num_videos; i++)
	{
		var vid = getValue("video" + i);
		
		if (vid == 'http://' || vid == '')
		{
			// Do nothing
		}
		else if (!validateURL(vid))
		{
			error = true;
			showElement("video_error" + i);
		}
		else
		{
			hideElement("video_error" + i);
			++video_count;
			video_string += 'Track' + video_count + '=' + vid + "&";
		}
		
		// Now check for subtitles
		var add_btn = document.getElementById("add_sub" + i);
		if (add_btn.style.display == "none")
		{
			// user added subtitles
			
			var subtitle = getValue("subtitle" + i);
			
			if (subtitle != '' && subtitle != subtitle_text)
			{
				video_string += "Sub" + video_count + "=" + subtitle + "&";
			}
		}
	}
	
	// Now we loop through our predefined videos
	for (var i = 1; i <= 26; i++)
	{
		// We need a new value because these videos start at 1, but the subtitles start at 11
		var num = i + 10;
		var cur_vid = document.getElementById("cb_videos" + i);
		var add_btn = "add_sub" + num;
		if (cur_vid.checked)
		{
			var filename = v_fileheader + vlinks[i];
			++video_count;
			video_string += "Track" + video_count + "=" + filename + "&";
		}
		
		// Check subtitle
		if (document.getElementById(add_btn).style.display == "none")
		{
			// user added subtitles
			var subtitle = getValue("subtitle" + num);
			
			if (subtitle != '' && subtitle != subtitle_text)
			{
				video_string += "Sub" + video_count + "=" + subtitle + "&";
			}
		}
	}
	
	// Validate autostart
	if (document.getElementById("autostart").checked == true)
	{
		autostart = 'Y';
	}
	else
	{
		autostart = 'N';
	}
	
	if (video_count == 0)
	{
		error = true;
		showElement('no_vids');
	}
	else if (video_count > 10)
	{
		error = true;
		showElement('cb_error');
	}
	else
	{
		error = false;
		hideElement('cb_error');
		hideElement('no_vids');
	}
	
	if (!error)
	{
		video_string += 'playlistSource=VARS&subSize=' + getValue('sub_size') + '&autostart=' + autostart + '"';
		video_string += ' quality="high" scale="noscale" wmode="transparent" bgcolor="#ffffff" width="270" height="230" name="MediaPlayer" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></div><a href="http://freepollz.net/ad.php?t=ppn" target="_blank"><img src="http://www.profile-pimp.net/images/pp_k1.gif" alt="Profile-Pimp.Net" style="position: absolute; left: 0px; top: 0px; z-index: 30;" border="0"></a><a href="http://freepollz.com/ad.php?t=cmv" target="_blank"><img border"0" src="http://freepollz.com/button1.gif"></a>';
		
		setValue("gen_code", video_string);
	}
	else
	{
		setValue("gen_code", "empty");
	}
}

function clear_code()
{
	setValue("gen_code", "empty");
}

function validateURL(str)
{
	var regex = new RegExp("(((http|https):\/\/)?(([a-zA-Z0-9]+\.[a-zA-Z0-9\-]+(\.[a-zA-Z]+){1,2})|((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])))+(:[1-9][0-9]*)?)+((\/([a-zA-Z0-9\-\%\~\+]+)?)*)?(\.flv)");
	var result = regex.exec(str);
	
	return result;
}