var $jq = jQuery.noConflict();

var isIE6 = $jq.browser.msie && $jq.browser.version == "6.0";

/*
	This script uses a live array to affect elements.
	Instead of complex and time-consuming calculations,
	it simply removes the shown elements and appends them
	to the back of the array, effectively creating an
	endless loop. Note: fadeIn occurs during callback to
	enable sets of only three elements to fade smoothly.
*/

$jq(document).ready(function() {
  var fadeInSpeed = 500;
  var fadeOutSpeed = 200;
  var newSlideSpeed = 9000;

  $jq("#homeNews h2").click(function() {
    location.href = $jq(this).attr("ref");
  });

  //$jq("marquee").marquee("marquee");

  /* vullen carousel met html o.b.v. array met gekozen images. */
  if ($jq("body").attr("class") != 'homepage') {
    var total = carousel_itemList.length;
    for (counter = 0; counter < (total - 1); counter++) {
      $jq("#carousel").append(getItemHTML(carousel_itemList[counter]))
    }
  }

  /* klikfunctie voor uitvoeren van a-href in teasermenu, door table in a werkt in ie het normale gedrag niet meer. again. */
  $jq(".teaser table").click(function() {
    location.href = $jq(this).parent().attr("href");
  });

  /* entire set of elements */
  var set = $jq("#carousel div").get();

  /* execute only when set is greater than two */
  if (set.length > 2) {

    /* hide all but first two elements */
    $jq("#carousel div:gt(1)").hide();

    /* position second element below first element */
    $jq("#carousel div:eq(1)").css("top", $jq("#carousel div:eq(1)").prev("div").height() + 20);

    /* create interval for fadeins/fadeouts */
    setInterval(function() {

      /* append copy of first two elements to array */
      set.push(set[0], set[1]);

      /* fadeout first two elements */
      $jq(set[0]).fadeOut(fadeOutSpeed);
      $jq(set[1]).fadeOut(fadeOutSpeed, function() {

        /* remove first two elements from array */
        set.splice(0, 2);

        /* reset positioning for first two elements */
        $jq(set[0]).css("top", "0").fadeIn(fadeInSpeed);
        $jq(set[1]).css("top", $jq(set[0]).height() + 20).fadeIn(fadeInSpeed);
      });

    }, newSlideSpeed);
  }
  else {
    $jq(set[1]).css("top", $jq(set[0]).height() + 20).fadeIn(fadeInSpeed);
  }

});

function getItemHTML(item)
{
    return ('<div class="item"><img src="' + item.url + '" alt="' + item.title + '"/><span>' + item.title + '</span></div>');
};



/*(function() {
	$("#carousel").jcarousel({
		auto: 4,
		animation: 4000,
		scroll: 2,
		vertical: true,
		wrap: "circular"
	});
});


function carousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, carousel_itemList.length);
    carousel.add(i, mycarousel_getItemHTML(carousel_itemList[idx - 1]));
};

function carousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
};*/

/**
 * Item html creation helper.
 */
/*function mycarousel_getItemHTML(item)
{
    return '<img src="' + item.url + '" alt="' + item.title + '" />';
};*/

/*jQuery(document).ready(function() {

    if (carousel_itemList.length != 0 ) {
	    if(carousel_itemList.length < 3) {
		jQuery('#carousel').jcarousel({
			    auto: 0, // minder dan drie ingeladen foto's via CMS? Dan niet scrollen.
			    vertical: true,
			    scroll: 2,
			    animation: 2000,
		    wrap: 'circular',
		    itemVisibleInCallback: {onBeforeAnimation: carousel_itemVisibleInCallback},
		    itemVisibleOutCallback: {onAfterAnimation: carousel_itemVisibleOutCallback}
		});
	    }
	    else {
		jQuery('#carousel').jcarousel({
			    auto: 7, // wel drie of meer, dan scrollen maar.
			    vertical: true,
			    scroll: 2,
			    animation: 2000,
		    wrap: 'circular',
		    itemVisibleInCallback: {onBeforeAnimation: carousel_itemVisibleInCallback},
		    itemVisibleOutCallback: {onAfterAnimation: carousel_itemVisibleOutCallback}
		});    
	    }
    }
});*/
