/////////////////////////////////////////////////////////////////////////////////////////
//Controll Flash Player

// some variables to save
var currentPosition;
var currentVolume;
var currentItem;

// these functions are caught by the JavascriptView object of the player.
//function sendEvent(typ,prm) {
//	thisMovie("JW").sendEvent(typ,prm);
//};

function getUpdate(typ,pr1,pr2,pid) {
	if(typ == "time") {
		currentPosition = pr1;
		var id = document.getElementById(typ);
		id.innerHTML = typ+ ": "+Math.round(pr1);
		pr2 == undefined ? null: id.innerHTML += ", "+Math.round(pr2);

		// Auto play in channels
		//if (channelon == 1) {
		//	if (pr2 < 1) {
		//		window.location.href  = channelnext
		//	}
		
		//}
	}
};

function getItemData(idx) {
	var obj = thisMovie("JW").itemData(idx);
	var nodes = "";
	for(var i in obj) {
		nodes += "<li>"+i+": "+obj[i]+"</li>";
	}
	//document.getElementById("data").innerHTML = nodes;
};

// This is a javascript handler for the player and is always needed.
function thisMovie(movieName) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////
// Ajax Call Backs

function initPlay(tags, videoid){
	showrate(videoid,0);
	showfavo(videoid);
	//showcomment();
	loadSideCol(tags,videoid);
	showbookmark(videoid);


}

function loadSideCol(tags, videoid) {
	jQuery.get("/search/findsimilar", { tag: tags, id: videoid },
	  function(data){
	  	//if (videoid == 7812) alert("data loaded" + data);
                jQuery("div#relatedview").html(data);
		jQuery("div.title").corner("round");
	  });

}

function showcomment(videoid){
	new Ajax.Request('/comment/show?videoid='+videoid,
	{
		method:'get',
		onSuccess: function(transport){
			var response = transport.responseText;
			//alert("Success! \n\n" + response);
			$('commentdiv').innerHTML = response;
			$('content').style.height = $("left_col").getHeight() + 'px';
		},
		onFailure: function(){ $('commentdiv').innerHTML = 'Comment is not available ...'; }
	});
}
function postcomment() {
	//alert($('detail').value.length);
	//return;
	if ($('detail').value.length < 3) {
		alert("Comments too short, please try again.");
		return;
	}
	//$('comment_form').hide();
	new Ajax.Request('/comment/postnew',
	{
		method:'post',
		parameters: $('comment'+'_form').serialize(true),
		onSuccess: function(transport){
			var response = transport.responseText;
			//alert("Success! \n\n" + response);
			//showcomment();
			$('comment_form').innerHTML = "Your comment has been posted!";
		},
		onFailure: function(){
			$('ratediv').innerHTML = 'Comments did not get posted ...';
		}
	});
}
function showrate(videoid,voted){
	new Ajax.Request('/rate/show?videoid='+videoid+'&voted=' + voted,
	{
		method:'get',
		onSuccess: function(transport){
			var response = transport.responseText;
			//alert("Success! \n\n" + response);
			$('ratediv').innerHTML = response;
		},
		onFailure: function(){ $('ratediv').innerHTML = 'Something went wrong...'; }
	});
}
function showrate_message(videoid,voted,rateopt,videoid){
        new Ajax.Request('/rate/show?videoid='+videoid+'&voted=' + voted,
        {
                method:'get',
                onSuccess: function(transport){
                        var response = transport.responseText;
                        //alert("Success! \n\n" + response);
                        $('ratediv').innerHTML = "<div id=\"ratediv\" class=\"page_block\" style=\"background-color:#8a898a\">You voted " + rateopt +
 "<br /></div>";
                },
                onFailure: function(){ $('ratediv').innerHTML = 'Something went wrong...'; }
        });
}
function dorate(rateopt, videoid){
	new Ajax.Request('/rate/dorate',
	{
		method:'post',
		parameters: $('vote'+rateopt+'_form').serialize(true),
		onSuccess: function(transport){
			var response = transport.responseText || "no response text";
			//alert("Success! \n\n" + response);
			//showrate(videoid,1);			
			showrate_message(videoid,1,rateopt,videoid);
			//$('ratediv').innerHTML = "You voted " + rateopt;
		},
		onFailure: function(){ $('ratediv').innerHTML = 'Something went wrong...'; }
	});

}
function showfavo(videoid){
	new Ajax.Request('/favorit/show?videoid='+videoid,
	{
		method:'get',
		onSuccess: function(transport){
			var response = transport.responseText;
			//alert("Success! \n\n" + response);
			$('favodiv').innerHTML = response;
		},
		onFailure: function(){ $('favodiv').innerHTML = 'Something went wrong...'; }
	});
}
function dofavo(todo,videoid){
	$('favodiv').innerHTML = "please wait ...";
	new Ajax.Request('/favorit/'+todo+'?id='+videoid,
	{
		method:'get',
		onSuccess: function(transport){
			showfavo(videoid);
		},
		onFailure: function(){ $('ratediv').innerHTML = 'Something went wrong...'; }
	});

}

function showbookmark(id){
	new Ajax.Request('/video/showbookmark?id='+id,
	{
		method:'get',
		onSuccess: function(transport){
			var response = transport.responseText;
			//alert("Success! \n\n" + response);
			$('bookmarklist').innerHTML = response;
		},
		onFailure: function(){ alert('bookmark list not availiable, please contact support'); }
	});
}

function share_with_friends(bt_send, id){
	var emails = $('friendemails').value;
	var friendname = $('friendname').value;
	var yourname  = $('yourname').value;
	var youremail  = $('youremail').value;
	
	new Ajax.Request('/video/shareviaemail?id='+id+'&emails='+emails+"&friendname="+friendname+"&yourname="+yourname+"&youremail="+youremail,
	{
		method:'post',
		onSuccess: function(transport){
			var response = transport.responseText;
			//alert("Success! \n\n" + response);
			$('send_email_result').innerHTML = response;
			if (response.indexOf('Email sent') > 0) {
				bt_send.hide();
			}
		},
		onFailure: function(){ alert('share with friends feature not availiable at this moment, please contact support'); }
	});
	
}
//////////////////////////////////////////////////////////////////////////////////////////////////////

