/* API - created by Valentin Agachi - http://agachi.name/ */

var api = {
	search: {
		b: null, t: null, n: null, c: null, b2: null,
		sel: {},

		init: function() {
			var o = $('#searchAdvanced');
			if (o.size() <= 0) return;
//			api.search.t = $('#type').bind('change', new Function("api.search.updateType(this);")).get(0);
			api.search.b2 = $('#brand_id2').bind('change', new Function("api.search.updateBrand2(this);")).get(0);
			api.search.n = $('#name').bind('change', new Function("api.search.updateName(this);")).get(0);
			api.search.c = $('#parent_id').bind('change', new Function("api.search.updateCateg(this);")).get(0);
			api.search.b = $('#brand_id').get(0);
//			this$('#brand_id').bind('change', new Function("api.search.updateBrand(this);"));
		},
		updateSelect: function(o, r) {
//			if (!r.length) return;
			var opts = [ $.OPTION({ 'value': '', 'innerHTML': ' - ', 'selected': 'selected' }) ];
			for (var i = 0; i < r.length; i++) {
				opts.push($.OPTION({ 'value': r[i]['id'], 'innerHTML': r[i]['name'] }));
			}
			$(o).empty().append(opts);
		},
		updateBrand2: function(o) {
			if (api.search.b2.selectedIndex == 0) {
				return false;
			}
			api.search.sel['b2'] = api.search.b2.options[api.search.b2.selectedIndex].value;
			apiws.getprod2names([api.search.sel['b2']], function(r){
				api.search.updateSelect(api.search.n, r[0]);
			});
		},
		updateName: function(o) {
			if (api.search.n.selectedIndex == 0) {
				return false;
			}
			api.search.sel['n'] = api.search.n.options[api.search.n.selectedIndex].value;
			apiws.getcategs([api.search.sel['n']], function(r){
				api.search.updateSelect(api.search.c, r[0]);
			});
		},
		updateCateg: function(o) {
			if (api.search.c.selectedIndex == 0) {
				return false;
			}
			api.search.sel['c'] = api.search.c.options[api.search.c.selectedIndex].value;
			apiws.getbrands([api.search.sel['n'], api.search.sel['c']], function(r){
				api.search.updateSelect(api.search.b, r[0]);
			});
		}
	},

	init: function() {
		$('table.prodsMain tbody tr td').bind('click', function() {
			var a = this.parentNode.childNodes[0];
			if (a.childNodes[0].tagName.toLowerCase() == 'a') {
				go(a.childNodes[0].href);
			}
		});

		$('#filter > ul > li').
			bind('mouseover', new Function("this.childNodes[1].style.display='block';")).
			bind('mouseout', new Function("this.childNodes[1].style.display='none';"));
	},

	stripes: function() {
		$.alternate('table.prodsMain tbody', 'tr', 'odd,even', 'oddH,evenH');
		$.alternate('ul.striped', 'li', 'odd,even');
		$.alternate('fieldset.striped', 'div', 'odd,even');
		$.alternate('form.striped', 'dl', 'odd,even');
	}
};

load.register(api.stripes);
load.register(api.search.init);
load.register(api.init);

var loading = {
	active: false,
	obj: null,
	txt: null,
	timer: null,
	
	init: function() {
		loading.obj = document.createElement('div'); 
		loading.obj.id = 'loading';
		loading.obj = $(loading.obj);
		loading.obj.
			addClass('hide').
			ajaxStart(function(){ loading.act(true); }).
			ajaxStop(function(){ loading.act(false); });
		loading.txt = document.createElement('p');
		loading.txt.innerHTML = 'Asteptati...';
		loading.obj.get(0).appendChild(loading.txt);
		document.body.appendChild(loading.obj.get(0));
	},
	act: function(b) {
		if (!loading.obj) loading.init();
		if (b) {
			loading.timer = setTimeout("loading.obj.removeClass('hide');", 150);
		} else {
			clearTimeout(loading.timer);
			loading.obj.addClass('hide');
		}
		loading.active = b;
	}
};
load.register(loading.init);


// jQuery filter
$.g[':']['alternate'] = "((p = m[3].split(',')) && ($.sibling(a,m[3]).n % (p[0]-0) == (p[1]-1)))";

// p = parent, ch = children, cls = classes applied, clsh = classes applied on hover
$.alternate = function(p, ch, cls, clsh) {
	cls = cls.split(',');
	var n = cls.length;
	if (clsh && clsh.length) clsh = clsh.split(',');
	for (var i = 1; i <= n; i++) {
		var o = $(p+' '+ch+':alternate('+n+','+i+')').addClass(cls[i-1]);
		if (clsh) {
			o.each(function(){ this.$alternateHover = clsh[i-1]; }).
			  bind('mouseover', function(){ $(this).addClass(this.$alternateHover); }).
			  bind('mouseout', function(){ $(this).removeClass(this.$alternateHover); });
		}
	}
}/* 4342 4342 */