$(window).ready(function () {
    //hoogtes gelijk trekken
    $('.equal').equalHeights();

    //twitter integratie
    //tweets
    $('#twitter').jTweetsAnywhere({
        username: 'IlpraBenelux',
        count: 2,
        tweetFeedDecorator: function () {
            return '<ul class="twitter"></ul>';
        },
        tweetBodyDecorator: function (tweet, options) {
            // the default tweet body contains the tweet text and the tweet's creation date
            var html = '';

            if (options.tweetAttributesDecorator) {
                html += "<a href='#'>" + options.tweetAttributesDecorator(tweet, options) + "</a>";
            }

            if (options.tweetTextDecorator) {
                html += options.tweetTextDecorator(tweet, options);
            }

            return '<p class="jta-tweet-body ' + (options._tweetFeedConfig.showProfileImages ? 'jta-tweet-body-list-profile-image-present' : '') + '">' + html + '</p>';
        },
        tweetTimestampDecorator: function (tweet, options) {
            // the default tweet timestamp decorator does a little bit of Twitter like formatting.

            // if tweet is a native retweet, use the retweet's timestamp
            var tw = tweet.retweeted_status || tweet;

            // reformat timestamp from Twitter, so IE is happy
            var createdAt = formatDate(tw.created_at);

            // format the timestamp by the tweetTimestampFormatter
            var tweetTimestamp = options.tweetTimestampFormatter(createdAt);
            var tweetTimestampTooltip = options.tweetTimestampTooltipFormatter(createdAt);

            var screenName = tw.user ? tw.user.screen_name : false || tw.from_user;
            var html =
			'<span class="jta-tweet-timestamp">ongeveer ' +
			tweetTimestamp +
			'</span> ';

            return html;
        },
        tweetTimestampDecorator: function (tweet, options) {
            var now = new Date();

            var tw = tweet.retweeted_status || tweet;

            // reformat timestamp from Twitter, so IE is happy
            var timeStamp = formatDate(tw.created_at);

            var diff = parseInt((now.getTime() - Date.parse(timeStamp)) / 1000);

            var tweetTimestamp = '';
            if (diff < 60) {
                tweetTimestamp += diff + ' seconden' + (diff == 1 ? '' : '') + ' geleden';
            }
            else if (diff < 3600) {
                var t = parseInt((diff + 30) / 60);
                tweetTimestamp += t + ' minuten' + (t == 1 ? '' : '') + ' geleden';
            }
            else if (diff < 86400) {
                var t = parseInt((diff + 1800) / 3600);
                tweetTimestamp += t + ' uur' + (t == 1 ? '' : '') + ' geleden';
            }
            else {
                var t = parseInt((diff + 43200) / 86400);
                tweetTimestamp += t + ' dag' + (t == 1 ? '' : 'en') + ' geleden';
            }

            return '<span class="jta-tweet-timestamp">' + tweetTimestamp + '</span>';
        },
        tweetSourceDecorator: function (tweet, options) {
            //leeg maken en niks tonen
            html = '';
            return html;
        }
    });

    //uri
    var uri = $.trim(document.location.pathname).split('/').clean('');

    //active state toevoegen aan producten
    if (uri[0] == 'producten') {$('#nav-main-producten').addClass('active');}

    //class toevoegen aan product detail pagina
    $('.product-gegevens ul li:last, .product-opties ul li:last').addClass('last');

    //selectiebox active state toevoegen
    $('.selectiebox ul li').each(function () {
        var class_ = $(this).find('a').attr('class');
	if(class_ != undefined) {
        	var segement = class_.split('=').pop();
        	var url_title = class_.split('=').shift();
        	if (segement == url_title) {$(this).find('a').addClass('active');}
        	$(this).find('a').removeClass(class_);
	}

    });

    //selectiebox goed style, lever 2 niet bold maken
    $('.selectiebox li.level-1').each(function () {
        if ($(this).children('ul').length > 0) {
            $(this).find('ul li a').css('font-weight', 'normal');
        }
    });


    //extra vak toevegen bij producten als het niet vol is
    var total_items = $('.producten-overzicht .product').length;
    var clone = $('.producten-overzicht .product:first').clone();
    clone.find('.pad_30').html('<h2>&nbsp;</h2><a href="#" class="product-image"></a> <a style=" border:1px solid white;text-decoration: none;display: inline-block;padding:5px 10px" href="#">&nbsp;</a>');
    var result = (total_items % 3);
    if (result != 0) {
        switch (result) {
            case 1: $('.producten-overzicht').append(clone); $('.producten-overzicht .product:last').width(624);
            case 2: $('.producten-overzicht').append(clone);
        }

    }
    //equel height de vakken
    $('.producten-overzicht .product-image').equalHeights();
    //eerste product altijd een margin van 1 to left
    $('.producten-overzicht .product').each(function (i) {
        if ((i % 3) == 0) {
            $(this).css('margin-left', '1px');
        }
    });
});

Array.prototype.clean = function(deleteValue) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == deleteValue) {         
      this.splice(i, 1);
      i--;
    }
  }
  return this;
};


