	/* -------------------------------------------------- *
 * Collapsorz 1.1
 * Updated: 04/21/09
 * -------------------------------------------------- *
 * Author: Aaron Kuzemchak
 * URL: http://aaronkuzemchak.com/
 * Copyright: 2008-2009 Aaron Kuzemchak
 * License: MIT License
** -------------------------------------------------- */



(function($) {

    var htmlinteractiveflg;

    $(document).ready(function(){

        if(document.getElementById("interactivehtmlflag"))
        {
           htmlinteractiveflg =  document.getElementById("interactivehtmlflag").value;
        }
        else
        {
            htmlinteractiveflg = "0";            
        }

    });

	$.fn.collapsorz = function(options) {

        //jQuery.browser.version = jQuery.browser.msie &&  parseInt(jQuery.browser.version) >= 7 &&   window["XMLHttpRequest"] ? "8.0" : jQuery.browser.version;
        var minItms = 0;

            if(htmlinteractiveflg == "1")
            {
                minItms = 0;
            }
            else
            {
                minItms = 4;
            }

        
        // default settings
		var defaults = {
			toggle: "> *", // elements inside the object to toggle
            minimum: minItms, // number to show in collapsed form
            showText: "More...", // text for the expand link
			hideText: "Less...", // text for the collapse link
			linkLocation: "after", // use "after" or "before" to determine where link displays
			defaultState: "collapsed", // use "collapsed" or "expanded" to show or hide items by default
			wrapLink: '' // specify HTML code to wrap around the link
		};

        var options = $.extend(defaults, options);
        var ieBrowVer = 0;
        
        return this.each(function() {
        var toggleSize =  $(options.toggle, this).length;
        var optMin = options.minimum;

        //Well, I don't know why the toogleSize is different for different
        //browsers(belowe code is easy fix, decrement the toggle size for mozilla and other, it's hack but it works fine)
        if(!$.browser.msie)
        {
                  toggleSize = toggleSize - 1;
                  optMin =  optMin + 2 ;
        }
        else
        {
            if($.browser.version >= 8)
            {
                    optMin =  optMin + 2 ;
            }
            ieBrowVer =  $.browser.version;
        }

       //This check is only for IE 7.0
      if(ieBrowVer != 7.0)
        {
           // only execute if there are more than minimum items
  	       if(toggleSize > optMin)
             {
               // setup variables
				var $obj = $(this);
				var $targets = $(options.toggle, this);

                // hide the items if necessary
				if(options.defaultState == "collapsed")
                {
                        $targets.filter(":gt("+(optMin-1)+")").hide();
				}

				// append/prepend the toggle link to the object
				var $toggler = $('<a href="#" class="toggler"></a>');
             
				if(options.linkLocation == "before") {
					$obj.before($toggler);
				}
				else {
					$obj.after($toggler);
				}

				if(options.wrapLink) {
					$toggler.wrap(options.wrapLink);
				}

               	// set data, link class, and link text
				if(options.defaultState == "expanded")
                {
					$obj.data("status", "expanded");
					$toggler.addClass("expanded");
					$toggler.html(options.hideText);
				}
				else
                {
					$obj.data("status", "collapsed");
					$toggler.addClass("collapsed");
					$toggler.html(options.showText);
				}

             	// click actions
				$toggler.click(function() {
					if($obj.data("status") == "collapsed") {
						$targets.filter(":hidden").show();
						$toggler.html(options.hideText);
						$obj.data("status", "expanded");
					}
					else if($obj.data("status") == "expanded")
                    {
                    $targets.filter(":gt("+(optMin-1)+")").hide();
                    $toggler.html(options.showText);
					$obj.data("status", "collapsed");
            		}
					$(this).toggleClass("collapsed").toggleClass("expanded");
					return false;
				});

			}
        }
		});
}
})(jQuery);
