(function($) {
    function SWindow(target_, options_) {
      	this.expand=expand;
    	this.close=close;
		this.parseOptions=parseOptions;
		this.hide=hide;
		
		this.target=getEl(target_);
		var options={
			expand_class:'ui-icon-triangle-1-s',
			collapse_class:'ui-icon-triangle-1-e',
			close_class:'ui-icon-close'
		};
		this.parseOptions(options_);
		function expand(){
			var exp=this.expandCtrl;
			if(exp.hasClass(options.collapse_class)){
				exp.removeClass(options.collapse_class);
			}
			exp.addClass(options.expand_class);
			exp.parent().next().show();
		}
		function close(){
			this.target.hide();
			return this;
		}
		function hide(){
			this.target.hide();
			return this;
		}
		function parseOptions(op){
			$.extend(true, options, op);
			var self=this;
			if (options.expandCtrl) {
				this.expandCtrl = getEl(options.expandCtrl);
				this.expandCtrl.addClass(options.collapse_class);
				this.expandCtrl.click(function(){
					toggleClass($(this), options.expand_class, options.collapse_class);
					$(this).parent().next().toggle();
					
					return false;
				});
			}
			if (options.closeCtrl) {
				this.closeCtrl = getEl(options.closeCtrl);
				this.closeCtrl.addClass(options.close_class);
				this.closeCtrl.click(function(){
					self.close();
					return false;
				});
			}
		}

    }
	
    
    $.swindow = function(target, options) {
        var swin = new SWindow(target,  options);
        return swin;
    };
	function toggleClass(el, cl1, cl2){
		
		if(el.hasClass(cl1)){
			el.removeClass(cl1);
			el.addClass(cl2);
			
		}
		else if(el.hasClass(cl2)){
			el.removeClass(cl2);
			el.addClass(cl1);
		
		}
		
	}
})(jQuery);	

