(function(jQuery) {

jQuery.fn.expandoSection = function(options) {

	var details = {
			name: 'expandoSection',
			description: 'An Expand/Contract section',
			version: '1.0.0'
		};
		// doctype must be set to transitional for IE7 to avoid jumps in the height animation
	var options = jQuery.extend({}, jQuery.fn.expandoSection.defaults, options);
	return this.each(function() {
		jQuery(this).data('owc-ui-type', details.name);
		var controlElement = jQuery(this).find('.showMoreLessControlElement:first'); 
		var contentElement = jQuery(this).find('.showMoreLessContentElement:first');
		controlElement.bind('click', toggleContentElementFunction(options));

		var controlElementImage = "";
		//html author may hide element initially by function call or by setting the content element display value to none
		if (contentElement.css('display') == 'none') {
			controlElementImage = jQuery('<imageSpacer><img src= "' + options.contractedImg + '" alt="Wissel tussen uitklappen/inklappen informatiegedeelte" />&nbsp;</imageSpacer>');
			controlElement.addClass(options.contractedControlStyle);
		} else {
			controlElementImage = jQuery('<imageSpacer><img src= "' + options.expandedImg + '" alt="Wissel tussen uitklappen/inklappen informatiegedeelte" />&nbsp;</imageSpacer>');
			controlElement.addClass(options.expandedControlStyle);
		}
		// convenience for html author, add in control element image
		if (jQuery(this).find('.showMoreLessControlElement:first').children('img').length == 0) {
			jQuery(this).find('.showMoreLessControlElement:first').prepend(controlElementImage);
		}
		//html author may hide element initially
		if (options.initialStateContracted && contentElement.css('display') != 'none') {
			controlElement.click();
		}
	});

};

jQuery.fn.pushDownDiv = function(options) {

	var details = {
			name: 'pushDownDiv',
			description: 'A push-down-div control',
			version: '1.0.0'
		};
	// doctype must be set to transitional for IE7 to avoid jumps in the height animation
	
	var options = jQuery.extend({}, jQuery.fn.pushDownDiv.defaults, options);
	return this.each(function() {
		jQuery(this).data('owc-ui-type', details.name);
		jQuery(this).find('.showMoreLessControlElement:first')
			.bind('click', toggleContentElementFunction(options));
	});
	
};

jQuery.fn.showMoreLessDiv = function(options) {

    var details = {
            name: 'showMoreLessDiv',
            description: 'A show more-less span control',
            version: '1.0.0'
        };
    // doctype must be set to transitional for IE7 to avoid jumps in the height animation

    var options = jQuery.extend({}, jQuery.fn.showMoreLessDiv.defaults, options);
    return this.each(function() {
        jQuery(this).data('owc-ui-type', details.name);
        var controlElement = jQuery(this).find('.showMoreLessControlElement:first');
        controlElement.data('container-div', jQuery(this));
        controlElement.bind('click', toggleShowMoreLessDisplayFunction(options));
    });

};


// coded as a closure to make options available for the callbacks
// functions are used in multiple plugins
function toggleContentElementFunction(options) {
	return function (e) {
			var content = jQuery(this).parent().find('.showMoreLessContentElement:first');
			if (content.is(':hidden')) {
				if (typeof(options.expandedContentStyle) != 'undefined') content.addClass(options.expandedContentStyle);
				jQuery(this).parent().triggerHandler('expanded');
			} else {
				jQuery(this).parent().triggerHandler('contracted');
			}
			content.animate({height: 'toggle', opacity: 'toggle'}, options.speed, options.easing, toggleControlElementFunction(options));
	};
};

//coded as a closure to make options available for the callbacks
//functions are used in multiple plugins
function toggleControlElementFunction(options) {
	return function () {
		var content = jQuery(this).parent().find('.showMoreLessContentElement:first');
		var control = jQuery(this).parent().find('.showMoreLessControlElement:first');
		var toggleText = jQuery(this).parent().find('.showMoreLessControlElement:first span');
		var toggleImg = jQuery(this).parent().find('.showMoreLessControlElement:first img');
		if (content.is(':hidden')) {
			if (typeof(options.expandedContentStyle) != 'undefined') content.removeClass(options.expandedContentStyle);
			if (typeof(options.expandedControlStyle) != 'undefined') control.removeClass(options.expandedControlStyle);
			if (typeof(options.contractedControlStyle) != 'undefined') control.addClass(options.contractedControlStyle);
			if (toggleText.length >= 1) toggleText.text(options.contractedText);
			if (toggleImg.length >= 1) toggleImg.attr('src', (options.contractedImg));
	        if (typeof(options.scrollToTopOnContract) != 'undefined' && options.scrollToTopOnContract) {
				window.scrollTo(0, jQuery(this).parent().offset().top - 0);
	        }
		} else {
			if (typeof(options.contractedControlStyle) != 'undefined') control.removeClass(options.contractedControlStyle);
			if (typeof(options.expandedControlStyle) != 'undefined') control.addClass(options.expandedControlStyle);
			if (toggleText.length >= 1) toggleText.text(options.expandedText);
			if (toggleImg.length >= 1) toggleImg.attr('src', (options.expandedImg));
		}
	};		
};


// coded show more/less sections of the reviews
function toggleShowMoreLessDisplayFunction(options) {
	var callback = toggleControlElementFunction(options);
	return function (e) {
		var container = jQuery(this).data('container-div');
		var content = container.find('.showMoreLessContentElement:first');
			if (content.is(':hidden')) {
				if (typeof(options.expandedContentStyle) != 'undefined') content.addClass(options.expandedContentStyle);
				container.triggerHandler('expanded');
                content.css('display', 'inline');
			} else {
				container.triggerHandler('contracted');
                content.css('display', 'none');
			}
			callback.call(this);

	};
};

jQuery.fn.expandoSection.defaults = {
	expandedImg:			'/wcpa/images/btn_dark_collapse.gif',
	contractedImg:			'/wcpa/images/btn_dark_expand.gif',
	expandedControlStyle:	'expanded-control',
	contractedControlStyle:	'contracted-control',
	initialStateContracted:	false,
	scrollToTopOnContract:	false,
	speed:					'fast',
	easing:					'easeInOutSine'
	};

jQuery.fn.pushDownDiv.defaults = {
	expandedText:			'&AllPage.ShowLess;',
	contractedText:			'&AllPage.ShowMore;',
	expandedImg:			'/wcpa/images/btn-hide.gif',
	contractedImg:			'/wcpa/images/btn-show.gif',
	expandedContentStyle:	'expanded-content',
	expandedControlStyle:	'expanded-control',
	speed:					'fast',
	easing:					'easeInOutSine'
};

jQuery.fn.showMoreLessDiv.defaults = {
		expandedText:			'Minder lezen',
		contractedText:			'Meer lezen...',
		expandedImg:			'/wcpa/images/btn-hide.gif',
		contractedImg:			'/wcpa/images/btn-show.gif',
		expandedContentStyle:	'expanded-content',
		//contractedContentStyle:	'contracted-content',
		expandedControlStyle:	'expanded-control',
		contractedControlStyle:	'contracted-control',
		speed:					'fast',
		easing:					'easeInOutSine'
	};

function log(msg) {
	if (window.console && window.console.log) {
		window.console.log('[OWC-UI]- ' + msg);
	}
	
};

})(jQuery);