var Content = 
{
		bindEvent: function (jqobj) {
		 
		$(".aff_img_int",jqobj).hover(
			function() {
				$(this)
					.find('.aff_img_int_infoszones').stop().animate({bottom:"-5px"}, 200, function() {$(this).animate({bottom:"-10px"}, 100)}).end()
					.find('.aff_img_int_link').stop().animate({opacity: 1}, 100).end();
			},
			function(e){
				$(this).data('arrayInfos',$(this).absolutePosAndSize());
				 if (e.pageX <= $(this).data('arrayInfos').left 
				|| e.pageX >= ($(this).data('arrayInfos').left + $(this).data('arrayInfos').width -1 ) 
				|| e.pageY <= $(this).data('arrayInfos').top 
				|| e.pageY >= ($(this).data('arrayInfos').top + $(this).data('arrayInfos').height -1 ))
			{
			  $(this)
				 .find('.aff_img_int_infoszones').stop().animate({bottom:"-5px"}, 100, function() {$(this).animate({bottom:"-30px"}, 200)}).end()
				 .find('.aff_img_int_link').css({'border-bottom-color' : '#999999'}).animate({opacity: 0.7}, 100).end()
				 .find('.aff_img_int_link_txt').fadeOut("slow").end();
			   }
			   
			   }

		);
		
		$(jqobj).each(function() {
					
				$(".aff_img_int_link",this).click(function () {
				$(this).css({'border-bottom-color' : '#000000'});
				$(this).parent().find('.aff_img_int_link_txt').fadeIn("slow");
				

					
				return false;
			});
				
			// copy in clipboard
			$('.link_copy', this).bind('mouseenter',function() {
				clipBoardCopy.glueAndSetText($(this),$('input',$(this).parent()).val(), $('#notifier'));
			});					
				
		});
	}
}

jQuery(document).ready(function() {
								
		//Autoselect value
			
		//jqobjGalEditorName.select().focus();
		function tb_getPageSize(){
		  var de = document.documentElement;
		  var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
		  var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
		  arrayPageSize = [w,h];
		  return arrayPageSize;
	  }
		
		// publicité à l'entrée
		if ($('#SplashPub').length > 0) {
		 var seconds=18;
		 var arraySize = tb_getPageSize();
		 $('#CompteARebours').text(seconds);
		 $('#SplashPub')
		  .css({width: arraySize[0], height: arraySize[1]})
		  .find('.closeSplash').click( function() { $('#SplashPub').hide(); return false; }).end()
		  .show();
		
		 var splashInterval = setInterval(function() {
		  if (seconds <= 0) {
		   $('#SplashPub').hide().remove();
		   clearInterval(splashInterval);
		  } else {
		   $('#CompteARebours').text(seconds--);
		  }
		 },1000);
		};
		
		//General copy home
		$('.link_copy_gal', this).bind('mouseenter',function() {
		   clipBoardCopy.glueAndSetText($(this),$('input',$(this).parent()).val(), $('#notifier'));
		});


		// Slide Accueil
		$('#sliderShow').slideShow();
		
		// Preparation de l'editeur
		var jqobjGalEditor = $('#gallery #editor');
		var jqobjGalEditorName = jqobjGalEditor.find('input[name=galleryName]').keyup(function(event){ if (event.keyCode == 13) { $(this).parent().find('.valid').trigger('click'); }});
		var jqobjGalEditorValid = jqobjGalEditor.find('.valid');
		
		$("#gallery").hover(function() {
			$('html').unbind('click');
		},function() {
			$('html').bind('click', function() { 
				jqobjGalEditor
					.parent('.element').removeClass('inEdit')
						.find('.title').removeClass('hidden').end()
					.end()
				.addClass('hidden'); 
			});
		})
		.find('.element').each(function() {					
			$(this).hover(
				function() { if(!$(this).hasClass('inEdit')) { $('.editLink',this).show(); } }, 
				function() { $('.editLink',this).hide(); }
			);
		
			$('.editLink', this).click(function () {
				var jqobjParent = $(this).parent();
				var jqobjTitle = jqobjParent.find('.title');
				$(this).hide();
				jqobjParent.addClass('inEdit');
				jqobjTitle.addClass('hidden');
				
				if (jqobjGalEditor.parent('.element').attr('rel') != jqobjParent.attr('rel')) {
					jqobjGalEditor
						.parent('.element').removeClass('inEdit')
							.find('.title').removeClass('hidden').end()
						.end();
				}

				jqobjGalEditorName.val(jqobjTitle.find('.name').text());
				jqobjGalEditorValid.unbind('click').click( function() { 
						$.post('http://' + location.hostname + '/php/edit_title.php',{galId: jqobjParent.attr('rel'), galTitle: $('input[name=galleryName]',jqobjGalEditor).val()}, function(response) {
							jqobjGalEditor.addClass('hidden');
							jqobjTitle.find('.name').text(response).end().removeClass('hidden');
							jqobjParent.removeClass('inEdit');
							
							
						});
					});
				jqobjGalEditor.removeClass('hidden').appendTo(jqobjParent);
				jqobjGalEditorName.select().focus();


				return false;
			});
		});
		
		// Mise en place de la fonction bindevent
		Content.bindEvent($('.content'));
		
		$('.supr').click(function () {
		  $.get($(this).attr('href'));
		  $(this).parent().parent().fadeOut("slow"); 
		  return false;
		});
		
		// Tout suprimer en visuel !
		$('.supr_all_view').click(function () {
		  $.get($(this).attr('href'));
		  $('#conteneur_view').fadeOut("slow", function() { $(this).empty()}); 
		  return false;
		});

		// Tout suprimer !
		$('#supr_all').click(function () {
		  $('#code_bloc').fadeOut("slow");
		  $.get($(this).attr('href'));
		  $('#uploadedFiles').fadeOut("slow", function() { $(this).empty()}); 
		  return false;
		});
		
		// Un toogle pour l'affichage / desaffichage de register
		$(".btnreg").click(function () {
			// check visibility
			if ($("#register-to").is(":hidden")) {
				// it's hidden - show it
				$("#register-to").fadeIn("slow"); 
			} else {
				// it's not hidden - slide it down
				$("#register-to").fadeOut("slow"); 
			}
		return false;
		});
		

		// L'enregistrement des droits par mail
		$(".btn_register").click(function() {    
			s = $("form.reg").serialize(); 
			$.ajax({ 
				type: "POST", 
				data: s, 
				url: $(this).attr("href"), 
				success: function(retour){ 
					$("#register-to").empty().append(retour); 
				} 
			}); 
			return false; 
		});
		
		
		// La gestion de l'envoi du livre d'or
		$("#envoi").click(function() {   
			$("#envoi").show();
			s = $("form.message").serialize(); 
			$.ajax({ 
				type: "POST", 
				data: s, 
				url: $(this).attr("href"), 
				success: function(retour){ 
					$(".laster_msg").fadeOut("slow");
					$("#results").append(retour).fadeIn("slow"); 
					$("#hide").show();
					
				} 
			}); 
			return false; 
		});
		
});


new function($) {
  $.fn.setSelection = function(pos1, pos2) {
    if ($(this).get(0).setSelectionRange) {
      $(this).get(0).setSelectionRange(pos1, pos2);
    } else if ($(this).get(0).createTextRange) {
      var range = $(this).get(0).createTextRange();
      range.collapse(true);
      range.moveEnd('character', pos2);
      range.moveStart('character', pos1);
      range.select();
    }
  }
}(jQuery);