var currentCategoryIndex=-1;
function ShowCategory(buttonId,categories, categoryIndex)
{
var firstCall=currentCategoryIndex<0;
if(firstCall)currentCategoryIndex=0;

var setButtonId=categoryIndex==="+" || categoryIndex==="-" || buttonId===null;
if(categoryIndex==="+"){currentCategoryIndex++;if(currentCategoryIndex>=categories.length)currentCategoryIndex=categories.length-1;}
else if(categoryIndex==="-"){currentCategoryIndex--;if(currentCategoryIndex<0)currentCategoryIndex=0;}
if(setButtonId){categoryIndex=currentCategoryIndex;buttonId=categories[currentCategoryIndex].name+"Btn";}


var allOffers=$("allOffers");
var originalHeight=allOffers.getHeight();
allOffers.style.height=originalHeight+"px";
buttonId=$(buttonId);
var cursor=$("specialCursor");
if(firstCall){
	cursor.style.marginLeft=buttonId.positionedOffset()[0]+"px";
	cursor.style.width=buttonId.getWidth()+"px";
}else{
	new Effect.Tween(null,cursor.positionedOffset()[0],buttonId.positionedOffset()[0],function(p){cursor.style.marginLeft=p.toString()+"px";});
	new Effect.Tween(null,cursor.getWidth(),buttonId.getWidth(),function(p){cursor.style.width=p.toString()+"px";});
}
cursor.style.display="block";

for(var pI=0;pI<categories.length;pI++){$(categories[pI].name).style.display="none";$(categories[pI].name+"Links").style.display="none";}
var visibleBlock=$(categories[categoryIndex].name);
visibleBlock.style.display="block";
$(categories[categoryIndex].name+"Links").style.display="block";
var offersPanel;
var targetHeight=$(categories[categoryIndex].name).getHeight();
if(firstCall)
	allOffers.style.height=targetHeight.toString()+"px";
else
	new Effect.Tween(null,originalHeight,targetHeight,{afterFinish:setAutoHeight.bind(allOffers)},function(p){allOffers.style.height=p.toString()+"px";});

currentCategoryIndex=categoryIndex;
}

function ShowOffer(offers, offerIndex)
{
var currentIndex;
if(typeof(offers.currentIndex)!="undefined")currentIndex=offers.currentIndex;
else currentIndex=0;
if(offerIndex==="+")offerIndex=currentIndex+1;
else if(offerIndex==="-")offerIndex=currentIndex-1;
if(offerIndex<0)offerIndex=0;
else if(offerIndex>=offers.length)offerIndex=offers.length-1;

var allOffers=$("allOffers");
var originalHeight=allOffers.getHeight();
allOffers.style.height=originalHeight+"px";
offers.currentIndex=offerIndex;
var button;
for(var oI=0;oI<offers.length;oI++){
	button=$(offers[oI].button);
	button.src=button.src.replace(".on.",".off.");
	$(offers[oI].content).style.display="none";
}
var content=$(offers[offerIndex].content);
button=$(offers[offerIndex].button);
button.src=button.src.replace(".off.",".on.");
content.style.display="block";
new Effect.Tween(null,originalHeight,content.getHeight()+40,{afterFinish:setAutoHeight.bind(allOffers)},function(p){allOffers.style.height=p.toString()+"px";});
}

function setAutoHeight(){
this.style.height="";
}
function InitOffers(categories){
  var hash=window.location.hash;
  var buttonId=null;
  var selectedCategory=0,selectedOffer=-1;
  if(typeof(hash)!="undefined" && hash.length){
    var category,oI,offer;
    hash=hash.replace("#","");
    for(var cI=0;cI<categories.length;cI++){
	category=categories[cI];
        for(oI=0;oI<category.offers.length;oI++){
	   offer=category.offers[oI];
           if(offer.code==hash){
		selectedCategory=cI;
		selectedOffer=oI;
		buttonId=category.name+"Btn";
		ShowOffer(category.offers, selectedOffer);
		break;
           }
        }
	if(selectedOffer!=-1)break;
    }
  }

  ShowCategory(buttonId,categories, selectedCategory);

}
