var Site = {
	onFocus: function() { 
		if (this.getValue() == this.getAttribute('title'))
			this.setValue('');
	},
	onBlur: function() {
		if (this.getValue().length == 0)
			this.setValue(this.getAttribute('title'));
	},
	selectThumb: function (e){
		if (e) e.stop();
		$(this.parentNode.parentNode).select('.selected').invoke('removeClassName', 'selected');
		$(this).addClassName('selected');
		var image = $('mainimage');
		if (image.down('img')) {
//			image.innerHTML = '<img src="' + this.getAttribute('href') + '" alt="" />';
			image.innerHTML = '';
			var img = new Image();
			img.src =  this.getAttribute('href');
			image.insert(img);
		} else
			image.innerHTML = '<span class="pic" style="background-image: url(' + this.getAttribute('href') + ');"></span>';
		return false;
	}
};
//document.observe('dom:loaded', function(){
Event.observe(window, 'load', function(){
	EventSelectors.start({
		'input[type=text]': function(input){
			if (input.getAttribute('title')){
				input.observe('focus',	Site.onFocus);
				input.observe('blur',	Site.onBlur);
			}
		},
		'#srcsindex .num a:click': Site.selectThumb,
		'#thumbslist a:click': Site.selectThumb,
		'#thumbs .galarrow:click': function(e){
			var a = 
			($('thumbslist').down('.selected').up('li')[this.hasClassName('nextdis') ? 'next' : 'previous']('li') || 
			 $$('#thumbslist li')[this.hasClassName('nextdis') ? 'first' : 'last']()).down('a');		 
			
			Site.selectThumb.call(a);
		},
		'#offers ul a:click': function(e){
			var number = (parseInt(this.innerHTML) - 1) || 0
			$$('#offers .offerbox').each(function(element, key){
				element[number == key ? 'show' : 'hide']();
			});
			$(this.parentNode.parentNode).select('.selected').invoke('removeClassName', 'selected');
			this.addClassName('selected');
			e.stop();
		},
		'#users .mainmenu a.openar:mouseover': function(){
			this.next('ul').show();
			this.up().addClassName('over').observe('mouseout', function(e){
				var is = false;
				try { 
					var target = e.relatedTarget || e.toElement;
					is = Element.descendantOf(target, this) || (target == this);
				} catch(err) {}
				if (!is) {
					this.down('ul').hide();
					this.removeClassName('over');
					this.stopObserving('mouseout');
				}
			});
		},
		'input[type=submit]': function(element){
			element.observe('mousedown', element.addClassName.bind(element, 'clicked'));
			element.observe('mouseup', element.removeClassName.bind(element, 'clicked'));
		}
	});
	
	var news = $$('#newsbox div');
	if (news.length > 1){
		var visible	= 0;
		var	looper	= function(){
			// for the news
			news[visible].fade();
			if (++visible >= news.length) visible = 0;
			news[visible].appear();
		};
	}
	
	if ($('srcsindex'))
		var looper = Object.isFunction(looper) ? looper.wrap(function(original){
			original();
			Site.selectThumb.call(($('srcsindex').down('.selected').up().next('.num') || $$('#srcsindex .num').first()).down());
		}) : function() {
			Site.selectThumb.call(($('srcsindex').down('.selected').up().next('.num') || $$('#srcsindex .num').first()).down());
		};
		
	if (looper)
		new PeriodicalExecuter(looper, 10);
});var Site = {
	onFocus: function() { 
		if (this.getValue() == this.getAttribute('title'))
			this.setValue('');
	},
	onBlur: function() {
		if (this.getValue().length == 0)
			this.setValue(this.getAttribute('title'));
	},
	selectThumb: function (e){
		if (e) e.stop();
		$(this.parentNode.parentNode).select('.selected').invoke('removeClassName', 'selected');
		$(this).addClassName('selected');
		var image = $('mainimage');
		if (image.down('img')) {
//			image.innerHTML = '<img src="' + this.getAttribute('href') + '" alt="" />';
			image.innerHTML = '';
			var img = new Image();
			img.src =  this.getAttribute('href');
			image.insert(img);
		} else
			image.innerHTML = '<span class="pic" style="background-image: url(' + this.getAttribute('href') + ');"></span>';
		return false;
	}
};
//document.observe('dom:loaded', function(){
Event.observe(window, 'load', function(){
	EventSelectors.start({
		'input[type=text]': function(input){
			if (input.getAttribute('title')){
				input.observe('focus',	Site.onFocus);
				input.observe('blur',	Site.onBlur);
			}
		},
		'#srcsindex .num a:click': Site.selectThumb,
		'#thumbslist a:click': Site.selectThumb,
		'#thumbs .galarrow:click': function(e){
			var a = 
			($('thumbslist').down('.selected').up('li')[this.hasClassName('nextdis') ? 'next' : 'previous']('li') || 
			 $$('#thumbslist li')[this.hasClassName('nextdis') ? 'first' : 'last']()).down('a');		 
			
			Site.selectThumb.call(a);
		},
		'#offers ul a:click': function(e){
			var number = (parseInt(this.innerHTML) - 1) || 0
			$$('#offers .offerbox').each(function(element, key){
				element[number == key ? 'show' : 'hide']();
			});
			$(this.parentNode.parentNode).select('.selected').invoke('removeClassName', 'selected');
			this.addClassName('selected');
			e.stop();
		},
		'#users .mainmenu a.openar:mouseover': function(){
			this.next('ul').show();
			this.up().addClassName('over').observe('mouseout', function(e){
				var is = false;
				try { 
					var target = e.relatedTarget || e.toElement;
					is = Element.descendantOf(target, this) || (target == this);
				} catch(err) {}
				if (!is) {
					this.down('ul').hide();
					this.removeClassName('over');
					this.stopObserving('mouseout');
				}
			});
		},
		'input[type=submit]': function(element){
			element.observe('mousedown', element.addClassName.bind(element, 'clicked'));
			element.observe('mouseup', element.removeClassName.bind(element, 'clicked'));
		}
	});
	
	var news = $$('#newsbox div');
	if (news.length > 1){
		var visible	= 0;
		var	looper	= function(){
			// for the news
			news[visible].fade();
			if (++visible >= news.length) visible = 0;
			news[visible].appear();
		};
	}
	
	if ($('srcsindex'))
		var looper = Object.isFunction(looper) ? looper.wrap(function(original){
			original();
			Site.selectThumb.call(($('srcsindex').down('.selected').up().next('.num') || $$('#srcsindex .num').first()).down());
		}) : function() {
			Site.selectThumb.call(($('srcsindex').down('.selected').up().next('.num') || $$('#srcsindex .num').first()).down());
		};
		
	if (looper)
		new PeriodicalExecuter(looper, 10);
});