var shiftPictureTimer;

$(document).ready(init);

function init()
{
	ensureSize();
	shiftPicture(SHIFT_URL,true);
	defer_ads();
}

$(window).resize(function()
{
	defer_ads();
});

function switchPod()
{
	id = $(this).attr("id").split("_")[1];
	/*APO - Chargement des images de popPod en asynchrone*/
	popPodA=$("#popPodContent").children('a')[id-1];
	if(popPodA.title){
		popPodA.innerHTML='';
		dossierImg=document.createElement('img');
		dossierImg.width='232';
		dossierImg.src=popPodA.title;
		popPodA.appendChild(dossierImg); 
		$("#poppod_"+id).children('img').addClass('linkImage');
		popPodA.removeAttribute('title');
	}
	$(".popPodMainPic").css("display", "none");
	$("#poppod_"+id).css("display", "block");
}

function ensureSize()
{
	c1Height = $("#majojColumn1").height();
	c2Height = $("#majorColumn2").height();
	
	if (c1Height<c2Height)
		$("#majojColumn1").height($("#majorColumn2").height());
}

function closepopup(url)
{
  //window.open(url,'_blank','toolbar=0, location=0, directories=0, status=0, scrollbars=1, resizable=0, copyhistory=0, menuBar=0, width=768');
  if(opener)
  {
    opener.location.href=url;
    self.close();
    return false;
  }else{
    document.location=url;
    return true;
  }
}

function goFakeLink()
{
	//document.location = $(this).children("a.fakeLink").attr("href");
	closepopup($(this).children("a.fakeLink").attr("href"));
}

function shiftPicture(url, isAutomated)
{
  shiftDelay = 5000;

  if (isAutomated)
    shiftPictureTimer = setTimeout("window.location.href = '"+url+"'", shiftDelay);
	else	
		clearTimeout(shiftPictureTimer);
}

function defer_ads()
{
	$('div.defer_ads').each(function()
		{
		  var id = $(this).attr('id').replace(/_DEFER_ADS/, '');
		  var pos = $("#" + id).offset();

		  if (pos)
		  {
		    $(this).css(
		    {
		      "left": pos.left +"px",
		      "top":  pos.top  +"px"
		    });

		    $("#" + id).height($(this).height() + 5);
		    $("#" + id).width($(this).width());
		  }
		});
}

// APO - Redirige vers la page jail_publicite.php avec l'url courante en paramètre
function ensure_ads()
{
	count_ensure_ads++;
	if(count_defer_ads < count_ensure_ads && window.location.pathname != "/jail_publicite.php")
	{
		window.location = "/jail_publicite.php?origine="+encodeURIComponent(window.location.href);
	}
}

// Redimensionnement des widgets Kelkoo
function truncate_kelkoo_description(emplacement,longueur)
{
	$(emplacement+' .name').each(function(){
		if($(this).children('a').text().length>longueur)
		{
			$(this).children('a').text($(this).children('a').text().toUpperCase().substring(0,1)+$(this).children('a').text().toLowerCase().substring(1,longueur)+'...');
		}else{
			$(this).children('a').text($(this).children('a').text().toLowerCase());
		}
	});
}

//////////////////////////////////////////////////
// Fonctions chargées avant que la page soit prête
//////////////////////////////////////////////////

// Initialisation des variables
var count_defer_ads=0;
var count_ensure_ads=0;

$(".fakeClickZone").click(goFakeLink);

// LA PHOTO DU JOUR
$(".picItem").mouseover(switchPod);
$("#dsArrows #pause").click(shiftPicture);

