// Show/hide offer descriptionshandlers
function showOfferDescription(e) {
  $(this).addClass("hover");
  return false;
}

function hideOfferDescription(e) {
  $(this).removeClass("hover");
  return false;
}

/* offers page interaction */
function setupOffers() {
  var nl = null;
  if ($("#offers_header").size() && $("#offers_header").hasClass("offers_header_bonus")) {
    nl = $("div.offer_block div.offers_item");
  }
  else {
    nl = $(".offers div.offers_item");
  }
  
  $(nl).each(function(nID, n) {
	  var n =$(n);
    if (n.hasClass("banner")) return;
    
    // Create the offerdescription togglers
    n.bind('mouseover', showOfferDescription);
    n.bind('mouseout', hideOfferDescription);
    
    var recipeLinks = $("a.recipes",n);
    if (recipeLinks.size() > 1) {
      setupRecipeLinks(n);
    }
    
    setupDiscountLink(n);
  });
}


/*  MULTIPLE RECIPE LINKS SETUP
 *
 */
function setupRecipeLinks(offersItem) {
  var recipeLinks = $("a.recipes",offserItem);
  
  // Build container.
  var recipeBox = document.createElement("div");
  recipeBox.className = "offers_recipes_box";
  recipeBox.style.position = "absolute";
  var recipeLinkParent = $(recipeLinks[0]).offsetParent();
  var recipeLinkParentPos = $(recipeLinkParent).position();
  var recipeLinkPosition = $(recipeLinks[0]).position();
  recipeBox.style.left = (recipeLinkParentPos['left'] + recipeLinkPosition['left']) + "px";
  if (!ua.gecko) recipeBox.style.top = (recipeLinkParentPos['top'] + recipeLinkPosition['top'] + 18) + "px";
    
  // Insert links from offers item.
  recipeLinks.each(function(index, recipeLink){
    var newRecipeSingular = document.createElement("p");
    newRecipeSingular.className = "singular";
    
    var newRecipeLink = document.createElement("a");
    newRecipeLink.href = recipeLink.href;
    newRecipeLink.title = recipeLink.title;
    
    var newRecipeText = document.createTextNode(recipeLink.title);
    newRecipeLink.appendChild(newRecipeText);
    newRecipeSingular.appendChild(newRecipeLink);
    recipeBox.appendChild(newRecipeSingular);
    
    if (index > 0) {
      recipeLink.style.display = "none";
    } else {
      recipeLink.title = "Toon links naar recepten met deze aanbieding";
      $(recipeLink).bind('click', toggleRecipeBox);
      var recipeImg = $('img:first', recipeLink);
      recipeImg[0].alt = recipeLink.title; // Fix for IE showing alt texts instead of link title (!).
    }
  });

  // Inject rounded corners.
  var recipeCorner = document.createElement("div");
  recipeCorner.className = "corner corner_tl";
  recipeBox.appendChild(recipeCorner);
  
  recipeCorner = document.createElement("div");
  recipeCorner.className = "corner corner_tr";
  recipeBox.appendChild(recipeCorner);

  recipeCorner = document.createElement("div");
  recipeCorner.className = "corner corner_br";
  recipeBox.appendChild(recipeCorner);

  recipeCorner = document.createElement("div");
  recipeCorner.className = "corner corner_bl";
  recipeBox.appendChild(recipeCorner);

  offersItem.appendChild(recipeBox);
}

function toggleRecipeBox(e) {
  var offersItem = $(this).parent().parent();
  var recipeBox = $("div.offers_recipes_box:first",offersItem);
  
  if (recipeBox.hasClass("offers_recipes_box_show")) {
    recipeBox.removeClass("offers_recipes_box_show");
  } else {
    recipeBox.addClass("offers_recipes_box_show");
  }
  
  if (this.blur) this.blur();
  return false;
}





/*  SPECIAL DISCOUNT LINK SETUP
 *
 */
function setupDiscountLink(offersItem) {
  var discountLink = $("a.special_discount:first", offersItem);
  if (!discountLink.size()) discountLink = $("span.special_discount:first",offersItem);
  if (!discountLink.size()) return;

  discountLink[0].improvedAlt = discountLink.children().first().html();
  //  mouseover event on image
  discountLink.hover(function(){discountShowAltText();},function(){discountHideAltText();});
}

function discountShowAltText() {
  var altDiv = document.createElement('div');
  altDiv.innerHTML = this.improvedAlt;
  altDiv.id='special_discount_alt';
  // append it to the body (otherwise it wont go over the right part of the site)
  document.body.appendChild(altDiv);
  // follow the mouse
  $(this).bind('mousemove', discountAltFollowMouse);
  $(this).addClass('special_discount_over');
}
function discountHideAltText() {
  $(this).unbind('mousemove', discountAltFollowMouse);
  // remove the improved alt block
  if ($('#special_discount_alt').size()) $('#special_discount_alt').remove();
  $(this).removeClass('#special_discount_over');
}

function discountAltFollowMouse(event) {
  $('#special_discount_alt').css({
	 left: (event.clientX-15)+'px',
	 top: (event.clientY-25)+'px'
  });
}

function beursMouseOver(obj) {
	var imagesInLink = $('img', obj);
	if (imagesInLink.size() > 0) {
		var altDiv = document.createElement('div');
		altDiv.innerHTML = imagesInLink[0].alt;
		altDiv.id='beurs_alt';

		// append it to the body (otherwise it wont go over the right part of the site)
		document.body.appendChild(altDiv);
		// follow the mouse
		$(obj).bind('mousemove', beursAltFollowMouse);
	}

  $(obj).addClass('beurs_over');
}
function beursMouseOut(obj) {
  $(obj).unbind('mousemove', beursAltFollowMouse);
  // remove the improved alt block
  if ($('#beurs_alt')) $('#beurs_alt').remove();
  $(obj).removeClass('#beurs_over');
}

function beursAltFollowMouse(event) {
	$('#beurs_alt').css({
		 left: (event.clientX-15)+'px',
		 top: (event.clientY-25)+'px'
	  });
}



/* initialize when DOM is loaded */
if (loader) {
  loader.schedule("setupOffers", setupOffers, Loader.priority.HIGH);
}
