/*------------------------------------------------------------------------------------
 
	Preloader
	A very simple image preloader object
  
 	Usage:
 	
    Preloader.add(path);
    Preloader.onFinish(func);
    Preloader.load();
    
      path: 		A string or array of strings of image paths to preload
      func:     A function or array of functions to be called after images are loaded

      load():   Start the preloader
      
------------------------------------------------------------------------------------*/

var Preloader = {
  callbacks: [],
  images: [],
  loadedImages: [],
  imagesLoaded: 0,
  
  add: function(image){
    if (typeof image == 'string') this.images.push(image);
    if (typeof image == 'array' || typeof image == 'object'){
      for (var i=0; i< image.length; i++){
        this.images.push(image[i]);
      }
    }
  },
  onFinish: function(func){
    if (typeof func == 'function') this.callbacks.push(func);
    if (typeof func == 'array' || typeof func == 'object'){
      for (var i=0; i< func.length; i++){
        this.callbacks.push(func[i]);
      }
    }
  },
  load: function(){
    for(var i=0; i<this.images.length; i++){
      this.loadedImages[i] = new Image();
      this.loadedImages[i].onload = function(){ Preloader.checkFinished.apply(Preloader) }
      this.loadedImages[i].src = this.images[i];
    }
  },
  
  checkFinished: function(){ 
    this.imagesLoaded++;
    if (this.imagesLoaded == this.images.length) this.fireFinish();
  },
  fireFinish: function(){
    for (var i=0; i<this.callbacks.length; i++){
      this.callbacks[i]();
    }
    this.images = [];
    this.loadedImages = [];
    this.imagesLoaded = 0;
    this.callbacks = [];
  }
}

/*------------------------------------------------------------------------------------*/

var hTimers = new Array();
function toggleHeaderBlock(id)
{

	if (jQuery("#idBlock"+id).is(':hidden'))
	{
		jQuery("#idBlock"+id).show();
	}
	else
	{
		jQuery("#idBlock"+id).hide();
	}
}

function toggleBlock(id)
{

	if (jQuery("#idBlock"+id).is(':hidden'))
	{
		jQuery("#idBlock"+id).show();
		jQuery("#msg").focus();
	}
	else
	{
		jQuery("#idBlock"+id).hide();
	}
}

function showHeaderBlock(id)
{
	for (var i=1;i<=12; i++ )
	{
		if (i!=id) instantHideHeaderBlock(i);
	}
	clearTimeout(hTimers[id]);
	jQuery("#idBlock"+id).show();

	var imgName = getTopImageName(id);
	topmnu('idImgTopMenu'+id, imgName, 1);
}

function hideHeaderBlock(id)
{
	hTimers[id] = setTimeout('hideHeaderBlockByTimeout(\''+id+'\')', 800);
}

function hideHeaderBlockByTimeout(id)
{
	jQuery("#idBlock"+id).hide();
	
	var imgName = getTopImageName(id);
	topmnu('idImgTopMenu'+id, imgName, 0);
}

function instantHideHeaderBlock(id)
{
	if (hTimers[id]) {
		clearTimeout(hTimers[id]);
	}
	jQuery("#idBlock"+id).hide();

	var imgName = getTopImageName(id);
	topmnu('idImgTopMenu'+id, imgName, 0);
}

function getTopImageName(id)
{
	//make string
	id = '' + id;
	switch (id) {
		case '1': return 'm_my_home';
			break;
		case '2': return 'm_my_diet_program';
			break;
		case '3': return 'm_my_workout_program';
			break;
		case '4': return 'm_fitness_world';
			break;
		case '5': return 'm_my_accout';
			break;

		case '11': return 'm_member_login';
			break;
		case '12': return 'm_free_goal';
			break;
	}
}

function topmnu(id, src, isVisible) 
{
	var imgObj = document.getElementById(id);
	if (imgObj){
		imgObj.src = "img/header/" + src + (isVisible ? "_over" : "") + ".gif";
	}
	
}