﻿YAHOO.namespace('search.answers');
YAHOO.search.answers = {
 
    cache : {
        data : {
        },
        setData : function(key, value){
            this.data[key] = value;
        },
        getData : function(key){
            return this.data[key];
        }
    } 
}


var Profiles_Sliding = function(
  pageTotal,
  recordCount,
  widthTotal,
  colum,
  offset_position
  ){
  YAHOO.search.answers.cache.setData('pageTotal', pageTotal);	
  YAHOO.search.answers.cache.setData('boaItemCount', recordCount);
  YAHOO.search.answers.cache.setData('currBoaIndex', 0);
  YAHOO.search.answers.cache.setData('boaTimerState', 1);
  YAHOO.search.answers.cache.setData('boaAnimating', false);
  YAHOO.search.answers.cache.setData('colum', colum);
  YAHOO.search.answers.cache.setData('offset_position', offset_position);
  YAHOO.search.answers.cache.setData('widthTotal', widthTotal);
  
  var liListCount= document.getElementById('sliding_div').getElementsByTagName('li').length;
  YAHOO.search.answers.cache.setData('boaItemCount', liListCount);
  if (liListCount%colum==0)
    YAHOO.search.answers.cache.setData('pageTotal', Math.floor(liListCount/colum));	
  else
  		YAHOO.search.answers.cache.setData('pageTotal', Math.floor(liListCount/colum)+1);

}
var finishFade = function() {
				var el = this.getEl();
				YAHOO.util.Dom.setStyle(el, 'display', 'none');
				YAHOO.util.Dom.setStyle(el, 'opacity', 1);
				YAHOO.search.answers.cache.setData('boaAnimating', false);
}

function displayFeaturedItem(index, forward) {
	currBoaIndex = YAHOO.search.answers.cache.getData('currBoaIndex');
	animating = YAHOO.search.answers.cache.getData('boaAnimating');
 
	if(index != currBoaIndex && !animating) {
		YAHOO.search.answers.cache.setData('boaAnimating', true);
		if(forward == null) {
			forward = (index - currBoaIndex > 0);
		}
		startPos = YAHOO.search.answers.cache.getData('widthTotal');
		if(!forward) {
		 
			startPos *=YAHOO.search.answers.cache.getData('widthTotal');
		}
 
		
		column=YAHOO.search.answers.cache.getData('colum');
		curPageStart=currBoaIndex*column;
		curPageEnd=currBoaIndex*column+column;
		curBeep=currBoaIndex*column;
		newPageStart=index*column;
		newPageEnd=index*column+column;
 	  newBeep=index*column;
 	  itemCount=YAHOO.search.answers.cache.getData('boaItemCount');
    var beep=0;
   
    for(var i=0; i<itemCount; i++) {
      
      if((i+curBeep)>=curPageStart && (i+curBeep)<curPageEnd)
      {
  
         currItem = $("#Profiles_"+(i+curBeep));
		     YAHOO.util.Dom.setStyle(currItem, 'opacity', 1);
			   YAHOO.util.Dom.setStyle(currItem, 'background-color', '#fff');
			   var fadeAnim = new YAHOO.util.Anim(currItem, { opacity: { to: 0 }}, 0.6, YAHOO.util.Easing.easeOut);
			   fadeAnim.onComplete.subscribe(finishFade); 
			   fadeAnim.animate();
      }
      if((i+newBeep)>=newPageStart && (i+newBeep)<newPageEnd)
      {
        newItem = $("#Profiles_"+(i+newBeep));
        YAHOO.util.Dom.setStyle(newItem, 'margin-left', (beep*YAHOO.search.answers.cache.getData('offset_position'))+'px');
        YAHOO.util.Dom.setStyle(newItem, 'position', 'absolute');
		    YAHOO.util.Dom.setStyle(newItem, 'display', 'block');
		    var flyAnim;
		    if(!forward)
		      startPos=-YAHOO.search.answers.cache.getData('widthTotal');
		    flyAnim=new YAHOO.util.Anim(newItem, { left: { from: startPos, to: 0 } }, 0.8, YAHOO.util.Easing.easeBoth); 			  
			  flyAnim.animate();
			  beep++;
      }
		}
	
		if(itemCount>0){
		  YAHOO.search.answers.cache.setData('currBoaIndex', index);
		  resetBoaTimer();
	  }
	}
} 

function previousFeaturedItem() {
	currBoaIndex = YAHOO.search.answers.cache.getData('currBoaIndex');
	pageTotal =YAHOO.search.answers.cache.getData('pageTotal'); 
	if(currBoaIndex > 0) {
		displayFeaturedItem(currBoaIndex - 1, false);
	} else {
		displayFeaturedItem(pageTotal - 1, false);
	}
}

function nextFeaturedItem() {
	currBoaIndex = YAHOO.search.answers.cache.getData('currBoaIndex');
	pageTotal = YAHOO.search.answers.cache.getData('pageTotal'); 
	if(currBoaIndex < pageTotal - 1) {
		displayFeaturedItem(currBoaIndex + 1, true);
	} else {
		displayFeaturedItem(0, true);
	}
}
function startBoaTimer() {
	stopBoaTimer();
	timer = setTimeout("nextFeaturedItem()", 15000);
	YAHOO.search.answers.cache.setData('boaTimer', timer);
	YAHOO.search.answers.cache.setData('boaTimerState', 1);
}
function stopBoaTimer() {
	timer = YAHOO.search.answers.cache.getData('boaTimer');
	if(timer) {
		clearTimeout(timer);
	}
	YAHOO.search.answers.cache.setData('boaTimerState', 0);
}
function resetBoaTimer() {
	boaTimerState = YAHOO.search.answers.cache.getData('boaTimerState');
	if(boaTimerState) {
		stopBoaTimer();
		startBoaTimer();
	}
}
function toggleBoaTimer() {
	boaTimerState = YAHOO.search.answers.cache.getData('boaTimerState');
	if(boaTimerState) {
		stopBoaTimer();
	} else {
		startBoaTimer();
	}
}
