var promo = new function() {
	this.COLUMNS = 5;
	this.COLWIDTH = 200;
	this.MARGIN = 10;
	this.loaded = 0; 
	this.colheight = new Array(this.COLUMNS);
	this.currentcol = 0;
	this.stoploading = 0;
	this.busy = 0;
	this.updateContentHeight = function() {
		var height = 0;
		for(var i=0;i<promo.COLUMNS;i++) {
			if(promo.colheight[i] > height) height = promo.colheight[i];
		}
		height+=10;
		if(height < $(window).height() - 235){height = $(window).height() - 235;}
		$('.main_content').height(height);
	}
	this.isFits = function(size, column) {
		if(size < 2) return true;
		if(column + size > promo.COLUMNS) return false;
		var height = promo.colheight[column];
		
		for(var i=1;i<size;i++) {
			if(promo.colheight[column + i] != height) return false;
		}
		return true;
	}
	this.findPlace = function(size) {
		var columnOrder = new Array();
		for(var i=0;i<=(promo.COLUMNS - size);i++){
			if(promo.isFits(size, i)) columnOrder.push([i, promo.colheight[i]]);
		}
		//console.log(columnOrder);
		
		columnOrder.sort(function(a, b){
			if(a[1]==b[1]) return a[0]-b[0]; else return a[1]-b[1];
		});
		promo.currentcol = columnOrder[0][0];
	}
	this.initOne = function() {
		var h = $(this).height();
		//console.log($(this).css('height'));
		var size = 1;
		if($(this).hasClass('c2')) size = 2;
		if($(this).hasClass('c3')) size = 3;
		if($(this).hasClass('c4')) size = 4;
		if($(this).hasClass('c5')) size = 5;
		promo.findPlace(size);
		
		$(this).css({left: (promo.currentcol * promo.COLWIDTH) + 'px', top: promo.colheight[promo.currentcol] + 'px'});
		var newheight = promo.colheight[promo.currentcol] + h + promo.MARGIN;
		if($(this).hasClass('optional') && newheight > $('.main_content').height()){
			$(this).hide();
		} else {
			$(this).not('#loadhere .promo_holder').show();
			for(var i=0;i<size;i++) promo.colheight[promo.currentcol + i] = newheight;
			promo.updateContentHeight();
			promo.loaded++;
		}
		if($(this).is('.promo_holder:last')){
			promo.busy = 0;
		}
	}
	this.init = function() {
		this.busy = 1;
		for(var i=0;i<promo.COLUMNS;i++){
			promo.colheight[i] = 0;
		}
		$('.promo_holder').each(promo.initOne);
	}
	this.initMore = function() {
		$('.promo_holder:gt(' + (promo.loaded - 1) + ')').each(promo.initOne);
	}
	this.getMore = function() {
		this.busy = 1;
		$('.promo_holder:last').after('<div id="loadhere"></div>');
		$('#loadhere').load('./?ajax=1&more=' + promo.loaded, function(response, status, xhr){
			if($('#loadhere .promo_holder').size() < 10) promo.stoploading = 1;
			$('#loadhere .promo_holder').hide();
			promo.initMore();
			var i=0;
			$('#loadhere .promo_holder').each(function(){
				$(this).delay(i*200).fadeIn(500);
				i++;
			});
			$('#loadhere').attr('id', '');
			
		});
	}
}

function setMinHeight() {
	if($('.main_content').height() < $(window).height() - 235){$('.main_content').height($(window).height() - 235)}
}
function updateSearchButton(){
	if($('.searchfield').val().length < 3){
		$('.submit').css({opacity: '0.2'});
	} else {
		$('.submit').css({opacity: '1'});
	}
}
$(function(){
	promo.init();
	//$('.promo .comments').each(function(){
	//	setTimeout(promo.init, 1000);
	//});
	
	setMinHeight();
	
	$('.tip').focus(function(){
		$(this).removeClass('tip');
	}).blur(function(){
		if(!$(this).val()){
			$(this).addClass('tip');
		}
	});
	if($('.scrollupdate').size() > 0){
		$(window).scroll(function(){
			if($(window).scrollTop() >= ($(document).height() - $(window).height() - 100)){
				if(! promo.stoploading && ! promo.busy) promo.getMore();
			}
		});
	}
	
	$('form[name="search"]').submit(function(){
		if($('.searchfield').val().length < 3){
			return false;
		}
	});
	updateSearchButton();
	$('.searchfield').keyup(function(){
		updateSearchButton();
	});
	
});
