/**
 * @package jSimpleToggle
 */
(function($) {
	$.fn.jsimpletoggle = function(stoptions) {
		
	return this.each(function(i){
		var $that = $(this)[0];
		//var $that = $(this).attr('id'); //works
		//var $that = jQuery(this); //doesnt work
		//var $that = $(this); //doesnt work
		
		var thesesettings = $.extend({}, $.fn.jsimpletoggle.defaults, stoptions);

		if (typeof(stoptions) == 'undefined' || typeof(stoptions.container) == 'undefined') {
			if ($(this).attr('id') != 'undefined' && $(this).attr('id') != '#') { 
				thesesettings.container = "#" + $(this).attr('id');
				try { //make sure container exists
					if ($(thesesettings.container).length != 1) //firefox doesnt like it when ids are duplicated
						return;
				} catch (e) {
					$(this).attr({ id: 'jsimpletoggle__container__' + i });
					thesesettings.container = "#" + $(this).attr('id');
				}
			}
		}
		
		if (typeof($(this).attr("jsimpletoggleTarget")) != 'undefined')
			thesesettings.target = $(this).attr("jsimpletoggleTarget");
		if (typeof($(this).attr("jsimpletoggleState")) != 'undefined')
			thesesettings.state = $(this).attr("jsimpletoggleState");
		if (typeof($(this).attr("jsimpletoggleGroup")) != 'undefined')
			thesesettings.group = $(this).attr("jsimpletoggleGroup");

		try { //make sure target exists too 
			if ($(thesesettings.target).length != 1) //firefox doesnt like it when ids are duplicated
				return;
		} catch (e) {
			return;
		}
		
		if (thesesettings.state != 'expanded' || thesesettings.state != 'collapsed')
			thesesettings.state == 'collapsed';

		if (typeof($.fn.jsimpletoggle.settings[$(this).attr('id')]) == 'undefined')
			$.fn.jsimpletoggle.settings[$(this).attr('id')] = thesesettings; 
		else {
			//we already did it once already
			//we could just let them reset element here and then return. uncomment next line
			//$.fn.jsimpletoggle.settings[$(this).attr('id')] = thesesettings; 
			return; 
		}
		
		//$(thesesettings.container).bind("click", {}, $.fn.jsimpletoggle.toggleState);
		$(this).bind("click", {}, $.fn.jsimpletoggle.toggleState); //no need for ID on toggle except that to change the pic we need it
		$(this).addClass("jsimpletoggle_container");
		if ($(this).css('cursor') =='auto' || $(this).css('cursor') =='inherit' || $(this).css('cursor') =='hand')
			$(this).css('cursor', 'pointer');
		
		/* State from the cookie*/
		var $cookie_state = null;
		if (typeof($.cookie) != 'undefined')
			$cookie_state = $.cookie('jsimpletoggle_' + thesesettings.target);
		
		if($cookie_state ==  'collapsed') {
			$(thesesettings.target).hide();
		} else if($cookie_state == null) {
			/* 
			 * If we set the state at init time and no state in the cookie 
			 * then use the init setting. 
			 */
			if(thesesettings.state != '' && thesesettings.state == 'collapsed') {
				$(thesesettings.target).hide();
			}
		}

		var img = document.createElement('img');
		//$(img).attr({ id: 'jsimpletoggle_icon', src: 'images/spacer.gif' }); //firefox requires src, ie doesnt
		$(img).attr({ id: 'jsimpletoggle_icon', src: 'spacer.gif' });
		$(img).addClass("jsimpletoggle_icon");
		if( $(thesesettings.target).css("display") == "block" ) {
			$(img).addClass("jsimpletoggle_iconcollapse");
		} else {
			$(img).addClass("jsimpletoggle_iconexpand");
		}
		//if ($(img).css('background-image') =='auto' || $(this).css('background-image') =='inherit')
		//	$(this).css('background-image', 'expand.gif');
		$(thesesettings.container).prepend(img);

	});	
	};
	
	$.fn.jsimpletoggle.toggleState = function() {
		var thesesettings = $(this).jsimpletoggle.settings[$(this)[0].id];
		
		if (thesesettings.group != '') {
			if ( $(thesesettings.target).css("display") == "none" ) {
				//jQuery.each($(this).jsimpletoggle.settings, function(i, val) { //loop 
				jQuery.each($(this).jsimpletoggle.settings, function() { //loop
					if (this.group == thesesettings.group) {
						if (this.target == thesesettings.target) { 
							toggleImage(this, 1);
							$(this.target).slideDown(500);
						} else {
							toggleImage(this, 0);
							$(this.target).slideUp(500);
						}
					}
				})
			} else {
				jQuery.each($(this).jsimpletoggle.settings, function() { //loop 
					if (this.group == thesesettings.group) {
						toggleImage(this, 0);
						$(this.target).slideUp(500);
					}
				})
			}
		} else {
			toggleImage(thesesettings);
			$(thesesettings.target).slideToggle(500);
		}
	
		function toggleImage(thesesettings, instate) {
			var state = false;
			if (typeof(state) != 'undefined' && state) {
				state = true;
			} else if (typeof(instate) != 'undefined' && !instate) {
				state = false
			} else if ( $(thesesettings.target).css("display") == "none" ) { //hasClass better? //toggleClass better below?
				state = true;
			} else if ( $(thesesettings.target).css("display") == "block" ) { //hasClass better?
				state = false
			}
			if (state) {
				$(thesesettings.container + ' .jsimpletoggle_icon').removeClass("jsimpletoggle_iconexpand");
				$(thesesettings.container + ' .jsimpletoggle_icon').addClass("jsimpletoggle_iconcollapse");
			} else {
				$(thesesettings.container + ' .jsimpletoggle_icon').removeClass("jsimpletoggle_iconcollapse");
				$(thesesettings.container + ' .jsimpletoggle_icon').addClass("jsimpletoggle_iconexpand");
			}
			if (typeof($.cookie) != 'undefined')
				$.cookie('jsimpletoggle_' + thesesettings.target, state ? 'expanded' : 'collapsed', { path: '/', expires: null });
		}

	}
	
	$.fn.jsimpletoggle.settings = {};
	
	$.fn.jsimpletoggle.defaults = {
			container: '#example',
			group: '',
			target: '',
			state: 'collapsed'
	};
})(jQuery);