/* * 
 * Copyright (c) 2006 yovisto.com - Search Web Search App. All Rights Reserved.
 *
 *
 */

///////////////////////////////////////////////////////////
// write new tag to database (AJAX)
///////////////////////////////////////////////////////////
function newTag() {
    // show animation until the procedure will be complite
    // $("wait").innerHTML = '<img src="img/wait_blue_of_white_.gif width="16" height="16" hspace="3" vspace="3">';
	doPlay();
    var vid = $("current_vid").value;
    var tag = $('Input_Tag').value;
        
        if (tag!='') {
            $('Input_Tag').value = "";
		    if ($('format').value=='rm')  {
			    var videoPosition = document.RealPlayer.GetPosition();
			} else if ( $('format').value=='google') {
			    var videoPosition = getFlashMovieObject("FlashMovie").GetVariable("myTime")*1000;			 	
			} else if ($('format').value=='flv') {
			   var videoPosition = getApp("FlashMovie").getPosition()*1000;			 	
			} else if ($('format').value=='mov' ) {
				var videoPosition = Math.round(document.QuicktimePlayer.GetTime()*1000/document.QuicktimePlayer.GetTimeScale());
			}
            if (videoPosition>15000) {
                videoPosition = videoPosition -5000; 
            } else {
                videoPosition = 0;
            } 
			
        var url = "/tagging;jsessionid=" + jsid;
		var pars = "tag=" + tag + "&position=" + videoPosition + "&vid=" + vid + "&do=newTag"; 
		var myAjax = new Ajax.Request( url, {
		    method: 'get', 
		    parameters: pars, 
			onComplete: newTagCallback
			});        
	}
}


function newTagCallback(transport) {
    var msg = transport.responseText;
	handleSlider(msg);	
	//$("TagCloudScrollable").innerHTML = msg;
    //$("tags").innerHTML = msg; 
    if (msg == "false"){
        // nothing
    } else {
       // nothing
    }  
}

///////////////////////////////////////////////////////////
// show "myTags" option for tag cloud (AJAX)
///////////////////////////////////////////////////////////
function showMyTags(vid, limit) {

	$("LinkTagCloudLimitTop50").href = 'javascript:showMyTags('+vid+', 50)'; 	
	$("LinkTagCloudLimitAll").href = 'javascript:showMyTags('+vid+',"inf")'; 

    if (document.getElementById) {	   
        // for MS IE
 	    if (_ie) {
	        $('myTags').setAttribute('className', 'Tag_Cloud_Play_Menu_Active');
            $('allTags').setAttribute('className', 'Tag_Cloud_Play_Menu_Disable');  
        } else {
            $('myTags').setAttribute('class','Tag_Cloud_Play_Menu_Active');
            $('allTags').setAttribute('class','Tag_Cloud_Play_Menu_Disable');
        }
        $('Input_Tag').value = "";
       
		var url = "/tagging;jsessionid=" + jsid;
		var pars = "vid=" + vid + "&do=showMyTags&limit=" + limit; 
		var myAjax = new Ajax.Request( url, {
		    method: 'get', 
			parameters: pars, 
			onComplete: showMyTagsCallback
		   });				   
    }		
}

function showMyTagsCallback(transport) {
    var msg = transport.responseText;	
	handleSlider(msg);	
	//$("TagCloud").innerHTML = msg;
    //$("tags").innerHTML = msg; 
    if (msg == "false"){
        // nothing
    } else {
	    // trigger the tag search method 
	    if ($('current_tag_focus').value!='') {
	        searchVideosForTag($('current_tag_focus').value, $('current_tag_focus_id').value);
	    }
    }  
	
	handleTagLimitMenu();
}

///////////////////////////////////////////////////////////
// show "allTags" option for tag cloud (AJAX)
///////////////////////////////////////////////////////////
function showAllTags(vid, limit) {
    $('ScopeTagSearch').value = "all";
	$("LinkTagCloudLimitTop50").href = 'javascript:showAllTags('+vid+', 50)'; 	
	$("LinkTagCloudLimitAll").href = 'javascript:showAllTags('+vid+',"inf")'; 
	
	if ($('myTags')) {
        _setClassValue('myTags', 'Tag_Cloud_Play_Menu_Disable');
	}				
	_setClassValue('allTags', 'Tag_Cloud_Play_Menu_Active');
    
    if ($('Input_Tag')) $('Input_Tag').value = "";
       		
	var url = "/tagging;jsessionid=" + jsid;
	var pars = "vid=" + vid + "&do=showAllTags&limit=" + limit;
	var myAjax = new Ajax.Request( url, {
	    method: 'get', 
		parameters: pars, 
		onComplete: showAllTagsCallback
	   });
}
function showAllTagsCallback(transport) {
    var msg = transport.responseText;
	handleSlider(msg);	
	//$("TagCloud").innerHTML = msg;
    //$("tags").innerHTML = msg; 
    if (msg == "false"){
        // nothing
    } else {
        // trigger the tag search method 
	    if ($('current_tag_focus').value!='') {
	        searchVideosForTag($('current_tag_focus').value, $('current_tag_focus_id').value);
	    }
    }   
	handleTagLimitMenu();
}

///////////////////////////////////////////////////////////////////
// show "currentVideo" for a special video in the tag cloud (AJAX)
///////////////////////////////////////////////////////////////////
function showTagsForThisVideo(vid, limit) {
    $('ScopeTagSearch').value = "current"; 		
	$("LinkTagCloudLimitTop50").href = 'javascript:showTagsForThisVideo('+vid+', 50)'; 	
	$("LinkTagCloudLimitAll").href = 'javascript:showTagsForThisVideo('+vid+',"inf")'; 
	    
	_setClassValue('currentVideo', 'Tag_Cloud_Play_Menu_Active');
	_setClassValue('allVideos', 'Tag_Cloud_Play_Menu_Disable');
		
	if ($('Input_Tag')) $('Input_Tag').value = "";
     		
	var url = "/tagging;jsessionid=" + jsid;
	var pars = "vid=" + vid + "&do=showCurrentVideo&limit="+limit; 
	var myAjax = new Ajax.Request( url, {
	    method: 'get', 
		parameters: pars, 
		onComplete: showTagsForThisVideoCallback
	   });
}
function showTagsForThisVideoCallback(transport) {
    var msg = transport.responseText;
    handleSlider(msg);	 
	//$("TagCloud").innerHTML = msg;
    //$("tags").innerHTML = msg; 
    if (msg == "false"){
        // nothing
    } else {
        // nothing
    }   
	
	handleTagLimitMenu();

}

///////////////////////////////////////////////////////////
// show "allVideos" for all videos in the tag cloud (AJAX)
///////////////////////////////////////////////////////////
function showTagsForAllVideos(limit) {
    $('ScopeTagSearch').value = "all";
	
    $("LinkTagCloudLimitTop50").href = 'javascript:showTagsForAllVideos(50)'; 	
	$("LinkTagCloudLimitAll").href = 'javascript:showTagsForAllVideos("inf")'; 
    
	_setClassValue('currentVideo', 'Tag_Cloud_Play_Menu_Disable');
	_setClassValue('allVideos', 'Tag_Cloud_Play_Menu_Active');
	
    if ($('Input_Tag')) $('Input_Tag').value = "";
		
	var url = "/tagging;jsessionid=" + jsid;
	var pars = "do=showAllVideos&limit="+limit;
	var myAjax = new Ajax.Request( url, {
	    method: 'get', 
		parameters: pars, 
		onComplete: showTagsForAllVideosCallback
	   });	
}
function showTagsForAllVideosCallback(transport) {
    var msg = transport.responseText;
	handleSlider(msg);	
	//$("TagCloud").innerHTML = msg;	
    //$("tags").innerHTML = msg; 
    if (msg == "false"){
        // nothing
    } else {
        // nothing
    }  
	
	handleTagLimitMenu();
}


function handleSlider(msg) {
	$("TagCloudContent").innerHTML = msg;	
    var countTags = msg.match(/searchVideosForTag/gi);	

	if ( msg=='' || countTags.length<60) {	
	    $("TagCloudSliderWrap").style.display = "none";
		_setClassValue('TagCloud', 'Tag_Cloud_Play');
    } else {		
		$("TagCloudSliderWrap").style.display = "inline";
		_setClassValue('TagCloud', 'Tag_Cloud_Play_Scrollable');
    }
}

///////////////////////////////////////////////////////////
// retrieve videos far tag (AJAX)
///////////////////////////////////////////////////////////
function searchVideosForTag(tag, tagID, limit) {
    $('current_tag_focus').setAttribute('value',tag);
	$('current_tag_focus_id').setAttribute('value',tagID);
	if ($(tagID)) {
        _setClassValue(tagID, 'Tag_Search_Focus');
	}
    if (TagID!='') {
        if ($(TagID)) {
			_setClassValue(TagID, '');
        }
    }
    TagID = tagID;
    $("TagSearchContent").innerHTML = '<img src="img/wait_blue_of_white_.gif" width="16" height="16" hspace="3" vspace="3">';
    var current_vid = $('current_vid').value;

    // only for the actual video
    if ($('ScopeTagSearch').value == "current") { 
        var pars = 'tag=' + tag + '&vid=' + current_vid + "&limit=" + limit;
        // for all videos           
    } else {
        var pars = 'tag=' + tag + "&limit=" + limit;        
    }
 
	var url = "/tagsearch;jsessionid=" + jsid;
    
	var myAjax = new Ajax.Request( url, {
	    method: 'get', 
	    parameters: pars, 
	 	onComplete: searchVideosForTagCallback
	   });	
}
function searchVideosForTagCallback(transport) {
    var msg = transport.responseText;
    $("TagSearchContent").innerHTML = msg; 
    if (msg == "false"){
        // nothing
    } else {
        // nothing
    }  
}


///////////////////////////////////////////////////////////
// delete tags
///////////////////////////////////////////////////////////
function deleteTag(tag, vid) {
    if (confirm('Are you sure you want to delete tag?')) {	
		var url = "/deleteTag;jsessionid=" + jsid;
	    var pars = 'tag='+ tag + '&vid='+ vid;            
	    var myAjax = new Ajax.Request( url, {
	        method: 'get', 
	        parameters: pars, 
	 	    onComplete: deleteTagCallback
	       });	
	}
}
function deleteTagCallback(transport) {
    var msg = transport.responseText;
    if (msg == "1"){
        // nothing
    } else {
	    // update the tag cloud 
		showMyTags('');
		//showMyTags(vid);
    }  
}

///////////////////////////////////////////////////////////
// set notes
///////////////////////////////////////////////////////////
function setNote() {
    $("embPlayer").setAttribute('height','200');
    $("embPlayer").setAttribute('width','350');
}


function handleTagLimitMenu() {

	if ($('CountTags')) {
        if ($('CountTags').value<47) {
            $('TagCloudLimitMenu').style.display = "none";
	    } else {
	        $('TagCloudLimitMenu').style.display = "inline";
	    }
	}
}


//slider control
var slider = new Control.Slider('TagCloudSliderHandle', 'TagCloudSliderTrack', {
axis: 'vertical',
	onSlide: function(v) { scrollTagCloud(v, $('TagCloudScrollable'), slider);  },
	onChange: function(v) { scrollTagCloud(v, $('TagCloudScrollable'), slider); }
	});					
//scroll the element based on its width and the slider maximum value
function scrollTagCloud(value, element, slider) {
	element.scrollTop = Math.round(value/slider.maximum*(element.scrollHeight-element.offsetHeight));
}     
