
(function( $ ){
  var methods = {
    init : function( options ) {
    	return this.each(function(){
    		var $this = $(this),
    		data = $this.data('bookmark'),
    		bookmark = {};
    		for(i in options)
    		{
    			bookmark[i] = options[i];
    		}
    		bookmark.dsnr = $this.children('input').val();
    		if( $this.hasClass('bookmark-activated'))
    		{
    			bookmark.activated = true;
    		}
    		bookmark.type = 'bookmark';
    		if( $this.hasClass('vergleichsliste') )
    		{
    			bookmark.type = 'compare';
    		}
    		$this.bind('click.bookmark',methods.toggle);
    		if( !data )
    		{
    			$this.data('bookmark',{
    				target: $this,
    				bookmark: bookmark
    			});
    			data = $this.data('bookmark');
    		}
    		if( data.bookmark.activated )
    		{
    			$this.html(data.bookmark.active);
    		}
    		else
			{
    			$this.html(data.bookmark.inactive);
			}
    	});
	//dsnr auslesen
	//status speichern
	//erstbefüllung
    },
    active: function () {
    	return this.each(function(){
    		var $this = $(this),
    		data = $this.data('bookmark');
   			$this.html(data.bookmark.active);
   			data.bookmark.activated = true;
    	});
    },
    toggle: function( ) {
    	return $(this).each(function(){
    		var $this = $(this),
    		data = $this.data('bookmark');
    		var opt = {};
    		opt.ajax = true;
    		if( data.bookmark.activated )
    		{
   				opt['del'+data.bookmark.type] = data.bookmark.dsnr;
    		}
    		else
    		{
    			opt['add'+data.bookmark.type] = data.bookmark.dsnr;
    			if( data.bookmark.type == 'compare')
    			{
    				_gaq.push(['_trackPageview','Vergleichsliste']);
    			}
    			else{
    				_gaq.push(['_trackPageview','Merkliste']);
    			}
    		}
		 	$.ajax({
		 		url:'/',
				global: false,
				type: 'post',
				data: opt,
				jsonp: 'jsonp',
				dataType: 'jsonp',
				async: true,
/*				type: 'GET',
				datatype: 'json',
				global: false,
				async: true,*/
				data:opt,
				success:function (adata, textStatus, XMLHttpRequest)
						{
							if( adata.ok )
							{
								data.bookmark.activated = !data.bookmark.activated;
								if( data.bookmark.activated )
								{
									$this.html(data.bookmark.active);
								}
								else
								{
									$this.html(data.bookmark.inactive);
								}
								if( typeof(data.bookmark.success) == 'function' )
								{
									data.bookmark.success();
								}
								if( data.bookmark.type == 'compare' )
								{
									$('#anzahlvergleich').html(adata.cnt);
								}
								else
								{
									$('#anzahlmerkliste').html(adata.cnt);
								}
							}
							else
							{
								if( adata.err == 1 )
								{
									showMessageBox('vergleichlimit');
								}
								else
								{
									showMessageBox('fehler',adata.msg);
								}
							}
						},
				error: data.bookmark.syserror
				});
    	});

	//ajax ausführen
	//entsprechend ergebnis, Status ändern
    //status Callback ausführen, sofern vorhanden
	},
	destroy: function(){
 		return this.each(function(){
         var $this = $(this),
             data = $this.data('bookmark');
         data.bookmark.remove();
         $this.removeData('bookmark');
         $this.unbind('.bookmark');
       });
	}
  };

  $.fn.bookmark = function( method ) {
    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }
  };

})( jQuery );
