var Pix = {

	div: null,
	imgs: [],
	line: null,
	last: null,
	
	init: function(){
		Pix.div = $('image');
		Pix.imgs = $$('#thumbline a.thumb');
		Pix.line = $('thumbline');
	},
	
	
	show: function(obj, width, height){
		if(!Pix.div){ Pix.init(); }
		var img = $(obj).down('img');
		var url = img.src.replace('_t.jpg','.jpg');
		
		var wh = 'width="'+width+'" height="'+height+'"';
		width  = parseInt(width);
		height = parseInt(height);
		var k  = width/height;
		if(k>1 && height<480){
			wh = 'height="480"';
		}else if(k<=1 && width<640){
			wh = 'width="640"';
		}
		Pix.div.innerHTML = '<img src="'+url+'" '+wh+' />';
		
		if(Pix.last){
			Pix.last.className = 'thumb';
		}
		obj.className = 'thumb selected';
		Pix.last = obj;		
		
		var ind = parseInt(obj.id.replace('pic',''));
		if(ind>3 && ind<(Pix.imgs.length-3)){
			Pix.focus(ind);
		}
		
	},
	
	prev: function(){
		if(!Pix.div){ Pix.init(); }
		if(!Pix.last){ return; }
		var ind = parseInt(Pix.last.id.replace('pic',''));
		ind--;
		if(ind<0){ return; }
		
		try{
		var segm = Pix.imgs[ind].getAttribute('onclick').match(/'[0-9]+'/g);
		var w = segm[0].replace("'","");
		var h = segm[1].replace("'","");
		Pix.show(Pix.imgs[ind], w, h);
		}catch(e){
			Pix.show(Pix.imgs[ind]);
		}
	},
	
	next: function(){
		if(!Pix.div){ Pix.init(); }
		var ind = -1;
		if(Pix.last){
			ind = parseInt(Pix.last.id.replace('pic',''));
		}
		ind++;
		if(ind>(Pix.imgs.length-1)){ return; }
		Pix.show(Pix.imgs[ind]);
	},

	focus: function(ind){		
		var x  = ind*90;
		var x1 = parseInt(Pix.line.style.left.replace('px',''));
		var x2 = 270;
		var delta = -(x - (Math.abs(x1)+x2));
		new Effect.Move(Pix.line, {x: delta, duration:0.3});
	}

}


var LoadDialog = {
	id: null,
	id_default: 'kakadudu',
	get: function(id, label){
		this.id = this.getId(id);
		var html = [];
		html.push('<div class="ajax-loader" id="'+this.id+'">');
		html.push('<div class="shader"></div>');
		html.push('<div class="loader">');
		html.push(LoadDialog.getLoading(label));
		html.push('</div>');
		html.push('</div>');
		return html.join('');
	},
	getLoading: function(label){
		var html = [];
		html.push('<img src="/img/ajax-loader.gif" width="32" height="32" style="vertical-align: middle;" />&nbsp;');
		html.push(this.getLabel(label))
		return html.join('');
	},
	attach: function(obj, prop){

		var elm   = $(obj);
		var id    = null;
		var label = null;

		if(typeof(prop)!='undefined'){
			if(typeof(prop.id)!='undefined'){ id = prop.id; }
			if(typeof(prop.label)!='undefined'){ label = prop.label; }
		}
		var html = this.get(id, label);
		var wh   = elm.getDimensions();
		new Insertion.Bottom(elm, html);

		var loader = elm.down('div.loader');
		var dm   = loader.getDimensions();
		var top  = Math.floor((wh['height']-dm['height'])/2);
		var left = Math.floor((wh['width']-dm['width'])/2);
		loader.style.top     = top + 'px';
		loader.style.left    = left + 'px';
		loader.style.display = 'table';

	},

	show: function(obj, prop){
		var elm = $(obj);
		var ald = elm.down('div.ajax-loader');
		if(ald){
			ald.show();
			return;
		}
		return this.attach(obj, prop);
	},

	hide: function(id){
		if(!this.id){ return; }
		var id = this.getId(id);
		var ald = $(id);
		ald.hide();
	},

	getId: function(id){
		if(typeof(id)=='string' && id){ return id; }
		return this.id_default;
	},

	getLabel: function(label){
		if(typeof(label)=='string' && label){ return label; }
		return 'Моля, изчакайте...';
	}

}

var Alerter = {
	
	alert: function(text){
		alert(text);
	}
	
}


	
function maxTextarea(id){
	var obj = $(id);
	var maxlength = 255;
	var numobj = $(id + '_num');
	try { maxlength = obj.getAttribute('maxlength'); }catch(e){}
	if(maxlength == null || maxlength ==''){maxlength = 255;}

	if (obj){
		var txt = obj.value;
		if(txt.length>maxlength)
		{
			$(id).value = txt.substr(0,maxlength);
			Alerter.alert('Не може да въведете повече от '+maxlength+' символа');
		}
		if (numobj)
			numobj.innerHTML = Math.max( (maxlength-txt.length), 0 );
	}
}

