
	/*
	* videos.js
	* Author(s): Lesya Romanchuk, Alex Baskov, Taras Ozarko 2008.
	*/


	function expandVideo(videoID, jumpToTime)
	{
		window.open('http://www.go211.com/videos/fullPlayer.php?videoID='+videoID+'&jumpToTime='+jumpToTime,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=800, height=600;');
		//alert('test');
	}

	// AJAX methods.
	// Do NOT modify unless you know what you are doing
	///////////////////////////////////////////////////

	// global vars
	var http;
	var d = document;

	function showElement(elmnt)
	{
		if(d.getElementById(elmnt))
		{
			d.getElementById(elmnt).style.display = 'block';
		}
	}
	
	function hideElement(elmnt)
	{
		if(d.getElementById(elmnt))
		{
			d.getElementById(elmnt).style.display = 'none';
		}
	}

	function validateShareFields()
	{
		var email = shareFrm.email.value;
		if (email == "")
		{
			alert("Please, enter email");
			return false;
		}

		if (!validateEmail(email))
		{
			alert("The email address does not appear to be valid. Please check and try again");
			return false;
		}

		return true;
	}

	function validateEmail(email)
	{
		var re, good;
		re=/.+\@.+\..+/;
		good = re.exec(email);
		return (good);
	}

	function badVideoContentReporting(videoID)
	{
		awnd = window.open("/wf/index.php?cmd=bad_video_content_reporting&id=" + videoID,"win",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=430,height=300');
		if (awnd.opener==null) awnd.opener=self;
		awnd.focus();
	}

	function addToFavs()
	{
		if(confirm("Do you really want to add this video to your favorites?"))
		{
			d.forms['add_to_fav_form'].submit();
		}
		return false;
	}

	////////////////////////
	// basic AJAX Methods
	//
	function createRequestObject()
	{
		var ro;
		if (window.XMLHttpRequest)
		{
			ro = new XMLHttpRequest();
		} else
		{
			ro = new ActiveXObject('Msxml2.XMLHTTP');
			if(!ro)
			{
				ro = new ActiveXObject('Microsoft.XMLHTTP');
			}
		}
		return ro;
	}

	function sndReq(url, parameters, method, handleResponse)
	{
		http = createRequestObject();
		http.onreadystatechange = handleResponse;

		if (method == "GET")
		{
			http.open('GET', url + "?" + parameters, true);
			http.send(null);
		}
		else if(method == "POST")
		{
			http.open('POST', url, true);
			http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			http.setRequestHeader("Content-length", parameters.length);
			http.setRequestHeader("Connection", "close");
			http.send(parameters);
		}
	}
	//
	//
	////////////////////////

	////////////////////////
	// Rate Video Routine
	//
	function rateVideo()
	{
		var params = rateVideoRequest();
		if(params != "")
		{
			var url = "/wf/index.php";
			sndReq(url, params, "POST", handleRateVideoResponse);
		}
	}

	function rateVideoCompetition()
	{
		var params = rateVideoCompRequest();
		sndReq("/videos/rateVideoCompetition.php", params, "POST", handleRateVideoResponse);
	}

	function rateVideoCompRequest()
	{
		video_id = d.form_rate.video_id.value;
		competition_id = d.form_rate.competition_id.value;
		user_id = d.form_rate.user_id.value;

		rate = d.form_rate.rate[d.form_rate.rate.selectedIndex].value;
		if (rate == "")
		{
			alert('Please Select Your Rating!');
			return false;
		}

		var res = "";
		res += "video_id=" + video_id;
		res += "&rate=" + rate;
		res += "&competition_id=" + competition_id;
		res += "&user_id=" + user_id;
		return encodeURI(res);
	}

	function rateVideoRequest()
	{
		video_id = d.form_rate.video_id.value;
		for(var i=0; i < d.form_rate.rate.length; i++)
		{
			if(d.form_rate.rate[i].checked)
			{
				rate = d.form_rate.rate[i].value;
			}
		}

		nameDiv = "rate_div";
		loadingMessage = "<b>Loading...</b>";
		d.getElementById(nameDiv).innerHTML = loadingMessage;

		var res = "";
		res += "cmd=rate_video";
		res += "&id=" + video_id;
		res += "&rate=" + rate;
		return encodeURI(res);
	}

	function handleRateVideoResponse()
	{
		if(http.readyState == 4)
		{
			var t = http.responseText;

			if(t != "")
			{
				d.getElementById('rate_div').innerHTML = t;
			} else
			{
				alert ("Error.");
			}
		}
	}
	//
	// end Rate Video
	////////////////////////


	////////////////////////
	// Add Comments
	//

	String.prototype.fetchAssocArray = function (value)
	{
		if(this != "")
		{
			var tmpArr = this.split(value);
			if(tmpArr.length > 0)
			{
				var arr = new Array();
				for(var i = 0; i < tmpArr.length; i++)
				{
					if(i % 2 == 1)
					{
						arr[tmpArr[(i - 1)]] = tmpArr[i];
					}
				}
				return arr;
			}
		}
		return false;
	}
		
	function showNewComments(arr)
	{
		if(arr)
		{
			var tbodyObj = d.getElementById('list_all_comments_table').getElementsByTagName('TBODY')[0];

			var trCollect = tbodyObj.getElementsByTagName('TR');
			var tmpArr = trCollect[0].id.split('_');
			var lastCommentID = tmpArr[1];

			var SHADOW_T = '<table align="center" border="0" cellspacing="0" cellpadding="0"><tr><td><div class="dropShadowTop"><div class="dropShadowRight"><div class="dropShadowBottom"><div class="dropShadowLeft"><div class="dropShadowTopLeft"><div class="dropShadowTopRight"><div class="dropShadowBottomRight"><div class="dropShadowBottomLeft"><div class="dropShadowPadding">';
			var SHADOW_B = '</div></div></div></div></div></div></div></div></div></td></tr></table>';

			commentArr = arr.fetchAssocArray(":|||:");
			var newCommentsCount = commentArr.length;
			for(var i = 0; i < newCommentsCount; i++)
			{
				comment = commentArr[i].fetchAssocArray(":||:");

				newRow = d.createElement("TR");
				newRow.id = "listCommentsTr_" + comment["ID"];

				newTd = d.createElement("TD");
				newTd.style.textAlign = "left";
				newTd.style.verticalAlign = "middle";
				newTd.innerHTML = '<a href="/u/' + comment["USERNAME"] + '" target="_new">' + SHADOW_T + '<img src="' + comment["IMG"] + '" alt="' + comment["BAND_NAME"] + '" title="' + comment["BAND_NAME"] + '" />' + SHADOW_B;
				//newTd.innerHTML += comment["BAND_NAME"];
				newTd.innerHTML += '</a>';
				newRow.appendChild(newTd);

				newTd1 = d.createElement("TD");
				newTd1.style.textAlign = "left";
				newTd1.vAlign = "top";
				newTd1.innerHTML = DASHED_BOX_T + '<b class="siteColor">Posted on: ' + comment["DATE"] + '</b><br /><br />' + comment["COMMENT"] + DASHED_BOX_B;
				newRow.appendChild(newTd1);

				if(lastCommentID == 0)
				{
					tbodyObj.innerHTML = "";
					tbodyObj.appendChild(newRow);
					lastCommentID = comment["ID"];
				}else
				{
					tbodyObj.insertBefore(newRow, tbodyObj.firstChild);
				}
			}

			var viewAllCommentsLink = d.getElementById('viewAllCommentsLink');
			viewAllCommentsLink.style.display = 'block';
			d.getElementById('addCommentsLink').style.display = 'block';
			showTab(viewAllCommentsLink);
		}
		return true;
	}

	function showCommentLoading()
	{
		var loadButton = d.getElementById("addCommentsLoading");
		showTab(loadButton);
		d.getElementById('viewAllCommentsLink').style.display = 'none';
		d.getElementById('addCommentsLink').style.display = 'none';
		return true;
	}

	function submitAddVideoComment()
	{
		var params = addVideoCommentRequest();
		if(params != "")
		{
			var url = "/wf/index.php";
			showCommentLoading();
			sndReq(url, params, "POST", handleAddVideoCommentResponse);
		}
		return true;
	}

	function addVideoCommentRequest()
	{
		comment = d.getElementById('comment').value;
		if(comment == "")
		{
			alert("Comment is empty.");
			return false;
		}
		d.getElementById('comment').value = "";

		var tbodyObj = d.getElementById('list_all_comments_table').getElementsByTagName('TBODY')[0];
		var lastCommentID = tbodyObj.getElementsByTagName('TR')[0].id;
		var tmpArr = lastCommentID.split('_');
		var res = "";
		res += "cmd=video_add_comments_ajax";
		res += "&id=" + d.getElementById('add_video_comment_id').value;
		res += "&last_comment_id=" + tmpArr[1];
		res += "&comment=" + comment;
		return encodeURI(res);
	}

	function handleAddVideoCommentResponse()
	{
		if(http.readyState == 4)
		{
			var t = http.responseText;
			var tmpArr = t.split(':|:');
			if(tmpArr[0] == "ok")
			{
				showNewComments(tmpArr[1]);
			} else
			{
				alert ("Error. Can't add video comments. Please, try later.");
			}
		}
	}

	//
	// end Add Comments
	////////////////////////




	////
	// gets related videos
	//
	function getRelatedVideos(formName)
	{
		if(formName != "")
		{
 			d.forms[formName].submit();
		}
		return false;
	} // /getRelatedVideos()



	////
	// shows all tags...
	//
	function showTags(obj)
	{
		if (obj != null)
		{
			obj.style.height = 'auto';
			obj.style.backgroundColor = '#f6f6f6';
		}

		return false;
	}



	////
	// hides all tags
	//
	function hideTags(obj)
	{
		if (obj != null)
		{
			obj.style.height = '32px';
			obj.style.backgroundColor = '';
		}

		return false;
	}
	
	
	////////////////////////
	// Add Comments
	//
	
	var shareFrm = null;

	function shareVideoSubmit()
	{
		shareFrm = d.getElementById("share_video_form");
		if(!validateShareFields())
		{
			return false;
		}
		var params = shareVideoRequest();
		if(params != "")
		{
			var url = "/wf/index.php";
			shareVideoShow('share_video_loading');
			sndReq(url, params, "POST", shareVideoHandleResponse);
		}
		return true;
	}

	function shareVideoRequest()
	{
		var res = "";
		
		for(var i = 0; i < shareFrm.elements.length; i++)
		{
			res += shareFrm.elements[i].name + "=" + shareFrm.elements[i].value + "&";
		}
		return encodeURI(res);
	}

	function shareVideoHandleResponse()
	{
		if(http.readyState == 4)
		{
			var t = http.responseText;
			var tmpArr = t.split(':|:');
			if(tmpArr[0] == "ok")
			{
				shareVideoShow('share_video_success');
				setTimeout("shareVideoShow('share_video_content')", 2000);
			} else
			{
				alert ("Error. Can't share this video. Please, try later.");
				shareVideoShow('share_video_content');
			}
		}
	}
	
	function shareVideoShow(div)
	{
		hideElement("share_video_success");
		hideElement("share_video_loading");
		hideElement("share_video_content");
		showElement(div);
		return true;
	}

	//
	// end Add Comments
	////////////////////////







