
function dump(obj) {
    var out = '';
    for (var i in obj) {
        out += i + ": " + obj[i] + "\n";
    }
    alert(out);
}

function getPositionByClassName(sClassName){
	if(sClassName=='kwick opt0'){
		return 0;
	}
	if(sClassName=='kwick opt1'){
		return 1;
	}
	if(sClassName=='kwick opt2'){
		return 2;
	}
	if(sClassName=='kwick opt3'){
		return 3;
	}
	if(sClassName=='kwick opt4'){
		return 4;
	}
	if(sClassName=='kwick opt5'){
		return 5;
	}
	if(sClassName=='kwick opt6'){
		return 6;
	}
}


//var myNav = new ImageNav($$('#kwick .kwick'),320,onClick function);
var ImageMenu = new Class({
	
	initialize: function(myElements,options){
		options = Object.extend({
			onClick: Class.empty,
			start: -1,
			openWidth: 0,
			smallWidth: 0,
			itemWidth: 0,
			selected: -1,
			open: -1
		}, options || {});
		
		this.myElements = myElements;
		this.options = options;
		
		options.itemWidth = myElements[0].getStyle('width').toInt();
		options.smallWidth = Math.round(((options.itemWidth*myElements.length)-options.openWidth)/(myElements.length-1));
		var fx = new Fx.Elements(myElements, {wait: false, duration: 400, transition: Fx.Transitions.quadOut});
		myElements.each(function(el, i){
			el.addEvent('mouseover', function(e){
				e = new Event(e).stop();
				el.show();
			});
			el.addEvent('click', function(e){
				iRightNowSelectedId = getPositionByClassName(el.className);
				if(iRightNowSelectedId!=options.open){
					el.select();
				}
				else {
					redirected=0;
					myUrl = window.location.href;
					childStart=myUrl.indexOf("&child_category");
					rootStart=myUrl.indexOf("&root_category");

					if(rootStart>0){
						if(childStart==-1){
							rootEnd=myUrl.length;
						}
						else{
							rootEnd = childStart;
						}
						root_page = myUrl.substr(rootStart+15,rootEnd);
						if(root_page>=45 && root_page<=50){
							window.location='index.php?page=special&root_category=5';
							redirected=1;
							//alert('page');
						}

						if(root_page==91){
							window.location='index.php?page=special&root_category=5';
							redirected=1;
							//alert('page');
						}
					}
					if(myUrl.indexOf('?page=product_category')>0){
						if(redirected==0){
							window.location='?page=special&root_category=0';
							redirected=1;
						}
					}
					//alert(rootStart);
					if(redirected==0){
						window.location=myUrl.substr(0,childStart);
					}

				}
			});
			
			el.addEvent('mouseout', function(e){
				e = new Event(e).stop();
				el.hide();
			});
			
			el.show = function(){
				var obj = {};
				obj[i] = {'width': [el.getStyle('width').toInt(), options.openWidth]};
				myElements.each(function(other, j){
					if (other != el){
						var w = other.getStyle('width').toInt();
						if (w != options.smallWidth) obj[j] = {'width': [w, options.smallWidth]};
					}
				});
				fx.start(obj);
			};
			
			el.hide = function(){
				var obj = {};
				if(options.selected == -1){
					myElements.each(function(el,i){
						obj[i] = {'width': [el.getStyle('width').toInt(), options.itemWidth]};	
					});
				}else{
					myElements.each(function(el,i){
						if(i != options.selected){
							var w = el.getStyle('width').toInt();
							if(w != options.smallWidth){obj[i] = {'width': [w, options.smallWidth]}};
						}else{
							obj[i] = {'width': [el.getStyle('width').toInt(), options.openWidth]};
						}
					});
				}
				fx.start(obj);
			};
			
			el.select = function(){
				if(options.selected == i){options.selected = -1}else{options.selected = i}
				options.onClick(options.selected,options.open);
				options.open = options.selected;
			};
		});
		
		if(options.start != -1){
			myElements[options.start].show();
			myElements[options.start].select();
		}
	},
	
	reset: function(){
		this.options.selected = -1;
		this.options.open = -1;
		this.myElements.each(function(el, i){
			el.hide();
		});
	},
	
	reopen: function(openVal,selectedVal){
		this.options.selected = selectedVal;
		this.options.open = openVal;
	}
});

