$(document).ready(function() {
	$(document).pngFix();
	
	$('input.textbox[rel=hint]').each(function(index) {
		$(this).attr('placeholder', $(this).val());
	});
	
	$('input.textbox[rel=hint]').focus(function(e) {
		e.preventDefault();
		if($(this).val() == $(this).attr('placeholder')) {
			$(this).val('');
		}
	});
	
	$('input.textbox[rel=hint]').blur(function(e) {
		e.preventDefault();
		if($(this).val() == '') {
			$(this).val($(this).attr('placeholder'));
		}
	});
	
	
	$('table.actions-report th').click(function(e) {
		toggleActionsReport($(this).parents('table:first').find('tbody'));
	});
	

	$('.ads-list li, #activities li, .article-preview').click(function(e) {
		document.location.href = $(this).find('a:first').attr('href');
	});

	setFormFocus();

	$('a[rel=external]').click(function(e) {
		e.preventDefault();
		window.open($(this).attr('href'));
	});
	
	
	$('a#unlogged').click(function(e) {
		e.preventDefault();
		$.get('/members/modal_login', function(r) {
			
			if(typeof(doRounded) != 'function') {
				// Not IE
				$(document.body).append(r);
			}
			else {
				// Is someone can explain to me the reason for what IE can not insert ALL the contents of the "r" var when unsign $.append() ???
				var div = document.createElement('div');
				div.setAttribute('id', 'ie-fixer')
				div.innerHTML = r;
				document.body.appendChild(div);
			}
			Modal.init();
			setFormFocus();
		});
	});
	
	
	
	
	$('#thumbs-list a').click(function(e) { 
		e.preventDefault();
		// Setup
		var img = $(this).attr('href');
		var id = 0;
		var img_list = [];
		$('#thumbs-list a').each(function(__id, __el) { 
			img_list.push($(__el).attr('href'));
			if($(__el).attr('href') == img) {
				id = __id;
			} 
		});
		// Launch
		$.get('/photos/modal_view', function(r) { 
			$('body').append(r);
			var img_tag = '<img src="'+ img +'" alt="image de l\'annonce" />';
			$('#picture-main').append(img_tag);
			
			if(img_list.length > 1) { 
				$('#picture-prev').click(function() {
					var n = id - 1;
					if(n < 0) {  n = img_list.length - 1; }
					$('#picture-main img').attr('src', img_list[n]);
					id = n;
				});
			
				$('#picture-main img, #picture-next').click(function() {
					var n = id + 1;
					if(n >= img_list.length) {  n = 0; }
					$('#picture-main img').attr('src', img_list[n]);
					id = n;
				});
			}
			else {
				$('#picture-prev, #picture-next').hide();
			}

			Modal.init();
		});
	});
	
	
	
	
	$('#reference-access').click(function(e) {
		e.preventDefault();
		if($('#reference').val() != '') {
			var action = $(this).parents('form:first').attr('action');
			document.location.href = action + $('#reference').val();
		}
	});
});



var Modal = {
	init: function() {
		var _this = this;
		$('#overlay, #close-modal-window').click(function() { _this._hide(); });
		/* If very bad browser, execute line below */
		if(typeof(doRounded) == 'function') {
			doRounded();
		}
		/* */
		this._show();
	},
	
	_show: function() {
		$('#overlay, div.overlay').hide();
		$('#overlay, div.overlay').fadeIn(600);
	}, 
	
	_hide: function() {
		$('#overlay, div.overlay').fadeOut(300, function() { $('#ie-fixer, #overlay, div.overlay').remove(); });
	}
}





function setFormFocus() {
	$('.form input, .form textarea, .form select').focus(function(e) {
		$(this).closest('li').addClass('focus');
	});

	$('.form input, .form textarea, .form select').blur(function(e) {
		$(this).closest('li').removeClass('focus');
	});
}


function overlayActions() {
	$('#overlay, #close-modal-window').click(function() {
		$('#overlay').remove();
		$('div.overlay').remove();
	});
}

function fix(str) {
	var separator = '.';
	var _num = str.toString().split('').reverse();
	// console.debug(_num);
	var _len = _num.length;
	for(var i = 2; i < _len; i += 3) {
		i++;
		_num.splice(i, 0, separator);
	}
	
	return _num.reverse().join('');
}



function toggleActionsReport(el) {
	if($(el).is(':visible')) {
		$(el).hide();
		$(el).parents('table:first').find('span.arrow-indic').text('►')
	}
	else {
		$(el).show();
		$(el).parents('table:first').find('span.arrow-indic').text('▼')
	}

}
