//var l = function(some) { console && console.log(some); }

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
};

var search_string_value = null;

$(function(){
	try {
		$(document).pngFix();
	} catch(e) {}
		
		
		$("body").click(function(){
			closeDivBody();
		});
		
		/*if ($(window).width() < 1275) {
			$('#search-form').css('width', '90%');
			$('.info').css('width', '50%');
		} else {
			$('#search-form').css('width', '98%');
			$('.info').css('width', '62%');
		}*/

		//Fix for IE6
		/*if ($.browser.msie && $.browser.version == '6.0') {
			$('#side-left').css('margin-left', '-100%');
			$('#side-left').css('width', '215px');
		}*/
});

$(document).ready(function() {
	hideTransDiv();

	var nowraps = $(".does-not-wrap-last-line"),
		nowraps_spaces = nowraps.children(".space");

	$(window).resize(function() {
		hideTransDiv();

		nowraps_spaces.removeClass("last-line");

		var pictures = $(".pictures-view"),
				
				number_of_pictures_in_row = function(pictures_width, picture_width) {
					return Math.floor(pictures_width / (picture_width));
				};

		pictures.each(function() {
			var pictures_width = $(this).parent().outerWidth(),
					picture = $(this).find(".ab-picture"),
					picture_width = $(picture[0]).outerWidth(),
					pictures_in_row = number_of_pictures_in_row(pictures_width, picture_width),

					result_margin = Math.floor((pictures_in_row * picture_width - pictures_width) / (1 - pictures_in_row)) - 1,
					
					remainder = Math.abs((pictures_in_row * picture_width - pictures_width) % (1 - pictures_in_row));
			
			var counter = 0,
				max_height = $(picture[0]).height(),
				picture_row = new Array();

			picture.each(function(picture_index, e) {
				var margin = result_margin,
						float = "left",
						this_picture = $(this);

				// First in row
				if (!(picture_index % pictures_in_row)) {
					margin = 0;
					counter = 0;

					max_height = this_picture.height();
					picture_row = [];
				}
				else {
					if (!remainder) {
						if (counter < remainder) {
							margin = result_margin + 1;
						}

						counter = counter + 1;
					}

					if (max_height < this_picture.height())
						max_height = this_picture.height();
				}

				picture_row.push(this_picture);

				// Last in row
				if (!((picture_index + 1) % pictures_in_row)) {
					float = "right";
					
					//l(picture_row);

					while (picture_row.length > 0) {
						picture_row.pop().css("height", max_height);
					}
				}

				this_picture.css({ 
					marginLeft: margin + "px",
					float: float
				});
			});

		});

		/*
		if ($(this).width() < 1033) {
			$('div.transp').hide();
		} else {
			$('div.transp').show();
		}*/

		nowraps.each(function() {
			var spaces = $(this).children(".space"),
				first_space_in_last_line;

			spaces.each(function() {

				if ($(this).position().top >= 18) {
					first_space_in_last_line = $(this);

					return false;
				}
			});

			if (typeof first_space_in_last_line != "undefined") {
				first_space_in_last_line.addClass("last-line").nextAll(".space").addClass("last-line");
			}
		});
	});

	$(window).resize();
/*
	// Tabs
	var tab = $(".ab-tab");

	tab.click(function() {
		$(this)
		.siblings().removeClass("current").end()
		.addClass("current")
			.next().addClass("current");

		return false;
	});
*/
});

function hideTransDiv() {
	if ($(window).width() < 1033) {
		$('div.transp').hide();
	} else {
		$('div.transp').show();
	}
}

function slideBlock(id) {
	$('#' + id).slideToggle('fast');
}




