﻿$(function() {

	// Show first news item
	showContentItem($('.ContentItemDetailNewsItem:first'));

	// Detail news links	
	$('.ContentItemDetailNewsItem .FormFieldTitle').live('click', function() {
		showContentItem($(this).closest('.ContentItemNewsItem'));
	});
	
	// Calendar
	$('.ContentItemContentGroupAppointment .ContentContainerGroup:not(:first)').css('display', 'none');
	$('.ContentItemContentGroupAppointment .ContentContainerGroupTitle:not(:first)').css('display', 'none');
	var isFirst = true;
	var isAlt = false;
	var index = 0;
	$('.ContentItemContentGroupAppointment .ContentContainerGroupTitle').each(function() {
		var month = $(this).text().split(' ')[0];
		$('#right').append($('<div class="GroupTitleCloned ' + (isFirst ? ' First' : '') + (isAlt ? ' Alt' : '') + '" />').html('<a href="#' + month + '">' + $(this).text() + '</a>').data('index', index));
		index += 1;
		isFirst = false
		isAlt = !isAlt;
	});
	$('.GroupTitleCloned a').click(function() {
		var index = $(this).closest('.GroupTitleCloned').data('index');
		$('.ContentItemContentGroupAppointment .ContentContainerGroup').css('display', 'none');
		$('.ContentItemContentGroupAppointment .ContentContainerGroupTitle').css('display', 'none');
		$('.ContentItemContentGroupAppointment .ContentContainerGroup').eq(index).css('display', 'block');
		$('.ContentItemContentGroupAppointment .ContentContainerGroupTitle').eq(index).css('display', 'block');
		return false;
	});
	$('.AppointmentHeader').live('click', function() {
		var isActive = $(this).closest('.ContentItem').hasClass('active');
		$('.ContentItemDetailAppointment').removeClass('active');
		if (!isActive) $(this).closest('.ContentItem').addClass('active');
	});
    var audio = new audioPlayer();
	
	if (typeof Cms !== 'undefined') {
		window.setInterval(function() {
			if ($('.ContentItemEditAppointment .FormFieldFinishDateTime').length) {
				$('.ContentItemEditAppointment .FormFieldFinishDateTime').each(function() {
					try {
						if (document.activeElement !== $(this).find('input:first').get(0)) {
							var startDateText = $(this).closest('.ContentItem').find('.FormFieldDateTime input:first').val();
							var startDate = Date.parseLocale(startDateText);
							var endDateText = $(this).find('input:first').val();
							var endDate = Date.parseLocale(endDateText);
							if (endDate < startDate) {
								$(this).find('input:last').val($(this).closest('.ContentItem').find('.FormFieldDateTime input:last').val());
								$(this).find('input:first').val(startDateText);
							}
						}
					} catch(ex) {}
				});
			}
		}, 1500)
	} 
});

function showContentItem($element) {
	if ($element.length) $('#left').html($('<div class="ContentItemCloned" />').html('<div class="addthis_toolbox addthis_default_style "><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a><a class="addthis_counter addthis_bubble_style"></a></div>' + $element.html()));
	if ($('.bigimagehome').length) {
		$('.ContentItemCloned').find('h2').wrap($('<a href="/Bart-van-Oort/nl-NL/nieuws.aspx" />'));
		$('#left').append('<p style="padding: 12px;"><a href="/Bart-van-Oort/nl-NL/nieuws.aspx">Lees meer</a></p>');
	}
	$('.addthis_toolbox').attr('addthis:title', $('.ContentItemCloned h2').text());
    $('.addthis_toolbox').attr('addthis:description', $('.ContentItemCloned .Header').text());
	$.getScript('http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4e54f9cd16b93ba8', function() { addthis.toolbox(".addthis_toolbox"); });
}

var audioPlayer = function(autoPlay) {
    var self = this;
    this.autoPlay = autoPlay;
    this.loadedSounds = 0;
    if ($('.ContentItemAudioTrack').length) {
        this.initSoundManager(function() {
            self.init();
        });
    }
}

audioPlayer.prototype = {

    init: function() {
        var self = this;
        this.$tracks = $('.ContentItemAudioTrack');
        $('.ContentItemAudioTrack .FormFieldTitle a').click(function() {
            var track = $(this).closest('.ContentItemAudioTrack');
            if (track.hasClass('playing')) self.stop(); else self.go(track); return false;
        });
        this.$buttonPrevious = $('.AudioButtons .previous').click(function() {
            self.previous(); return false;
        });
        this.$buttonTogglePlay = $('.AudioButtons .toggleplay').click(function() {
            self.togglePlay(); return false;
        });
        this.$buttonNext = $('.AudioButtons .next').click(function() {
            self.next(); return false;
        });
    },
    
    initSoundManager: function(onSuccess) {
        var self = this;
        $.getScript('/js/resources/soundmanager/script/soundmanager2-jsmin.js', function() {
            soundManager.debugMode = false;
            soundManager.url = '/js/resources/soundmanager/swf/';
            soundManager.onready(function(oStatus) {
                if (oStatus.success) {
                    // Goto first track
                    onSuccess();
                    if (self.autoPlay) self.goToFirst(false);
                } else {
                    // Error, disable player buttons
                    $('.AudioButtons').css('display', 'none');
                }
            });
        });
    },
    
    go: function(elementOrPath, noPlay) {
        if (this.activeSound) this.activeSound.stop();
        if (this.activeElement) $(this.activeElement).removeClass('playing');
        if (typeof elementOrPath === "string" ) {
            this.activeElement = null;
            this.activePath = buttonOrPath;
            this.activeSound = loadSound(this.activePath);
        } else {
            this.activeElement = elementOrPath;
            this.activePath = $('.FilePath a', elementOrPath).attr('href');
            this.activeSound = $(this.activeElement).data('sound');
            if (!this.activeSound) {
                this.activeSound = this.loadSound(this.activePath);
                $(this.activeElement).data('sound', this.activeSound);
            }
        }
        if (this.activeElement) $(this.activeElement).addClass('playing');
        if (!noPlay) this.play();
    },
    
    goToFirst: function(noPlay) {
        if (this.$tracks.length > 0) this.go(this.$tracks.get(0), noPlay);
    },
    
    loadSound: function(path) {
        var self = this;
        this.loadedSounds += 1;
        return soundManager.createSound({
            id: 'sound' + this.loadedSounds,
            url: path,
            onfinish: function() {self._soundOnFinish(this)}
        });
    },
    
    _soundOnFinish: function(sound) {
        if (sound === this.activeSound) {
            this.next();
        }
    },
    
    previous: function() {
        var nextElement = $(this.activeElement).prev();
        if (nextElement.length > 0) {
            this.go(nextElement);
        } else {
            this.goToFirst();
        }
    },
    
    next: function() {
        var nextElement = $(this.activeElement).next();
        if (nextElement.length > 0) {
            this.go(nextElement);
        } else {
            this.goToFirst();
        }
    },
    
    play: function() {
        this.$buttonTogglePlay.addClass('playing');
        if (this.activeSound) this.activeSound.play();
    },
    
    stop: function() {
        this.$buttonTogglePlay.removeClass('playing');
        if (this.activeElement) $(this.activeElement).removeClass('playing');
        if (this.activeSound) this.activeSound.stop();
    },
    
    togglePlay: function() {
        if (this.isPlaying()) this.stop(); else this.play();
    },
    
    isPlaying: function() {
        return this.$buttonTogglePlay.hasClass('playing');
    }

}
