/* Category Tab Scrolling Logics goes here */
var tabArrayList;
var currentTab;
var vCatTabId;
var vCategoryId;

var vStoryPager;
var isAuthorMode=0;

function publishStory(storyId,vObject,publishFlag)
{
	if(publishFlag==1) 
		showProgress("Publishing..");
	else
		showProgress("Unpublishing..");
	invoke(null,'story:publish',{publish:publishFlag,story_id:storyId},false,
		function(req){
	
			if(req.responseText=='success') {
	
				if(publishFlag==1) {
					$(vObject).innerHTML="unpublish";
					$(vObject).setAttribute("PubSts",0);
				}
				else{
					$(vObject).innerHTML="publish";
					$(vObject).setAttribute("PubSts",1);
				}
					hideProgress();
					showBottomWalls();
			}
			else{
				Message.alert(req.responseText);
	
			}
			}
		);
		
}

function resetFilterTab(vCatId,vDefaultSort){		

	$$('.catFilter').each(function(item){item.className="catFilter";});
	if($("catFilter_" + vCatId)) 	$("catFilter_" + vCatId).className="active catFilter";
	resetStoryFilterSel(vDefaultSort,0);
}
function resetStoryFilterSel(filterID,vMode){			
	if(filterID==0 || filterID=="") filterID=1;
	for(i=1;i<=5;i++) $("filter" + i).className='';
	if ($("filter" + filterID) != null) {
		$("filter" + filterID).className = 'filterSelBold';
		$("filter" + filterID).highlight();
	}		
	if (vMode == 0) resetDefaultFilterSel(filterID);
}

function resetDefaultFilterSel(filterID){			

	if(isAuthorMode==1){
		for(i=1;i<=4;i++) { $("filterSort" + i).className='';  $("filterSort" + i).innerHTML="make Default";}
		if ($("filterSort" + filterID) != null) {
			$("filterSort" + filterID).className = 'filterSelBold';
			$("filterSort" + filterID).innerHTML = "Default";
		}		
	}
	
}

function saveInfo(vMode){

	vcatName=$("txtCatName").value;
	vcatDesc=$("txtCatDesc").value;
	vcatType=$("txtCatType").value;
	$A(document.getElementsByName("default_story")).each(function(item){
		if (item.checked) {
			vcatDefStory = item.value;
			throw $break;
		}
	});
/*	vfontColor=document.getElementsByName("tab_font_color")[0].value;
	vBgColor=document.getElementsByName("tab_bg_color")[0].value; */

	vactualCatid=vCatTabId;
	if(vMode==1) vactualCatid=0;
	if(vcatName==""){
		Message.alert("Category Name  cannot be empty.",'info');
		return false;
	}
	else{
		selStoryList="";
	
		$$('.storySel').each(function(item){
			
			if(item.checked){
				if(selStoryList=="")
					selStoryList=item.value;
				else
					 selStoryList +="," + item.value;
			}
		}

		);
		
		if (vMode == 1) {
			showProgress("Creating category...", true);
		} else {
			showProgress("Saving category...", true);
		}
		invoke(null,"story:map_category", {
			fk_default_story_id: 	vcatDefStory, 
			catid: 					vactualCatid, 
			category:				vcatName, 
			description:			vcatDesc,
			type:					vcatType,
			stories:				selStoryList,
			edit:					1
		}, true, function(req){
			if(req.responseText=="success"){
				hideProgress();	
				location.reload();
			} else {
				hideProgress();
				if(vMode==1)	
					Message.alert("Category could not be created.",'info');
				else
					Message.alert("Category could not be edited.",'info');
			}
		});

	}
}

	

	var tabBarFixedWidth=890;
	var tabArrayList;
	
	function initializeSortability(){
	
		if(isAuthorMode==1){
			Sortable.create("storyCategoryTabs", {
				constraint: "horizontal",
			onUpdate: function() {
			
				new Ajax.Request("?event=channel:updateTabSequence", {
			  			method:"post",
			  			parameters: {
			  				data: Sortable.serialize("storyCategoryTabs")
			  			},
			  			onSuccess: function(req) {
			  				//alert(req.responseText);
			  			}
			  		});
			},
			    scroll: "container"
			  });	
			 Position.includeScrollOffsets = true;
		}
	}
	
	

	function applyTabScrolling(){
	

		var tabBarWidth=0;
		tabArrayList.each(function(item){tabBarWidth=tabBarWidth + item.offsetWidth;});
		if(tabBarWidth>=tabBarFixedWidth){
			$("leftScroll").show();
			$("rightScroll").show();
			$("storyCategoryTabs").style.width=tabBarWidth + "px";
		}
		else{
			$("leftScroll").hide();
			$("rightScroll").hide();
			$("storyCategoryTabs").style.width=tabBarFixedWidth + "px";

		}
		//tabBarActualWidth=tabBarWidth + 25;

	}


	var scrollStep=10;
	var scrollStart = 0;
	var alpha = 0.0;
	var scrollEnd=0;
	var timerLeft="";
	var timerRight="";

		
	function animateScrollItemLeft(){
  		clearTimeout(timerRight);
		alpha += 0.3;	
		var oContainer=$("container");
		oContainer.scrollLeft = scrollStart + (scrollEnd - scrollStart) * alpha;
		if(alpha >= 1){
			oContainer.scrollLeft=scrollEnd;
			clearInterval(timerLeft);
		}
	}

	function animateScrollItemRight(){

		clearTimeout(timerLeft);
		alpha += 0.3;
		var oContainer=$("container");
		oContainer.scrollLeft = scrollStart + (scrollEnd - scrollStart) * alpha;
		if(alpha >= 1){
			oContainer.scrollLeft=scrollEnd;
			clearInterval(timerRight);
	
		}
	}

	function getTabCurIndex(vScrollWidth,vMode){
		
		var curOffsetWidth=0;
		var oContainer=$("container");
		if(vMode==1){
		for(k=0;k<=tabArrayList.length-1;k++){
			currentTab=tabArrayList[k];
			curOffsetWidth= (currentTab.offsetLeft + currentTab.offsetWidth) ;
			vScrollWidth=oContainer.offsetLeft + oContainer.scrollLeft + oContainer.offsetWidth;

			if(curOffsetWidth>vScrollWidth){
				return k;
				break;
			}
		}

			return tabArrayList.length-1;
		}
		else{


			for(k=0;k<=tabArrayList.length-1;k++){
				currentTab=tabArrayList[k];
				curOffsetWidth= (currentTab.offsetLeft + currentTab.offsetWidth) ;
				if(curOffsetWidth>vScrollWidth){
					return k;
					break;
				}
			}
			return 0;
		}
	}
	function scrollDivLeft(id){

		var oContainer=$(id);
		var tabArray=tabArrayList;
		curTabIndex=getTabCurIndex(oContainer.scrollLeft,1);
		currentTab=tabArrayList[curTabIndex];

		scrollStart = oContainer.scrollLeft; 
		scrollEnd=(currentTab.offsetLeft + currentTab.offsetWidth-oContainer.offsetWidth)-oContainer.offsetLeft + 33;

		clearTimeout(timerLeft);
		clearTimeout(timerRight);
		alpha = 0.0;
		timerLeft=setInterval("animateScrollItemLeft()",50);

	}
		
	function scrollDivRight(id){
	
		var oContainer=$(id);
		var tabArray=tabArrayList;
		curTabIndex=getTabCurIndex(oContainer.scrollLeft,2);
		currentTab=tabArrayList[curTabIndex];
		scrollStart = oContainer.scrollLeft;
		scrollEnd=(currentTab.offsetLeft - oContainer.offsetLeft) ;
	
		clearTimeout(timerLeft);
		clearTimeout(timerRight);
		alpha = 0.0;
		
		timerRight=setInterval("animateScrollItemRight()",50);
	
			
	}
		
	function setDefaultSort(vSortId){	
		
		filterBySort(vSortId);
		$("filterSort" + vSortId).blur();
		resetDefaultFilterSel(vSortId);
		$$(".tabs li a").each(function(item) {		
			if (vCatTabId == item.id.replace('catFilter_','')) {				
				item.name = vSortId;
			}
				
		})		
			invoke(null, 'channel:set_default', {sort:vSortId,catid:vCatTabId},false,function(req){
		});

	}
	function filterBySort(vSortId){		
		var sortTypes=["","published","views","alpha","premium","unpublished"];
		$("filter" + vSortId).blur();
		resetStoryFilterSel(vSortId,1);
		$('story_list').innerHTML='';
		invoke($('story_list'), 'story:channelFilter', {type:sortTypes[vSortId]});

	}

	function removeCatTab(vCatTabId){
		//if (!vCatTabId) vCatTabId = window.vCatTabId;
		Message.confirm('Are you sure to remove this category tab? This will remove all the relation to the stories for this category.', function(){
			showProgress("Removing category tab.", true);
			invoke(null,'story:map_category', {catid: vCatTabId, edit:2}, true, function(req) {
				hideProgress();
				location.reload();
			});
		});


	 }
	 
	 function makeCategoryTabActive(vCategoryId,vSortType){

		if(vCategoryId>0){
			resetFilterTab(vCategoryId,vSortType);
		 	vCatTabId=vCategoryId;
			$('tabOption').show();
			var oContainer=$("container");
			var tabArray=tabArrayList;

			currentTab=tabArrayList[activeTabCtr];
			scrollStart = oContainer.scrollLeft; 
			scrollEnd=(currentTab.offsetLeft + currentTab.offsetWidth-oContainer.offsetWidth) -oContainer.offsetLeft + 33;

			clearTimeout(timerLeft);
			clearTimeout(timerRight);
			alpha = 0.0;
			timerLeft=setInterval("animateScrollItemLeft()",50);
		}else{		
			resetFilterTab(0,vSortType);
			var activeState=false;
			var tabLinks=$$(".tabs li a");
			tabLinks.each(function(item){
				if(item.hasClassName('active')==true) activeState=true;
			});
			$('tabOption').hide();
		}
	
		if(activeState==false){
		
		if(tabLinks.length>0) 
		{	tabLinks[0].addClassName("active");
			var actualId=tabLinks[0].id.replace("catFilter_","");
		}
		//showStoriesByCategory(actualId);
		}
		
		
			
	 }