/* Copyright (c) 2007 Onlyweb Studio | http://www.onlyweb.ru/ */
/*

Author: Miha Zimin 

Big thanks to http://jquery.org/
Big thanks to http://prototypejs.org/

From Russia with love!

*/




var newClass = function() {
	return function() {
		return this.init.apply(this, arguments);
	}
}


var Onlyweb = {};
var App = {};


/**
Static methods for working with images
*/
Onlyweb.Images = {
	
	preload: function(a) {
		var cache = [];
		for(var i = 0; i < a.length; i++) {
			cache[i] = new Image();
			cache[i].src = a[i];
		}
	}

};


App.MainMenu = newClass();
App.MainMenu.prototype = {

  init: function(p) {
    var t = this;
    t.p = p;
    t.firstMenu = $('#' + t.p.firstMenu);
    t.secondMenu = $('#' + t.p.secondMenu);

    t.references = {};
    t.firstMenu.find('.' + t.p.referenceC)
    .each(function(i, v) {
      var el = $(v);
      var id = el.attr('id').replace(t.p.referencePrefix, '');
      t.references[id] = el;
    })
    .click(function(e) {
      return t.referenceClick(e);
    });
    
    t.menus = {};
    t.secondMenu.find('.' + t.p.subMenuC)
    .each(function(i, v) {
      var el = $(v);
      var id = el.attr('id').replace(t.p.subMenuPrefix, '');
      t.menus[id] = el;
    });
  },

  referenceClick: function(e) {
    var t = this;
    var el = $(e.target);
    var id = el.attr('id').replace(t.p.referencePrefix, '');
    t.selectSubmenu(id);
    return false;
  },

  selectSubmenu: function(id) {
    var t = this;
    t.menus[id].css('display', 'block');
    if(t.currentId != undefined) {
      t.menus[t.currentId].css('display', 'none');
      t.references[t.currentId].parents('li.' + t.p.itemC).removeClass('Current');
    }
    t.currentId = id;
    t.menus[t.currentId].css('display', 'block');
    t.references[t.currentId].parents('li.' + t.p.itemC).addClass('Current');
  }
}





App.MagicMenu = newClass();
App.MagicMenu.prototype = {

  init: function(p) {
    var t = this;
    t.p = p;
    t.magicMenu = $('#' + t.p.magicMenu);
    t.magicMenu.mouseout(function(e) {
      return t.magicMenuOut(e);  
    });
    t.magicMenu.mouseover(function(e) {
      return t.magicMenuOver(e); 
    });
    t.selectedLi = t.magicMenu.find('.' + t.p.selectedC);
    t.evalTop();
    t.menuIn = false;
    t.minimized = true;
  },

  evalTop: function() {
    var t = this;

    t.top = 0;

    var b = false;
    t.magicMenu.find('.' + t.p.itemC).each(function(i) {
      if (b) {
        return;
      };
      var li = $(this);
      if (li.hasClass(t.p.selectedC)) {
        b = true;
        return;
      }
      t.top += t.p.step;
    });
    t.magicMenu.find('.' + t.p.itemC).mouseover(function(e) {
      return t.magicMenuOver(e);
    })
    .mousemove(function(e) {
      return t.magicMenuMove(e);
    });
  },


  liIn: function(e) {
    var t = this;
    t.menuIn = true;
    console.log('! over ' + $(e.target).attr('class'));
    return false;
  },

  magicMenuOut: function(e) {
    console.log('out ' + $(e.target).attr('class'));
    var t = this;
    t.menuIn = false;
    if (!t.minimized) {
      setTimeout(function() {
        if (!t.menuIn) {
          t.minimized = true;
          t.magicMenu.addClass(t.p.minimizedC);
          t.magicMenu.css('margin-top', '0');
        }
      }, 1000);
    }
    return false;
  },

  magicMenuMove: function(e) {
    var t = this;
    console.log('move ' + $(e.target).attr('class'));
    t.e = e;
  },

  magicMenuOver: function(e) {
    console.log('over ' + $(e.target).attr('class'));
    var t = this;
    t.menuIn = true;
    if (t.minimized) {
      t.minimized = false;
      t.magicMenu.removeClass(t.p.minimizedC);
      t.magicMenu.css('margin-top', '-' + t.top + 'px');
    }
    return false;
  }
};

console = {};

console.log = function(t) {
  var h = $('#b').html();  
  $('#b').html(t + '<br />' + h);
};


App.preloadImages = function() {
	Onlyweb.Images.preload([

	]);
};

//
//