/*
 * $Author: wayne $
 * $LastChangedBy: wayne $
 * $Revision: 2878 $
 * $Date: 2010-09-30 12:39:59 +0200 (Thu, 30 Sep 2010) $
 */
var tabMenu;
var menuFlyBackTimeout=null;
var resizeTimer=null;

//$(".subnav_" + it).css('text-decoration','underline');

//$(".subnav_sa-news").css('text-decoration','underline');

//alert(it);


function tabHover(idx)
{
	clearTimeout ( menuFlyBackTimeout );
	tabMenu.set("activeIndex", idx);	
	//alert('#mtab'+(idx+1));
	$('#mtab'+(idx+1)).removeClass('yui-hidden');
	$('#mtab'+(idx+1)).css('background-color','#ffffff');
	//$('#navTab'+idx).addClass('selected');
	//$(".yui-nav > li").addClass('selected');
}
function menuFlyBackArm()
{
	menuFlyBackTimeout = setTimeout('doMmenuFlyback();', 3000);
}
function doMmenuFlyback()
{
	if (selectedMenuTabId == -1)
	{

		//tabMenu.set("activeIndex", 0);
		//$('#subnav_extras_hp').css('z-index','3000');
		var pos = $('#divMenu').offset();  
	  	var trgWidth  = $('#divMenu').width();
	  	var trgHeight = $('#divMenu').height();

		$('#divMenu').css('z-index','1000');
		
		$('#mtab0').css('visibility','visible');
		$('#mtab0').css('display','block');
		//$('#mtab0').css('z-index','2000');
		//$('#mtab0').css({backgroundColor: '#fff'});
		$('#mtab0').css('background-color','#ffffff');
		//$('#mtab0').css('border','#000 1px solid ');
		$('#mtab0').css('position','absolute');
		//$("#mtab0").css( { "left": (pos.left) + "px", "top":(pos.top+30) + "px", "width":"1000px"} );
		$('#mtab0').css( 'width','1000px');
		
		$(".yui-nav > li").removeClass('selected');

		$(".yui-content > div").addClass('yui-hidden');

		//alert('fly back should happen');		
		
		
		/*$('#mtab0').css('visibility', 'visible');
		$('#mtab0').css('display', 'block');
		$('#mtab0').css('z-index', '2000');*/
		/*$('#mtab0').css('backgroundColor', '#ffff00');
		document.getElementById('mtab0').style.background =  "#0EA2E0";*/

	}
	else
	{
		tabMenu.set("activeIndex", selectedMenuTabId-1);
	}
}

$(window).bind('resize', function() {
	if ($('#mtab0').css('visibility') == 'visible')
	{
		var pos = $('#divMenu').position();  
	  	var trgWidth  = $('#divMenu').width();
	  	var trgHeight = $('#divMenu').height();
	  	
		$('#divMenu').css('z-index','1000');
		$('#mtab0').css('visibility','visible');
		$('#mtab0').css('display','block');
		$('#mtab0').css('z-index','2');
		$('#mtab0').css({backgroundColor: '#fff'});
		$('#mtab0').css('position','absolute');
		$("#mtab0").css( { "left": (pos.left) + "px", "top":(pos.top+30) + "px", "width":"1000px", "height":"23px"} );
		$(".yui-nav > li").removeClass('selected');
	}
});
function catNav(catPath)
{
		window.location.href=catPath;
}

function _CCM_loadCategoryArticles(categoryID,renderOverride,targetContainer) {
	var myCommand = new jsonCommand("pfl_GetCategoryContent");
	myCommand.parameters.add('categoryID', categoryID);
	myCommand.parameters.add('ignoreCache', true);
	myCommand.parameters.add('renderOverride', renderOverride);
	myCommand.parameters.add('offset', 0);
	myCommand.parameters.add('limit', 100);
	myCommand.parameters.add('targetContainer', targetContainer);
	jsonQuery(myCommand, "_renderCategoryContent");
}

function _renderCategoryContent(ms)
{
	if (ms.data.length == 0) {
		return;
	}
	
	var htmlOutput='<h2>Content in '+ms.catDisplayName+'</h2><hr />'; 
	var dataSet = ms.data;
	var tileClass = 'tile_article';
	for ( var i = 0, len = dataSet.length; i < len; ++i) {
		var row = dataSet[i];
		
		htmlOutput += '<div class="' + tileClass + '" id="pfObj_' + row.AID
				+ '" onmouseover="resTileFocus(\'' + row.AID
				+ '\')" onmouseout="resTileBlur(\'' + row.AID + '\')" >';
		
		htmlOutput += '<div class="tileActions" id="pfObj_actions_'+row.AID+'">';
		htmlOutput += '<a href="JavaScript:_CCM_assignKeywords('+row.AID+',5);"><img src="/images/btn_Keywords.gif" /></a> &nbsp;';
		htmlOutput += '<a href="JavaScript:_CCM_Publish('+row.AID+',5);"><img src="/images/btn_Publish.gif" /></a>&nbsp;';
		htmlOutput += '<br /><a href="JavaScript:_CCM_editContentObject('+row.AID+',5);"><img src="/images/btn_Edit.gif" /></a>&nbsp;';
		htmlOutput += '</div>';
		
		htmlOutput += '<b>'+row.title+'</b><br />';
		
		if (row.subTitle != null)
		{
			htmlOutput += row.subTitle;
		}
		htmlOutput += '</div>';
		
	}
	var target;
	if (ms.targetContainer == null)
		target = document.getElementById('contentView');
	else
		target = document.getElementById(ms.targetContainer);
		
	
	target.innerHTML = htmlOutput;
	
}


function loadCategoryHomePage(categoryID,resultSetSize) {
	if (resultSetSize == null)
	{
		resultSetSize = defaultResultSize;
	}

	var myCommand = new jsonCommand("pfl_GetCategoryHomePage");
	myCommand.parameters.add('categoryID', categoryID);
	myCommand.parameters.add('resultSetSize', resultSetSize);
	myCommand.parameters.add('ignoreCache', false);
	myCommand.parameters.add('targetContainer', 'pageBodyMain');
	jsonQuery(myCommand, "_renderCategoryHomePage");
	
}

function _renderCategoryHomePage(ms) {
	if (ms.data.length == 0) {
		alert("Sorry but it seems that a problem occured when loading articles.\n\nPlease refresh this page.");
		return;
	}
	var htmlOutput='<h2>'+ms.catDisplayName+'</h2><hr />'; 
	var dataSet = ms.data;
	var tileClass = 'tile_article';
	for ( var i = 0, len = dataSet.length; i < len; ++i) {
		var row = dataSet[i];
		
		htmlOutput += '<div class="' + tileClass + '" id="pfObj_' + row.AID
				+ '" onmouseover="resTileFocus(\'' + row.AID
				+ '\')" onmouseout="resTileBlur(\'' + row.AID + '\')" >';
		htmlOutput += '<b>'+row.title+'</b>';
		if (row.author != '')
		{
			htmlOutput += '<br /><em>by '+row.author+'</em>';
		}
		htmlOutput += '<p>'+row.introText+'</p>';
		htmlOutput += '</div>';
	}
	var target = document.getElementById(ms.targetContainer);
	target.innerHTML = htmlOutput;
}

function resTileFocus(ID,objectType,categoryID)
{
	currentSelectedOID= ID;
	if (categoryID != null)
		currentCategoryID  = categoryID;
	
	
	
	var tileID;
	var actionTileID;
	if (objectType != null)
	{
		tileID 			= 'pfObj_'+ID+'_'+objectType;
		actionTileID 	= 'pfObj_actions_'+ID+'_'+objectType;
	}
	else
	{
		 tileID			= 'pfObj_'+ID;
		 actionTileID 	= 'pfObj_actions_'+ID;
	}
	
	
	var target = document.getElementById(tileID);
	//target.style.backgroundColor = '#efefef';

	target = document.getElementById(actionTileID);
	if (target != null)
	{
		target.style.visibility='visible';
		target.style.display='block';
	}
	
}
function resTileBlur(ID,objectType)
{
	var tileID;
	var actionTileID;
	if (objectType != null)
	{
		tileID 			= 'pfObj_'+ID+'_'+objectType;
		actionTileID 	= 'pfObj_actions_'+ID+'_'+objectType;
	}
	else
	{
		 tileID			= 'pfObj_'+ID;
		 actionTileID 	= 'pfObj_actions_'+ID;
	}
	
	var target = document.getElementById(tileID);
	//target.style.backgroundColor = '#ffffff';
	
	tileID = 'pfObj_actions_'+ID;
	target = document.getElementById(actionTileID);
	if (target != null)
	{
		target.style.visibility='hidden';
		target.style.display='none';

	}
}




function _CCM_assignKeywords(contentID,contentTypeID)
{
	var myCommand = new jsonCommand("pfl_GetContent");
	myCommand.parameters.add('contentID', contentID);
	myCommand.parameters.add('contentTypeID', contentTypeID);
	myCommand.parameters.add('getKeywords', true);
	myCommand.parameters.add('panelSizeWidth', '500px');
	myCommand.parameters.add('panelSizeHeight', '600px');
	jsonQuery(myCommand, "_renderContentEditForm");
}
function _CCM_Publish(contentID,contentTypeID)
{
	alert('"Publish" feature not available in this demo');
}
function _CCM_editContentObject(contentID,contentTypeID)
{
	var myCommand = new jsonCommand("pfl_GetContent");
	myCommand.parameters.add('contentID', contentID);
	myCommand.parameters.add('contentTypeID', contentTypeID);
	jsonQuery(myCommand, "_renderContentEditForm");
}

function _renderContentEditForm(ms)
{
	if(ms.panelSizeHeight != null && ms.panelSizeWidth!=null)
	{
		panelSetSize(ms.panelSizeWidth,ms.panelSizeHeight);
	}
	else
		panelSetSize('500px','400px');
	
	var senderID="pfObj_"+ms.contentID;
	panelShow(
			'View / Edit content',
			ms.data,
			'<b>Demo restriction:</b> Only keywords will be saved',false,senderID);
}





function _CCM_saveContentObject(formFieldPrefix)
{
	//TODO We are currently only saving keywords - for quick and nasty demo
	var field = document.getElementById(formFieldPrefix+"_contentID");
	var contentID =field.value;
	
	field = document.getElementById(formFieldPrefix+"_contentTypeID");
	var contentTypeID =field.value;

	field = document.getElementById(formFieldPrefix+"_newKeyword");
	var newKeyword = field.value;

	//field = document.getElementById(formFieldPrefix+"_Keywords");
	//var keywordList = field.value;

	var myCommand = new jsonCommand("pfl_SaveContent");
	myCommand.parameters.add('contentID', contentID);
	myCommand.parameters.add('contentTypeID', contentTypeID);
	myCommand.parameters.add('newKeyword', newKeyword);
	
//	var fields = document.getElementsByTagName("input");
//	for (var i=0; i<fields.length;i++)
//	{
//		var thisField = fields[i];
//		if (thisField.id.indexOf(formFieldPrefix)>-1)
//		{
//			var paramKey =thisField.id.replace(formFieldPrefix+'_',''); 
//			myCommand.parameters.add(paramKey, thisField.value);
//		}
//	}
//	
//	fields = document.getElementsByTagName("textarea");
//	for (var i=0; i<fields.length;i++)
//	{
//		var thisField = fields[i];
//		if (thisField.id.indexOf(formFieldPrefix)>-1)
//		{
//			var paramKey =thisField.id.replace(formFieldPrefix+'_',''); 
//			myCommand.parameters.add(paramKey, thisField.value);
//		}
//	}
		
	jsonQuery(myCommand, "_renderFeedback");
}
