var Bg = new function() {
	this.elements = new Array;
	
	this.getNewArticles = 0;
	this.opInProgrss = 0;
	
	this.fadeIn = function(obj) {
		var duration = Math.round(Math.random() * 1000);
		var toOpacity = obj.css('opacity');
		obj.fadeTo(duration, toOpacity, function() {
			Bg.setState(obj.attr('class'), 1);
		});
	}
	
	this.setState = function(className, state) {
		for(i = 0; i < this.elements.length; i++) {
			if(this.elements[i].name == className) {
				this.elements[i].state = state;
			}
		}
		this.isStatesDone(state);
	}
	
	this.isStatesDone = function(state) {
		var ok = true;
		for(i = 0; i < this.elements.length; i++) {
			if(this.elements[i].state != state) {
				ok = false;
				break;
			}
		}		
		if(ok) {
			if(state) {
				$(document).trigger('articlesShown');
			} else {
				$(document).trigger('articlesHidden');
			}
		}
	}
	
	this.onArticlesShown = function() {
		this.getNewArticles = 0;
		this.opInProgress = 0;
		
		$('.random_news > div').click(
			function(e) {
				Bg.reload();
			}
		);
	}
	
	this.onArticlesHidden = function() {
		if(!this.getNewArticles) {
			return;
		}
		this.refreshArticles();
	}
	
	this.refreshArticles = function() {
		$.get('/home/getRemoteArticles/'+Math.random()+1234567, function(data) {
			$('.random_news').html(data);
			Bg.show();
			var cssId = Math.floor(Math.random() * (17 - 1 + 1)) + 1;
			var cssUrl = 'css/var/color_' + cssId + '.css';
			$('#varCss').attr('href', cssUrl);
		});
	}
	
	this.fadeOut = function(obj) {
		var duration = Math.round(Math.random() * 1000);
		obj.fadeOut(duration, function() {
			Bg.setState(obj.attr('class'), 0);
		});
	}	
	
	this.show = function() {
		this.setObjectsState();
		var objects = $('.random_news > div');
			$.each($(objects), function(item) {
				Bg.fadeIn($(this));
		});
	}
	
	this.hide = function() {
		this.setObjectsState();
		var objects = $('.random_news > div');
			$.each($(objects), function(item) {
				Bg.fadeOut($(this));
		});
	}
	
	this.reload = function() {
		if(this.opInProgress) {
			return;
		}
		this.getNewArticles = 1;
		this.opInProgress = 1;
		this.hide();
	}
	
	this.setObjectsState = function() {
		this.clearElements();
		var objects = $('.random_news > div');
		$.each($(objects), function(item) {
			var el = {
				'name' 	: $(this).attr('class'),
				'state'	: -1
			};
			Bg.elements.push(el);
		});

	}
	
	this.clearElements = function() {
		for(i = 0; i < this.elements.length; i++) {
			this.elements.pop();
		}
		
	}
}

$(document).ready(
	function(e) {
		$(document).bind('articlesShown',  function() {
			Bg.onArticlesShown();
		});
		$(document).bind('articlesHidden', function() {
			Bg.onArticlesHidden();
		});
	}
)


var Gallery = new function() {
	this.images = new Array;
	
	this.change = function(obj, src) {
		this.resetDisabled(obj);
		$('#mainImage').attr('src', '/uploads/'+src);
		var innerSpan  = $(obj).find('span');
		var strong = $('<strong></strong>');
		strong.append(innerSpan);
		$(obj).replaceWith(strong);
	}
	
	this.resetDisabled = function(obj) {
		var disabledEl = $(obj).parent().find('strong');
		var innerSpan  = $(disabledEl).find('span');
		
		var imageSrc = this.getSrcFromSpan($(innerSpan).css('background-image'))
		var newLink = $('<a id="'+imageSrc+'" href="javascript:void(0)" onclick="Gallery.change(this, \''+imageSrc+'\')"></a>');
		newLink.append(innerSpan);
		$(disabledEl).replaceWith(newLink);
	}
	
	this.requestNewImage = function(obj) {
		var mainImg = $(obj).parent().find('img');
		var oldSrc  = $(mainImg).attr('src').split('/').pop();
		
		var newSrc = this.getNextImage(oldSrc);
		
		var changeWith = $('.mini_img > a[id="'+newSrc+'"]');
		
		this.change(changeWith, newSrc);

		var cssId = Math.floor(Math.random() * (17 - 1 + 1)) + 1;
		var cssUrl = 'css/var/color_' + cssId + '.css';
		$('#varCss').attr('href', cssUrl);
		
	}
	
	this.getNextImage = function(oldSrc) {
		var pos = this.indexOf(oldSrc);
		var count = this.images.length;
		
		if(pos == (count - 1)) {
			pos = 0;
		} else {
			pos++;
		}
		return this.images[pos];
	}
	
	this.indexOf = function(item) {
		for(i = 0; i < this.images.length; i++) {
			if(this.images[i] == item) return i;
		};
		return -1;
	}
	
	this.getSrcFromSpan = function(src) {
		var imageSrc = src.replace('url("', '').replace('")', '');
		imageSegments = imageSrc.split('/');
		imageSrc = imageSegments.pop().replace('_28.', '_362.').replace(')', '');	
		
		return imageSrc;
	}
	
	this.collectImages = function() {
		var images  = $('.mini_img').find('span');
		
		for(i = 0; i < images.length; i++) {
			var src = this.getSrcFromSpan($(images[i]).css('background-image'));
			this.images.push(src);
		}
	}
}

var ProjectsScroller = new function() {
	this.nextInProgress = 0;
	this.prevInProgress = 0;
	this.nextTimeout = 0;
	this.prevTimeout = 0;

	this.init = function() {
		// use the custom easing
		var scrollables = $("div.elastik").scrollable({easing: 0, circular: true});
		scrollables.each(function() {
			var $itemsToClone = $(this).scrollable().getItems().slice(1);
			var $wrap = $(this).scrollable().getItemWrap();
			var clonedClass = $(this).scrollable().getConf().clonedClass;
			$itemsToClone.each(function() {
				$(this).clone(true).appendTo($wrap)
					.addClass(clonedClass + ' hacked-' + clonedClass);
			})
			
		});
		
		$('#nextProjects').mouseover(
			function() {
				ProjectsScroller.startShowNext();
				//$("div.elastik").scrollable().next();
			}
		);
		$('#nextProjects').mouseout(
			function() {
				clearTimeout(ProjectsScroller.nextTimeout);
				ProjectsScroller.nextInProgress = 0;
			}
		)
		
		$('#prevProjects').mouseover(
			function() {
				ProjectsScroller.startShowPrev();
				//$("div.elastik").scrollable().prev();
			}
		);
		
		$('#prevProjects').mouseout(
			function() {
				clearTimeout(ProjectsScroller.prevTimeout);
				ProjectsScroller.prevInProgress = 0;
			}
		)
	}
	
	this.startShowNext = function() {
		if(this.nextInProgress) {
			return;
		}
		$("div.elastik").scrollable().next(250);
		this.nextInProgress = 1;
		this.nextTimeout = setTimeout(ProjectsScroller.scrollNext, 250);
	}
	
	this.startShowPrev = function() {
		if(this.prevInProgress) {
			return;
		}
		$("div.elastik").scrollable().prev(250);
		this.prevInProgress = 1;
		this.prevTimeout = setTimeout(ProjectsScroller.scrollPrev, 250);
	}
	
	this.scrollNext = function() {
		$("div.elastik").scrollable().next(250);
		if(ProjectsScroller.nextInProgress) {
			ProjectsScroller.nextTimeout = setTimeout(ProjectsScroller.scrollNext, 250);
		}
	}
	
	this.scrollPrev = function() {
		$("div.elastik").scrollable().prev(250);
		if(ProjectsScroller.prevInProgress) {
			ProjectsScroller.prevTimeout = setTimeout(ProjectsScroller.scrollPrev, 250);
		}
	}	

}
