/*@contentSlide Plugin
 *Todo: multiple elements, utilise switch and styling flags, maybe call destructor(?) [21/04/2011]
 *Update:[22/04/2011]:multiple elems done, secondary plugin test implemented
 *Todo: Write own function that works with secondary plugin, fix divgrow smaller content, implement user-styling[22/04/2011]
 *Notes: divgrow plugin utilises jquery.browser (which may be deprecated in future).
 *Credits:
 */
(function($){
	var ContentSlide = function(element, settings)
	{
		var initalized = 0; //default 0 signifies it hasn't been run
		var elem = $(element);
		var obj = this;

		/*@settings
		 *noOfElement: the numer of target elements
		 *width: the required width of the drop down
		 *minHeight: the minimum height of the single slide box
		 *maxHeight: maximum height of the single slide box
		 *headHeight: the header height of the slide box
		 *contentHeight: the height of the content when maximized
		 * ~Colour: the colour of the three sections
		 * targetElem: storage for the target element
		 * targetClass: class containing elements (can be null)
		 * defaultDisplay: should start open or closed
		 * overflow: how should the content handle overflow use css naming
		 * imageLocation: the url of the open/close button
		 * endFlag: the end flag that signifies end of container
		 */
		var settings = $.extend({
			'noOfElement'				:	0,
			'width'							:	100,
			'minHeight'					: 100,
			'maxHeight'					:	200,
			'headHeight'				:	70,
			'contentHeight'			: 100,
			'footHeight'				: 70,
			'botColour'					:	'#e2e2e2',
			'topColour'					:	'#e2e2e2',
			'midColour'					:	'#e2e2e2',
			'targetElem'				:	'',
			'targetClass'				:	'',
			'defaultDisplay'		:	0,
			'overflow'					:	'hidden',
			'imageLocation'			:	'',
			'endFlag'						: '</div>',
			'initialHeight'		: 100,
			'moreText'		: "-Show More",
			'lessText'		: "-Show Less",
			'speed'				: 1000,
			'showBrackets'	: true
		}, settings || {});

		/*@initPlugin
		 *description:initalise the plugin, 'constructor'
		 *other: public
		 */
		this.initPlugin = function(){					
			if(initalized === 1){	//return if the plugin is already running
				return;
			}
			else if(!jQuery().divgrow){	//secondary plugin undefined
				buildMarkup(false);
			}
			else{
				buildMarkup(true);//a call to build markup
			}
		};

		/*@buildMarkup
		 *description:build the markup around the target(s)
		 *other: private
		 *notes:
		 */
		var buildMarkup = function(plugged){
			initalized = 1; //set the flag
			var i = 0;
			
			//ensure that we're in the correct user-specified class
			if($(settings.targetClass).length <= 0){//if the class doesn't exist, end plugin
				return;
			}
				$(settings.targetClass).wrapInner('<div class="slideContainer"></div><div class="more-block"></div>');

				$(settings.targetElem).each(function(){
					$(this).nextUntil(settings.targetElem).wrapAll('<div class="linkContent"></div>'); //content container
					$(this).wrap('<div class="linkHead"></div>');
					$(this).attr('id', i);
					i++;
				});//header
				//$('.linkContent').after('<div class="linkFoot"></div>');
				$('.linkHead').each(function(){
					$('this').nextUntil('.linkHead').wrapAll('<div class="linkBox"></div')
				});
			if(plugged === true){
			$('.linkContent').divgrow({initialHeight: 195,moreText:'<img src="/images/details_open.png" alt="open"></img>',lessText:'<img src="/images/details_close.png" alt="close"></img>',showBrackets:false});
			}else{
			settings.initialHeight = 195;
			settings.moreText = '<img src="/images/details_open.png" alt="open"></img>';
			settings.lessText = '<img src="/images/details_close.png" alt="close"></img>';
			settings.showBrackets = false;
			defaultSize();
			}
		}

		var defaultSize = function(){
			/*Currently this is copyrighted to Simon Hibbard the author of divgrow*/
			    var divgrowid = 0;
					var temp = 0;
					if(jQuery.browser.mozilla){settings.speed = 0;}
						$('.linkContent').each(function () {
            divgrowid++;
						temp = divgrowid-1;

            obj = $(this);

            var fullHeight = obj.height() + 10;

            obj.css('height', settings.initialHeight).css('overflow', 'hidden');
            obj.addClass('divgrow-wrapper');
            if (settings.showBrackets) {
                obj.after('<p class="divgrow-brackets">[&hellip;]</p><a href="#" class="divgrow-showmore' + " divgrow-obj-" + divgrowid + '"' + '></a>');
            }
            else {
                obj.after('<a href="#'+ temp +'"class="divgrow-showmore' + " divgrow-obj-" + divgrowid + '"' + '></a>');
            }
            $("a.divgrow-showmore").html(settings.moreText);

            $("." + "divgrow-obj-" + divgrowid).toggle(function (e) {
              //  alert(obj.attr('class'));
								 //alert(e);
                // Set the height from the elements rel value
                //var height = $(this).prevAll("div:first").attr('rel');

                $(this).prevAll(".divgrow-wrapper:first").animate({ height: fullHeight + "px" }, settings.speed, function () { // Animation complete.
                    // Hide the overlay text when expanded, change the link text
                    if (settings.showBrackets) {
                        $(this).nextAll("p.divgrow-brackets:first").fadeOut();
                    }
                    $(this).nextAll("a.divgrow-showmore:first").html(settings.lessText);
                });
            }, function (e) {
                $(this).prevAll(".divgrow-wrapper:first").stop(true, false).animate({ height: settings.initialHeight }, settings.speed, function () {// Animation complete.
										// show the overlay text while closed, change the link text
                    if (settings.showBrackets) {
                        $(this).nextAll("p.divgrow-brackets:first").stop(true, false).fadeIn();
                    }
                    $(this).nextAll("a.divgrow-showmore:first").stop(true, false).html(settings.moreText);
                });
            });
        });
		}

	};

	jQuery.fn.contentSlide = function(settings)
	{
		return this.each(function(){
			var element = $(this);
			if(element.data('myplugin')) {
				return;
			}
			var contentSlide = new ContentSlide(this, settings);
			element.data('contentSlide', contentSlide);
		});
	};
})(jQuery);
