var aMenuData = new Array();			// menu data array
var nCurrentSel = 0;					// current menu selection (set with default)
var url;
var flag=false;
var player_id_from_video_gallery="";
var first=0,sec=0,chan=null,vid_=null,saveCat=0,saveMenu=0,saveChannel,saveClip; //dont erase!!!!!
var odBaAmbatiaFlashFirstChannel="";

function VidGal_updateIndexsByCategoryId(_sChannelId) 
{
	var result = new Array();
	result[0] = -1; // menu item Id
	result[1] = -1; // category id
	var numberOfMenus = aMenuData.length;
	for (menuIndex = 0; menuIndex < numberOfMenus; menuIndex++)
	{
		var numberOfCategories = aMenuData[menuIndex].aCategories.length;
		for (i = 0; i < numberOfCategories; i++)
		{
			if (aMenuData[menuIndex].aCategories[i].sChannelId == _sChannelId)
			{
				result[0] = menuIndex;
				result[1] = i;			
				return result;
			}
		}	
	}
	return result;
}

function VidGal_addMenuItem(_sName,_sChannel) {
	var nIndex = aMenuData.length;
	aMenuData[nIndex] = new Object();				
	aMenuData[nIndex].sName = _sName;	
	aMenuData[nIndex].sChannel = _sChannel;		
	aMenuData[nIndex].aCategories = new Array();	// category objects array
}

function VidGal_findMenuItemIndex(_sChannel) 
{
	var numberOfMenus = aMenuData.length;
	for (i = 0; i < numberOfMenus; i++)
	{
		if (aMenuData[i].sChannel == _sChannel)
		{
			return i;
		}
	}
	return -1;
}

function VidGal_addCategory(nItem, _sName, _sChannelId) {
	var nIndex = aMenuData[nItem].aCategories.length;
	aMenuData[nItem].aCategories[nIndex] = new Object();
	aMenuData[nItem].aCategories[nIndex].sName = _sName;
	aMenuData[nItem].aCategories[nIndex].sChannelId = _sChannelId;
}

function VidGal_findCategoryIndex(nItem,_sChannelId) 
{
	var numberOfCategories = aMenuData[nItem].aCategories.length;
	for (i = 0; i < numberOfCategories; i++)
	{
		if (aMenuData[nItem].aCategories[i].sChannelId == _sChannelId)
		{
			return i;
		}
	}
	return -1;
}
//////////////////// test/debug code //////////////////// --> END

var MENU_CELL_HEIGHT = 25;	// height of menu item (table cell height)
var MENU_FOOTER_HEIGHT = 2;	// height of menu footer
var MENU_ANI_FREQ_MS = 20;	// menu animation frequency in MS
var MENU_ANI_PER = 10;		// menu animation reveal/hide percent per frame
var MENU_ANI_TIME = MENU_ANI_FREQ_MS * MENU_ANI_PER;	// calculate animation time
var isMenuOpen = false;
// return the height of the menu (calculated by menu items)
function VidGal_getMenuHeight() {
	return MENU_CELL_HEIGHT * aMenuData.length + MENU_FOOTER_HEIGHT;
}
// set menu top position (to animate menu)
function VidGal_setMenuTop( nMenuTop ) {
	// set display "none" and then "block" to resolve browser bug
	$('id_PopupMenu').style.display = "none" ;
	$('id_PopupMenuWrapper').style.top = nMenuTop;
	$('id_PopupMenu').style.display = "block" ;
}
// reveal menu - this function calls itself until menu is fully revealed
function VidGal_revealMenu() {
	var nTimePassed = new Date().getTime() - nAniStartTime;	// check time passed to calculate animation
	var nRevealPer = Math.min(Math.round(nTimePassed / MENU_ANI_TIME * 100), 100);	// percent of menu to display
	var nMenuTop = Math.round(-(100 - nRevealPer) * VidGal_getMenuHeight() / 100);
	VidGal_setMenuTop(nMenuTop);
	if (nRevealPer < 100)
		setTimeout(VidGal_revealMenu, MENU_ANI_FREQ_MS);
	else
	{
		VidGal_updateMenuState(false);
		isMenuOpen = true;
	}
}
// hide menu - this function calls itself until menu is fully hidden
function VidGal_hideMenu() {
	var nTimePassed = new Date().getTime() - nAniStartTime; // check time passed to calculate animation
	var nRevealPer = Math.max(Math.round(100 - nTimePassed / MENU_ANI_TIME * 100) , 0); // percent of menu to display
	var nMenuTop = Math.round(-(100 - nRevealPer) * VidGal_getMenuHeight() / 100);
	VidGal_setMenuTop(nMenuTop);
	if (nRevealPer > 0)
		setTimeout(VidGal_hideMenu, MENU_ANI_FREQ_MS);
	else
	{
		$('id_PopupMenu').style.display = "none";
		VidGal_updateMenuState(true);
		isMenuOpen = false;
	}
}

// open pop-up menu
function VidGal_openMenu() {
	nAniStartTime = new Date().getTime();
	VidGal_revealMenu();
}

// close pop-up menu (hide)
function VidGal_closeMenu() {
	nAniStartTime = new Date().getTime();
	VidGal_hideMenu();
}

// generic method to change class for a given object
function VidGal_setClass(obj, newClass) {
	obj.className = newClass;
}

// generic method to set new image
function VidGal_setImage(obj, imgPath) {
	obj.src = imgPath;
}


// update menu categories 
function VidGal_updateCategories(nSel, bSetChannel) {
   	saveCat = nSel;

	if (nSel != -1 && isMenuOpen)
		VidGal_closeMenu();	// make sure menu is closed
	var sHTML = "<table class=\"vid_gal_category_table\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n";
	sHTML += "\t<tr>\n";

	if (nSel == 0)	// if selection is first - add select-seperator before category
		sHTML += "\t\t<td class=\"vid_gal_category_sep_sel_right\"></td>\n"
	else
		sHTML += "\t\t<td class=\"vid_gal_category_sep_empty\"></td>\n"

	for (i = 0; i < aMenuData[nCurrentSel].aCategories.length; i++)
	{
		if (nSel == i)
		{
			sHTML += "\t\t<td class=\"vid_gal_category_sel\">" + aMenuData[nCurrentSel].aCategories[i].sName + "</td>\n";
			sHTML += "\t\t<td class=\"vid_gal_category_sep_sel_left\"></td>\n";
		}
		else
		{
			sHTML += "\t\t<td class=\"vid_gal_category\" onclick=\"VidGal_updateCategories(" + i + ",true)\">" + aMenuData[nCurrentSel].aCategories[i].sName + "</td>\n";
		
			if (nSel == i + 1)
				sHTML += "\t\t<td class=\"vid_gal_category_sep_sel_right\"></td>\n";
			else if (i < aMenuData[nCurrentSel].aCategories.length - 1)
				sHTML += "\t\t<td class=\"vid_gal_category_sep\"></td>\n";
		}
	}
	sHTML += "\t</tr>\n"
	sHTML += "</table>\n"

	$("id_CategoryTable").innerHTML = sHTML;
	if (bSetChannel)
		$("id_ChannelView").setChannel(aMenuData[nCurrentSel].aCategories[nSel].sChannelId);

}

// triggered when user selects a menu item, this method closes the menu and updates the categories
function VidGal_selectMenu(nSel, bNoUpdate) {
	saveMenu = nSel;
	if (isMenuOpen)
		VidGal_closeMenu();	// close menu
	nCurrentSel = nSel;
	$("id_MenuText").innerHTML = aMenuData[nCurrentSel].sName;
	if (bNoUpdate || bNoUpdate == null)
		VidGal_updateCategories(0, true);
}


// create pop-up menu
function VidGal_createMenu() {

	var sHTML = "<div class=\"vid_gal_menu_wrapper\" id=\"id_PopupMenuWrapper\">";
	sHTML += "<div class=\"vid_gal_menu_wrapper_table\">";
	sHTML += "<table class=\"vid_gal_menu_table\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">";


	sHTML += "<table class=\"vid_gal_menu_table\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">";
	
	for (i = 0; i < aMenuData.length; i++)
	{
		if (i < aMenuData.length - 1)
		{
			sHTML += "<tr><td class=\"vid_gal_menu_item\"";
			sHTML += "onmouseover=\"javascript:VidGal_setClass(this,'vid_gal_menu_item vid_gal_menu_item_sel');\"";
			sHTML += "onmouseout =\"javascript:VidGal_setClass(this,'vid_gal_menu_item');\"";
		}
		else
		{
			sHTML += "<tr><td class=\"vid_gal_menu_item_last\"";
			sHTML += "onmouseover=\"javascript:VidGal_setClass(this,'vid_gal_menu_item_last vid_gal_menu_item_sel');\"";
			sHTML += "onmouseout =\"javascript:VidGal_setClass(this,'vid_gal_menu_item_last');\"";
		}
			
		sHTML += "onclick=\"javascript:VidGal_selectMenu(" + i + ");\">";
		sHTML += "<img src=\"" + staticResourcesUriPrefix + "/images/common/bullet_white.gif\" width=\"5\" height=\"5\" />&nbsp; " + aMenuData[i].sName + "</td></tr>";
	}

	sHTML += "</table>";

	sHTML += "</div>";
	sHTML += "<div class=\"vid_gal_menu_footer\"></div>";
	sHTML += "</div>";


	$("id_PopupMenu").innerHTML = sHTML;	// update menu div
	$("id_MenuText").innerHTML = aMenuData[nCurrentSel].sName;
	VidGal_updateMenuState(true);
}

// update the menu state: opened, closed or hidden (when there's a single item in the menu)
function VidGal_updateMenuState(openable)
{
	if (aMenuData.length > 1) 
	{
		if (openable)
			$("id_MenuPlus").innerHTML = "<a href=\"javascript:VidGal_openMenu();\"><img src=\"" + staticResourcesUriPrefix + "/images/common/season_picker_plus.gif\" width=\"13\" height=\"14\" /></a>";
		else
			$("id_MenuPlus").innerHTML = "<a href=\"javascript:VidGal_closeMenu();\"><img src=\"" + staticResourcesUriPrefix + "/images/common/season_picker_minus.gif\" width=\"13\" height=\"14\" /></a>";
	}
	else
		$("id_MenuPlus").innerHTML = "";
}

//////////////////// flash object API ////////////////////

function onChannelRequest( nChannelId ) {
	//alert("onChannelRequest(" + nChannelId + ") : no implementation yet");
	// this function is triggered by the flash movie when a user requests a new node
	// the function connects to the server and gets the corresponding data
	alert ("onChannelRequest");
}


function onClipPlay ( nSubChannelId, nClipId,xml) {
	dText = "";
	clip = nClipId;
	// get the talkbacks in ajax
	submitAjaxTalkbacks(nSubChannelId,nClipId);	
	var i = 0;
	getDescFunc(nClipId,nSubChannelId);	// this function makes the player start
	getRankFunc(nClipId);
	saveClip = nClipId;
	saveChannel = nSubChannelId;
}

function onClipAddToPlaylist ( nChannelId, nClipId ) {
	
	// this function is triggered by the flash movie when a user request to add a clip to the playlist
	// var frm=$("addQuickListFRM");
	// frm.vcmID.value=nClipId;
	// new VignettePortal.AJAXClient().sendForm(document.forms['addQuickListFRM'],addToQuickPlayList)
	submitQuicklistForm('add',nClipId);
}

/*
function sendClipToFriendUrl (req)
{
	var html = req.responseText;
}
*/

function openSendToFriendDiv(id)
{
//alert(saveMenu);
//alert(saveCat);
//alert("sendToFriendFRM_"+id);
	 var frm=$("sendToFriendFRM_"+id);
	// frm.vcmID+"_"+player_id_from_video_gallery.value=saveClip;
	 frm.level_b.value=aMenuData[saveMenu].aCategories[saveCat].sChannelId;
	 frm.level_a.value=aMenuData[saveMenu].sChannel;
	 frm.channelID.value=saveChannel;
	 frm.uri.value=rURI;
//	 alert("222");
	 ShowDiv("divSendToFriend_"+id);
//	 alert("777");
}
