//loading function
var wait_block = function(wsubmitter, text) {
	
	block = ($('#' + wsubmitter).length > 0) ? '#' + wsubmitter : '.' + wsubmitter;
	
	jQuery(block + ' #objwait').remove();
	
	var objwait = '<div id="objwait" style="position:absolute; background:#CCC; ' +
                               'width:100%; height:100%; opacity:0.50; moz-opacity:0.50; top:0; left:0;" ' +
                               'align="center">' +
                               '<img style="margin:10% 0 0 0;" src="'+ skinurl +'/images/ajax-loader.gif">'+
                               '<br/><br/><small>'+text+'</small></div>';
	
    jQuery('input, textarea, select').attr('readonly', 'readonly');
    jQuery(block).append(objwait);
    
    // Permet en cas d'erreur Ajax d'echapper
    jQuery(block).ajaxError(function(request, settings){
    	jQuery(block + ' #objwait').fadeOut();
    	jQuery('input, textarea, select').removeAttr('readonly');
    });
    
    // Permet en cas d'erreur Ajax d'echapper
    jQuery(block).ajaxSuccess(function(request, settings){
    	jQuery(block + ' #objwait').fadeOut();
    	jQuery('input, textarea, select').removeAttr('readonly');
    });
};

var isScrolledIntoView = function(elem) {
    var docViewTop = $(window).scrollTop(),
    docViewBottom = docViewTop + $(window).height(),
    elemTop = $(elem).offset().top,
    elemBottom = elemTop + $(elem).height();
    //Is more than half of the element visible
    return ((elemTop + ((elemBottom - elemTop)/2)) >= docViewTop && ((elemTop + ((elemBottom - elemTop)/2)) <= docViewBottom));
};


var gotoWithId = function(uri, id) {
	window.location.href = uri.replace(/isreplaceid/, '~' + id);
};

$(document).ready(function() {
	/**
	 * Animate linked div  by animated
	 */
	$('div.animated-2').hover(
		function(e) {
			$(this).removeClass('not-selected');
			$('div.not-selected')
			.stop(true, true)
			.animate({
				opacity: 0.3
			}, 2000);
		}, 
		function(e) {
			$(this).addClass('not-selected');
			$('div.not-selected')
			.stop(true, true)
			.animate({
				opacity: 1
			}, 2000);
		}
	);

	$('#blocEspacePrive #email').live('focus', function() { 
		$('#email').val('');
	});
	
	$('#blocEspacePrive #passwd').live('focus', function() { 
		node = $(this)[0];
		try {
           		node.type='password';
      		} catch(e) {
           		if (node.outerHTML) {
               			node.outerHTML=node.outerHTML.replace(/value="Mot de passe"/,'value="" type="password"');
               			document.getElementById('edit-pass').focus();
           		}
     		 }
	         node.value='';
	});

	/**
	 * back to home
	 *
	 */
	 
	 $("#page .wrapper .header[href*=]").css('cursor', 'pointer').click(function () {
		window.location.href = $(this).attr('href');
	 });
	
	/**
	 * colorpicker
	 */
	$('input.color').each(function(e) {
		if ($(this).attr('id')) {
			$(this).ColorPicker({
				onSubmit: function(hsb, hex, rgb, el) {
					$(el).val('#' + hex.toUpperCase());
					$(el).ColorPickerHide();
				},
				onBeforeShow: function () {
					$(this).ColorPickerSetColor(this.value);
				}
			})
			.bind('keyup', function(){
				$(this).ColorPickerSetColor(this.value);
			});
		}
	});
	
	/**
	 * datepicker
	 */
	$('input.datetimepicker').each(function(e) {
		if ($(this).attr('id')) {
			$(this).datetimepicker({ dateFormat: 'yy-mm-dd'});
		}
	});
	
	/**
	 * datepicker
	 */
	$('input.datepicker').each(function(e) {
		if ($(this).attr('id')) {
			$(this).datepicker({ dateFormat: 'yy-mm-dd'});
		}
	});
	
	/**
	 * Submission bar
	 */
	if ($('.action-bar').length > 0) {
		
		$(window).scroll(function() {
			
				if (!isScrolledIntoView($('.action-bar')[0])) {
					if ($('.top-bar').length == 0) {
						_clone = $('.action-bar').clone();
						_clone.css({
							position: 'fixed',
							bottom: 0
						}).addClass('top-bar');
						$('body').append(_clone);
					}
					else {
						$('.top-bar').show();
					}
				}
				else {
					$('.top-bar').hide();
				}
		});
	}
	
	/**
	 * FlashMessenger
	 */
	if ($('div.flashMessenger').length > 0) {
		setTimeout(function() {
			$('div.flashMessenger').slideUp(
					function() {
						$(this).remove();
					}
			);
		}, 5000);
	} 
	
	/**
	 * Linked-Link
	 */
	if ($('a.linked-link').length > 0) {
		$('a.linked-link').live('click', function() {
			if ($(this).attr('target')) {
				$target = $(this).attr('target');
				
				$input = $('#' + $target + '-input').val();
				$select = $('#' + $target);
				//search value in target
				$select.find('option').each(function() {
					if ($.trim($(this).html()) == $.trim($input)) {
						$(this).attr('selected',true);
						$select.change();
					}
				});
			}
			
			return false;
		});
	} 
	
	/**
	 * delete confirm
	 */
	if ($('a.delete').length > 0) {
		$('a.delete').live('click', function() {
			_link = $(this);
			$.prompt($(_link).attr('title'),{ 
				submit: function(v,m,f) {
					if (v) {
						wait_block('page', '');
						setTimeout(function(e) {
							window.location.href = $(_link).attr('href');
						}, 2000);
					}
				}, 
				buttons: { 
					Ok: true, 
					Cancel: false
				} 
			});
			
			return false;
		});
	} 
	
	/**
	 * document animation
	 */
	if ($('div.document').length > 0) {
	
		$('div.document').live('mouseenter', function() {
			$(this).find('.document-image').fadeTo('slow', 1);
		});
		
		$('div.document').live('mouseleave', function() {
			$(this).find('.document-image').fadeTo('slow', 0.3);
		});
	}
	
	/**
	 * document animation
	 */
	if ($('div#bottom-block-helper').children().length == 0) {
		$('div#bottom-block-helper').hide();
	}
	
	
});
