////////////////////////////////////////////////////////////////////////
//  THIS JAVASCRIPT IS WRITTEN BY                                     //
//  BURHAN UDDIN                                                      //
//  USING JAVASCRIPT MOOTOOLS FRAMEWORK                               //
//  THIS FRAMEWORK CAN BE OBTAINED FROM http://www.mootools.net       //
//                                                                    //
//  Email: burhan.bd@gmail.com                                        //
////////////////////////////////////////////////////////////////////////



var DDSPEED = 10;
var DDTIMER = 15;

var details;  
var box;
var ajax;
var listReq = new Request({method: 'get', url: 'list.php'});
var detailsReq = new Request({method: 'get', url: 'details.php'});


// main function to handle the mouse events //
function ddMenu(id,d){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearInterval(c.timer);
  if(d == 1){
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    h.timer = setTimeout(function(){ddCollapse(c)},50);
  }
}

// collapse the menu //
function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(id){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / DDSPEED));
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.height = currh + (dist * d) + 'px';
  c.style.opacity = (currh / c.maxh)/1.2;
  c.style.filter = 'alpha(opacity=' + ((currh * 100 / c.maxh)/1.2) + ')';
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }
}


function showAjax()
{
	ajax = $('ajax');
	ajax.fade(1);
}

function hideAjax()
{
	ajax = $('ajax');
	ajax.fade(0);
}


function adjustOnLoad()
{
	 // get details and box div
	 details = $('details');
	 box = $('box');
	 ajax = $('ajax');
	 backBox = $('back');
	 backtext = $('backText');
	 
	 if(details==null || details==undefined)
	 {
		 details = document.createElement('div');
		 details.id = 'details';
		 $('content').appendChild(details);
	 }
	 details.style.opacity = .00;
  	 details.style.filter = 'alpha(opacity=0)';
	 details.style.display = 'none';
	 
	 box.style.opacity = .00;
  	 box.style.filter = 'alpha(opacity=0)';
	 
	 ajax.style.opacity = .00;
  	 ajax.style.filter = 'alpha(opacity=0)';
	 
	 backBox.style.opacity = .00;
	 backBox.style.filter = 'alpha(opacity=0)';
	 backBox.style.display='';
	 
	 backtext.style.opacity = .00;
	 backtext.style.filter = 'alpha(opacity=0)';
	 	 
	 
    if(!document.all) // style fix for moz
    {
        menubar = $('menubar');
        menubar.style.marginLeft='76px';
    }
}


function loadList(id, cat, subcat)
{	
	
	box = $('box');
	details = $('details');
	titleh = $('titleh1');
	//if(!document.all)
		titleh.fade(0);
	subtitle = $('titleh2');
	//if(!document.all)
		subtitle.fade(0);	
	listReq.send('id='+id);		
	box.fade(0);
	if(details.style.display!='none')
	{
		details.fade(0);
		backBox.fade(0);
	}		
	showAjax();

	listReq.onSuccess =  function(responseText)
	{
		hideAjax();
		titleh.innerHTML = cat;
		subtitle.innerHTML = subcat;

		box.innerHTML = responseText;
		details.fade(0);
		details.style.display='none';
		box.fade(1);
		//if(!document.all)
		{
			titleh.fade(1);
			subtitle.fade(1);
		}
	}
	listReq.onFailure =  function()
	{
		showDialog('Connection Lost!','Failed to communicate with server. Please check your internet connection.','error',2);
	}
	listReq.onCancel =  function()
	{
		showDialog('Connection Lost!','Failed to communicate with server. Please check your internet connection.','error',2);
	}
}


function loadDetails(id, name)
{
	var box = $('box');
	var details = $('details');
	var backBox = $('back');
	details.fade(0);
	details.style.display='none';
	box.fade(0);
	detailsReq.send('id='+id);	
	showAjax();
	detailsReq.onSuccess =  function(responseText)
	{
		details.innerHTML = responseText;
		details.style.display='';
		details.fade(1);
		hideAjax();
		backBox.fade(1);
	}
	detailsReq.onFailure =  function()
	{
		showDialog('Connection Lost!','Failed to communicate with server. Please check your internet connection.','error',2);
	}
	
	detailsReq.onCancel =  function()
	{
		showDialog('Connection Lost!','Failed to communicate with server. Please check your internet connection.','error',2);
	}
}


function showBackText()
{
	text = $('backText');
	text.fade(1);
}

function hideBackText()
{
	text = $('backText');
	text.fade(0);
}

function backToList()
{
	box = $('box');
	details = $('details');
	backBox = $('back');
	
	details.fade(0);
	backBox.fade(0);
	box.fade(1);	
}


window.onerror = function(err)
{
	showDialog('JavaScript Error',err,'error',1);
	return true;
}

