var productInfo = false;
var productList = false;
var innerContainer = false;
var _duration = 500;

function initPage()
{
	initAccordions();
	initLightbox();
	initAjaxLinks();
	productList = document.getElementById('shop-list-block');
	productInfo = document.getElementById('shop-info-block');
	innerContainer = document.getElementById('inner-container');
	if(innerContainer)
	{
		innerContainer._height = innerContainer.offsetHeight;
	}
	
	if(productList)
	{
		productList._mootools = new Fx.Styles(productList.id,{duration: _duration});
	}
	if(productInfo)
	{
		productInfo._mootools = new Fx.Styles(productInfo.id,{duration: _duration});
	}
	
}

function initAjaxPage()
{
	initAjaxLinks();
	initTabs();
	initLightbox();
}

function initLightbox()
{
	Lightbox.init();
}

if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);

function openList()
{
	if(productList)
	{
		//alert(1);//'open list'
		if(productList._cTimeout)
		{
			//alert(3);//'clear timeout'
			clearTimeout(productList._cTimeout);
		}
		fixInnerHeight('openList');
		//productList._mootools.custom({'top':[-1*productList.offsetHeight,1]});
		setTimeout('goAfterLoad()',50);
		var _closeList = document.getElementById('close-list');
		if(_closeList)
		{
			_closeList.href = "javascript:;";
			_closeList.onclick = closeList;
		}
		hideSelectBoxes(productList);
	}
}

function goAfterLoad()
{
	if(productList)
	{
		productList._mootools.custom({'top':[-1*productList.offsetHeight,1]});
	}
}

function closeList()
{
	if(productList)
	{
		//alert(2);//'close list'
		productList._cTimeout = setTimeout("fixInnerHeight('')",_duration);
		productList._mootools.custom({'top':[1,-1000]});
	}	
}

function getList(_href)
{
	if(productInfo.offsetTop >= 0)
	{
		closeInfo();
	}	
	if(productList.offsetTop >= 0)
	{
		closeList();
	}	
	getHttpRequest(_href,'','openList()');
}

function openInfo()
{
	if(productInfo)
	{
		if(productInfo._cTimeout)
		{
			clearTimeout(productInfo._cTimeout);
		}
		fixInnerHeight('openInfo');
		productInfo._mootools.custom({'top':[-1*productInfo.offsetHeight,1]});
		var _closeInfo = document.getElementById('close-info');
		if(_closeInfo)
		{
			_closeInfo.href = "javascript:;";
			_closeInfo.onclick = closeInfo;
		}
	}
}
function closeInfo()
{
	if(productInfo)
	{
		productInfo._cTimeout = setTimeout("fixInnerHeight('openList')",_duration);
		productInfo._mootools.custom({'top':[1,-1000]});
	}	
}

function getInfo(_href)
{
	if(productInfo.offsetTop >= 0)
	{
		closeInfo();
	}	
	getHttpRequest(_href,'','openInfo()');
}

function fixInnerHeight(act)
{
	if(act == "openList")
	{
		innerContainer.style.height = Math.max(productList.offsetHeight,innerContainer._height) + "px";
	}
	else if(act == "openInfo")
	{
		innerContainer.style.height = Math.max(productList.offsetHeight,innerContainer._height,productInfo.offsetHeight) + "px";		
	}
	else
	{
		innerContainer.style.height = innerContainer._height + "px";
		showSelectBoxes(productList);	
	}
	
}


function initAccordions()
{

Fx.SlideMenu = Fx.Elements.extend({

       extendOptions: function(options){
             Object.extend(this.options, Object.extend({
                  start: '',
                  minWidth: 160,
                  fixedHeight: false,
                  fixedWidth: false,
                  alwaysHide: false,
                  wait: false,
                  onActive: Class.empty,
                  onBackground: Class.empty,
                  height: false,
                  opacity: true,
                  minOpacity: 1,
                  width: true
             }, options || {}));
       },



       hideThis: function(i){
             if (this.options.width) this.w = {'width': [this.elements[i].offsetWidth, this.options.minWidth || 0]};
             if (this.options.opacity) this.o = {'opacity': [this.now[i]['opacity'] || 1, this.options.minOpacity]};
       },

       _hideThis: function(i){
             if (this.options.width) this.w = {'width': [this.elements[i].offsetWidth, this._minWidth[i] || 0]};
             if (this.options.opacity) this.o = {'opacity': [this.now[i]['opacity'] || 1, this.options.minOpacity]};
       },

       showThis: function(i){
             if (this.options.width) this.w = {'width': [this.elements[i].offsetWidth, this.options.fixedWidth || this.elements[i].scrollWidth]};
             if (this.options.opacity) this.o = {'opacity': [this.now[i]['opacity'] || this.options.minOpacity, 1]};
       },

       showThisHideOpen: function(iToShow){

             if (iToShow != this.previousClick || this.options.alwaysHide){
                  this.previousClick = iToShow;
                  var objObjs = {};
                  var err = false;
                  var madeInactive = false;
                  this.elements.each(function(el, i){
                      this.now[i] = this.now[i] || {};
											if(iToShow == -1)
											{
                         this._hideThis(i);
											}
                      else if (i != iToShow){
                         this.hideThis(i);
                      } else if (this.options.alwaysHide){
                         if (el.offsetWidth == el.scrollWidth){
                           this.hideThis(i);
                           madeInactive = true;
                         } else if (el.offsetWidth == 0){
                           this.showThis(i);
                         } else {
                           err = true;
                         }
                      } else if (this.options.wait && this.timer){
                         this.previousClick = 'nan';
                         err = true;
                      } else {
                         this.showThis(i);
                      }
                      objObjs[i] = Object.extend(this.w, Object.extend(this.o));
                  }, this);
                  if (err) return;
                  if (!madeInactive) this.options.onActive.call(this, this.togglers[iToShow], iToShow);
                  this.togglers.each(function(tog, i){
                      if (i != iToShow || madeInactive) this.options.onBackground.call(this, tog, i);
                  }, this);
                  return this.custom(objObjs);
             }
       }

}); 


	var Boxes = getElementsByClassName(document.getElementById('main-menu'),'li','*');
	var myBoxOpen = getElementsByClassName(document.getElementById('main-menu'),'li','*');

	if(Boxes.length > 0)
	{
		var myEffect = new Fx.SlideMenu(Boxes,myBoxOpen,{duration: 200,

				onBackground: function(tog){
				},
				onActive: function(tog,iToShow){
				}	
			
			});
	}

}

function getElementsByClassName(oElm, strTagName, oClassNames)
{
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var arrRegExpClassNames = new Array();
    if(typeof oClassNames == "object")
    {
        for(var i=0; i<oClassNames.length; i++)
        {
            arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
        }
    }
    else
    {
        arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
    }
    var oElement;
    var bMatchesAll;
    for(var j=0; j<arrElements.length; j++)
    {
        oElement = arrElements[j];
        bMatchesAll = true;
        for(var k=0; k<arrRegExpClassNames.length; k++)
        {
            if(!arrRegExpClassNames[k].test(oElement.className))
            {
                bMatchesAll = false;
                break;                      
            }
        }
        if(bMatchesAll)
        {
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
}


function hideSelectBoxes(object)
{
	
	if (!object) return;
	
	var in_IE6 = false;
	
	if (navigator.appName.substring(0,9) == "Microsoft")
	{ // Check if IE version is 6 or older

		var appVer = navigator.appVersion.toLowerCase();
		var iePos = appVer.indexOf('msie');
		if (iePos !=-1) {
			var is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)));
			var is_major = parseInt(is_minor); 
		}

		if (is_major <= 6) { 
			in_IE6 = true;
		}
	}
		
	if (in_IE6) {
			if (!object.sboxes) {
				object.sboxes = [];
			}

			var ol = getElementX(object);
			var ot = getElementY(object);
			var ow = object.offsetWidth;
			var oh = object.offsetHeight;
		
			var sboxes = document.getElementsByTagName("select");
			for (var i=0; i<sboxes.length; i++)
			{
				var node = sboxes[i].parentNode;
				while (node != object && node.tagName != "BODY") {
					node = node.parentNode;
				}

				var skip = (node == object);
				if (skip) continue;
				var t = getElementY(sboxes[i]);
				var l = getElementX(sboxes[i]);
				var w = sboxes[i].offsetWidth;
				var h = sboxes[i].offsetHeight;
				var ver = false;
				if (t > ot && t < (ot + oh))
					ver = true;
				else if ((t + h) > ot && (t + h) < (ot + oh))
					ver = true;
				var hor = false;
				if (l > ol && l < (ol + ow))
					hor = true;
				else if ((l + w) > ol && (l + w) < (ol + ow))
					hor = true;
				else if (l < ol && (l + w) > ol)
					hor = true;
				if ( sboxes[i].style.visibility != "hidden")
					object.sboxes[object.sboxes.length] = sboxes[i];
			}
			for (var i=0; i<object.sboxes.length; i++)
				object.sboxes[i].style.visibility = "hidden";
		}
}

function showSelectBoxes(object)
{
	if (!object) return;
	if (!object.sboxes) return;
	for (var i=0; i<object.sboxes.length; i++)
		object.sboxes[i].style.visibility = "";
	object.sboxes = [];
}


function getElementX(object) {return getElementC(object, true)}
function getElementY(object) {return getElementC(object, false)}

function getElementC(element, xAxis)
{
	var initialElement = element;
	var c = 0;

	while (element != null)
	{
		c += (xAxis) ? element.offsetLeft : element.offsetTop;
		if (element.style.position == "absolute")
			break;
		else
			element = element.offsetParent;
	}

	var elementWnd = document.window;
	if (!elementWnd) return c;

	if (!elementWnd.frameElement) return c;

	return c + getElementC(elementWnd.frameElement, xAxis);
}



